From 006ded0938a565c18fdfe49e3e7f603ab4570314 Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Wed, 23 Dec 2020 10:06:47 -0500 Subject: [PATCH] feat: add support for NodeJS 15.x Include support for NodeJS 15.x, updates some examples to use this Switches bazel-in-bazel example tests to use a tar file for package.json replacements --- examples/vendored_node/WORKSPACE | 14 +++++------ examples/vendored_node/vendored.spec.js | 4 ++-- examples/vue/WORKSPACE | 5 +++- .../bazel_integration_test.bzl | 7 ++++++ .../bazel_integration_test/test_runner.js | 24 ++++++++++++++++--- internal/node/node_versions.bzl | 12 ++++++++++ scripts/update-nodejs-versions.js | 2 +- tools/defaults.bzl | 14 ++++++++++- 8 files changed, 67 insertions(+), 15 deletions(-) diff --git a/examples/vendored_node/WORKSPACE b/examples/vendored_node/WORKSPACE index 6fc234c449..2850df781b 100644 --- a/examples/vendored_node/WORKSPACE +++ b/examples/vendored_node/WORKSPACE @@ -26,10 +26,10 @@ http_archive( ) http_archive( - name = "vendored_node_10_12_0", - build_file_content = """exports_files(["node-v10.12.0-linux-x64/bin/node"])""", - sha256 = "4eba2e9a6db95745b769915d58e57df6ca6724ec1f023f76556fce30ceca2367", - urls = ["https://nodejs.org/dist/v10.12.0/node-v10.12.0-linux-x64.tar.xz"], + name = "vendored_node_15_0_1", + build_file_content = """exports_files(["node-v15.0.1-linux-x64/bin/node"])""", + sha256 = "cc9c3eed21755b490e5333ccab208ce15b539c35f64a764eeeae77c58746a7ff", + urls = ["https://nodejs.org/dist/v15.0.1/node-v15.0.1-linux-x64.tar.xz"], ) load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "npm_install") @@ -37,14 +37,14 @@ load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "npm_install" # This example only works on linux as it downloads the linux node distribution # TODO(gregmagolan): make node_repositories acccept different archives for different platforms node_repositories( - node_version = "10.12.0", - vendored_node = "@vendored_node_10_12_0//:node-v10.12.0-linux-x64", + node_version = "15.0.1", + vendored_node = "@vendored_node_15_0_1//:node-v15.0.1-linux-x64", ) npm_install( name = "npm", data = [ - "@vendored_node_10_12_0//:node-v10.12.0-linux-x64/bin/node", + "@vendored_node_15_0_1//:node-v15.0.1-linux-x64/bin/node", ], package_json = "//:package.json", package_lock_json = "//:package-lock.json", diff --git a/examples/vendored_node/vendored.spec.js b/examples/vendored_node/vendored.spec.js index 6942f476fe..fbc97956b8 100644 --- a/examples/vendored_node/vendored.spec.js +++ b/examples/vendored_node/vendored.spec.js @@ -1,5 +1,5 @@ describe('vendored node', () => { - it('version should be 10.12.0', () => { - expect(process.version).toBe('v10.12.0'); + it('version should be 15.0.1', () => { + expect(process.version).toBe('v15.0.1'); }); }); diff --git a/examples/vue/WORKSPACE b/examples/vue/WORKSPACE index 850295a80e..9e8ef281fc 100644 --- a/examples/vue/WORKSPACE +++ b/examples/vue/WORKSPACE @@ -13,7 +13,10 @@ http_archive( load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "npm_install") -node_repositories(package_json = ["//:package.json"]) +node_repositories( + node_version = "15.0.1", + package_json = ["//:package.json"], +) npm_install( name = "npm", diff --git a/internal/bazel_integration_test/bazel_integration_test.bzl b/internal/bazel_integration_test/bazel_integration_test.bzl index 61d6b9877f..4da5770340 100644 --- a/internal/bazel_integration_test/bazel_integration_test.bzl +++ b/internal/bazel_integration_test/bazel_integration_test.bzl @@ -269,6 +269,12 @@ def rules_nodejs_integration_test(name, **kwargs): repositories = kwargs.pop("repositories", {}) repositories["//:release"] = "build_bazel_rules_nodejs" + # convert the npm packages into the tar output + npm_packages = kwargs.pop("npm_packages", {}) + _tar_npm_packages = {} + for key in npm_packages: + _tar_npm_packages[key + ".tar"] = npm_packages[key] + bazel_integration_test( name = name, check_npm_packages = NPM_PACKAGES, @@ -278,6 +284,7 @@ def rules_nodejs_integration_test(name, **kwargs): bazelrc_imports = { "//:common.bazelrc": "import %workspace%/../../common.bazelrc", }, + npm_packages = _tar_npm_packages, tags = tags, **kwargs ) diff --git a/internal/bazel_integration_test/test_runner.js b/internal/bazel_integration_test/test_runner.js index 00824481b7..1fedcee86c 100644 --- a/internal/bazel_integration_test/test_runner.js +++ b/internal/bazel_integration_test/test_runner.js @@ -169,11 +169,13 @@ function copyNpmPackage(packagePath) { const to = tmp.dirSync({keep: DEBUG, unsafeCleanup: !DEBUG}).name; const from = RUNFILES_MANIFEST ? RUNFILES_MANIFEST[packagePath] : path.posix.join(process.cwd(), '..', packagePath); - if (!isDirectory(from)) { + if (!isFile(from)) { throw new Error(`npm package ${packagePath} not found at ${from}`); } - copyFolderSync(from, to); - return to; + + const parsed = path.parse(from); + copyFolderSync(parsed.dir, to); + return path.join(to, parsed.base); } const workspacePath = config.workspaceRoot.startsWith('external/') ? @@ -243,6 +245,11 @@ if (config.bazelrcAppend) { // Handle package.json replacements const packageJsonFile = path.posix.join(workspaceRoot, 'package.json'); +const packageJsonLockFile = path.posix.join(workspaceRoot, 'package-lock.json'); +const packageJsonLockContents = isFile(packageJsonLockFile) ? + JSON.parse(fs.readFileSync(packageJsonLockFile, {encoding: 'utf-8'})) : + undefined; + if (isFile(packageJsonFile)) { let packageJsonContents = fs.readFileSync(packageJsonFile, {encoding: 'utf-8'}); @@ -259,8 +266,19 @@ if (isFile(packageJsonFile)) { packageJsonKey} failed!`) process.exit(1); } + + if (packageJsonLockContents && packageJsonLockContents.packages && packageJsonKey) { + // keeping the entry results in a npm error, but removing it solves it + // TODO(matt): regenerate the entry here when I understand the filepaths :/ + delete packageJsonLockContents.packages[`node_modules/${packageJsonKey}`]; + } } + fs.writeFileSync(packageJsonFile, packageJsonContents); + + if (packageJsonLockContents) { + fs.writeFileSync(packageJsonLockFile, JSON.stringify(packageJsonLockContents, null, 2)); + } } const packageJsonReplacementKeys = Object.keys(config.packageJsonRepacements); diff --git a/internal/node/node_versions.bzl b/internal/node/node_versions.bzl index 7b24fb5d9f..62f386faf9 100644 --- a/internal/node/node_versions.bzl +++ b/internal/node/node_versions.bzl @@ -1090,4 +1090,16 @@ NODE_VERSIONS = { "14.15.3-linux_s390x": ("node-v14.15.3-linux-s390x.tar.xz", "node-v14.15.3-linux-s390x", "eb976b0d14cc629be3b1d03516ec5516c27d3655f84f11d23af15f4f83d34e27"), "14.15.3-linux_amd64": ("node-v14.15.3-linux-x64.tar.xz", "node-v14.15.3-linux-x64", "6715ecd8b5ab626b4de8454bd0da629a6194bf8f25db71545da64b78b95e4350"), "14.15.3-windows_amd64": ("node-v14.15.3-win-x64.zip", "node-v14.15.3-win-x64", "621de473d22debc0ab1e9a336dc2112d6efca7f7daa769018153e4309e6da844"), + # 15.0.0 + "15.0.0-darwin_amd64": ("node-v15.0.0-darwin-x64.tar.gz", "node-v15.0.0-darwin-x64", "f9eee1f659d96991bc629ec1cec986f504242fc0f046f4487d2fe13b9ab37c99"), + "15.0.0-linux_arm64": ("node-v15.0.0-linux-arm64.tar.xz", "node-v15.0.0-linux-arm64", "52aba1b117499680adea5fec43083fcb94a3483eda182ea82ca47f5d7c9eec97"), + "15.0.0-linux_s390x": ("node-v15.0.0-linux-s390x.tar.xz", "node-v15.0.0-linux-s390x", "7c916b1f3729a66701a0fc0ffe0afde05e401ccc3c53e7114db37cfbf4604f27"), + "15.0.0-linux_amd64": ("node-v15.0.0-linux-x64.tar.xz", "node-v15.0.0-linux-x64", "054c1c20ee237614e12ee2baab1ec96bfafc835a2d36fb2b860fdf10be0777b0"), + "15.0.0-windows_amd64": ("node-v15.0.0-win-x64.zip", "node-v15.0.0-win-x64", "fe8358e18a1f5a44849420d9d76c755e7489f92e0933c8d780107cd688e1d3d6"), + # 15.0.1 + "15.0.1-darwin_amd64": ("node-v15.0.1-darwin-x64.tar.gz", "node-v15.0.1-darwin-x64", "8f7e2ddd44d2aef20d568489f2cf844383037725ce2fc04ad722a312ef08b2d0"), + "15.0.1-linux_arm64": ("node-v15.0.1-linux-arm64.tar.xz", "node-v15.0.1-linux-arm64", "403571f7e37dfefd7bd46411fff4ec7d81bf3d1a34feb37939ad35a06e61d855"), + "15.0.1-linux_s390x": ("node-v15.0.1-linux-s390x.tar.xz", "node-v15.0.1-linux-s390x", "537fec4b3e2c06459991d25641da83533fee551575326b36d54e44364a10678a"), + "15.0.1-linux_amd64": ("node-v15.0.1-linux-x64.tar.xz", "node-v15.0.1-linux-x64", "cc9c3eed21755b490e5333ccab208ce15b539c35f64a764eeeae77c58746a7ff"), + "15.0.1-windows_amd64": ("node-v15.0.1-win-x64.zip", "node-v15.0.1-win-x64", "efa7a74d91789a6e9f068f375e49f108ff87578fd88ff4b4e7fefd930c04db6c"), } diff --git a/scripts/update-nodejs-versions.js b/scripts/update-nodejs-versions.js index 3fe82b0da3..f34e17e818 100644 --- a/scripts/update-nodejs-versions.js +++ b/scripts/update-nodejs-versions.js @@ -5,7 +5,7 @@ const https = require("https"); const MIN_VERSION = [8, 0, 0]; -const MAX_VERSION = [14, 99, 99]; +const MAX_VERSION = [15, 0, 1]; const REPOSITORY_TYPES = { "darwin-x64.tar.gz": "darwin_amd64", diff --git a/tools/defaults.bzl b/tools/defaults.bzl index e09fdda8b5..0eb6e207df 100644 --- a/tools/defaults.bzl +++ b/tools/defaults.bzl @@ -55,7 +55,7 @@ def pkg_npm(**kwargs): "0.0.0-PLACEHOLDER": "{STABLE_BUILD_SCM_VERSION}", }) - # Finally call through to the rule with our defaults set + # Call through to the rule with our defaults set _pkg_npm( deps = deps, substitutions = select({ @@ -66,6 +66,18 @@ def pkg_npm(**kwargs): **kwargs ) + # create a tar that can be used in an action, consumed by the examples + # tagged as manual so this doesn't case two actions for each input with builds for "host" (as used as a tool) + name = kwargs.get("name") + native.genrule( + name = "%s.tar" % name, + outs = ["%s.tgz" % pkg], + cmd = "$(location :%s.pack) | xargs -I {} cp {} $@" % name, + tools = [":%s.pack" % name], + tags = ["manual"], + visibility = visibility, + ) + _GLOBAL_OWNERS = [ "@alexeagle", "@gregmagolan",