Skip to content

Commit

Permalink
fix(builtin): fix npm_version_check.js when running outside of bazel (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored Apr 7, 2020
1 parent 1667d50 commit afabe89
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions internal/npm_version_check.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
#!/usr/bin/env node

// Fetch the version of this package from its package.json
const pkg = require('./package.json');
const pkgVersion = pkg.version;
const rules_nodejsVersion = process.env['BUILD_BAZEL_RULES_NODEJS_VERSION'];
const pkgVersion = pkg.version || '0.0.0';

// BUILD_BAZEL_RULES_NODEJS_VERSION is only set when within the bazel context
const rulesVersion = process.env['BUILD_BAZEL_RULES_NODEJS_VERSION'] || '0.0.0';

const getMajor = versionString => versionString.split('.')[0];
const getMajor = versionString => versionString ? versionString.split('.')[0] : '';

// special case the version 0.0.0 so that we don't have to stamp builds
// for dev and testing
if (pkgVersion !== '0.0.0' && getMajor(pkgVersion) !== getMajor(rules_nodejsVersion)) {
// Special cases when either version is 0.0.0.
// rulesVersion will be 0.0.0 when outside of bazel
// pkgVersion may be 0.0.0 for dev builds that are not stamped
if (rulesVersion !== '0.0.0' && pkgVersion !== '0.0.0' &&
getMajor(pkgVersion) !== getMajor(rulesVersion)) {
throw new Error(`Expected package major version to equal @build_bazel_rules_nodejs major version
${pkg.name} - ${pkgVersion}
@build_bazel_rules_nodejs - ${rules_nodejsVersion}
@build_bazel_rules_nodejs - ${rulesVersion}
See https://github.com/bazelbuild/rules_nodejs/wiki/Avoiding-version-skew`);
}

0 comments on commit afabe89

Please sign in to comment.