Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(builtin): fix npm_version_check.js when running outside of bazel #1802

Merged
merged 1 commit into from
Apr 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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`);
}