Skip to content

Commit 7af0598

Browse files
committed
build: setup toolchain for nodejs v12 for integration tests
Sets up another toolchain for the NodeJS v12 version. This allows us to use Node v12 in integration tests without having to manually manage the Node version in the integration test which runs usually with the default Node workspace toolchain. Note that we need a small patch to `rules_nodejs` as the standalone node toolchain feature is technically internal (but shipped). Rules NodeJS is in progress of migrating from their current workspace name to `@rules_nodejs//`. We temporarily patch the external repository to work with the workspace name we use with `@build_bazel_rules_nodejs#v4`.
1 parent f84ca05 commit 7af0598

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

WORKSPACE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
88
# Add NodeJS rules
99
http_archive(
1010
name = "build_bazel_rules_nodejs",
11+
patches = ["//tools:multiple-node-versions.patch"],
1112
sha256 = "3635797a96c7bfcd0d265dacd722a07335e64d6ded9834af8d3f1b7ba5a25bba",
1213
urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/4.3.0/rules_nodejs-4.3.0.tar.gz"],
1314
)
@@ -60,6 +61,16 @@ node_repositories(
6061
package_json = ["//:package.json"],
6162
)
6263

64+
load("@build_bazel_rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")
65+
66+
# This call sets up another repository for Node 12.x used in integration tests. This
67+
# allows us to ensure our schematic code works with NodeJS v12 LTS. The Node v12.x
68+
# version is not fetched unless explicitly requested by the tests.
69+
nodejs_register_toolchains(
70+
name = "node12",
71+
node_version = "12.20.0",
72+
)
73+
6374
load("//tools:integration.bzl", "create_npm_package_archive_build_file")
6475

6576
yarn_install(

tools/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package(default_visibility = ["//visibility:public"])
22

33
exports_files([
4-
"system-config-tmpl.js",
4+
"multiple-node-versions.patch",
55
])
66

77
# Bazel config setting that matches if the partial compilation flag

tools/defaults.bzl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,10 +347,10 @@ def node_integration_test(data = [], tool_mappings = {}, **kwargs):
347347
# because the `yarn_bin` target is not a self-contained standalone binary.
348348
"@nodejs//:yarn_files",
349349
],
350-
tool_mappings = dict(tool_mappings, **{
350+
tool_mappings = dict({
351351
"@nodejs//:yarn_bin": "yarn",
352352
"@nodejs//:node_bin": "node",
353-
}),
353+
}, **tool_mappings),
354354
**kwargs
355355
)
356356

tools/multiple-node-versions.patch

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
diff --git nodejs/private/toolchains_repo.bzl nodejs/private/toolchains_repo.bzl
2+
index 9dcdc60..4927e75 100644
3+
--- nodejs/private/toolchains_repo.bzl
4+
+++ nodejs/private/toolchains_repo.bzl
5+
@@ -70,7 +70,7 @@ def _toolchains_repo_impl(repository_ctx):
6+
7+
# Forward all the providers
8+
def _resolved_toolchain_impl(ctx):
9+
- toolchain_info = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"]
10+
+ toolchain_info = ctx.toolchains["@build_bazel_rules_nodejs//nodejs:toolchain_type"]
11+
return [
12+
toolchain_info,
13+
toolchain_info.default,
14+
@@ -82,7 +82,7 @@ def _resolved_toolchain_impl(ctx):
15+
# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl
16+
resolved_toolchain = rule(
17+
implementation = _resolved_toolchain_impl,
18+
- toolchains = ["@rules_nodejs//nodejs:toolchain_type"],
19+
+ toolchains = ["@build_bazel_rules_nodejs//nodejs:toolchain_type"],
20+
incompatible_use_toolchain_transition = True,
21+
)
22+
"""
23+
@@ -107,7 +107,7 @@ toolchain(
24+
exec_compatible_with = {compatible_with},
25+
target_compatible_with = {compatible_with},
26+
toolchain = "@{user_node_repository_name}_{platform}//:node_toolchain",
27+
- toolchain_type = "@rules_nodejs//nodejs:toolchain_type",
28+
+ toolchain_type = "@build_bazel_rules_nodejs//nodejs:toolchain_type",
29+
)
30+
""".format(
31+
platform = platform,
32+
diff --git nodejs/repositories.bzl nodejs/repositories.bzl
33+
index 494217b..6dfb8fb 100644
34+
--- nodejs/repositories.bzl
35+
+++ nodejs/repositories.bzl
36+
@@ -690,7 +690,7 @@ filegroup(
37+
# the platform attribute is only set when used from this file, not from build_bazel_rules_nodejs
38+
if repository_ctx.attr.platform:
39+
build_content += """
40+
-load("@rules_nodejs//nodejs:toolchain.bzl", "node_toolchain")
41+
+load("@build_bazel_rules_nodejs//nodejs:toolchain.bzl", "node_toolchain")
42+
node_toolchain(name = "node_toolchain", target_tool = ":node_bin")
43+
"""
44+
repository_ctx.file("BUILD.bazel", content = build_content)

0 commit comments

Comments
 (0)