diff --git a/docs/Built-ins.md b/docs/Built-ins.md index 1f0059af65..e9d1ec39dd 100755 --- a/docs/Built-ins.md +++ b/docs/Built-ins.md @@ -263,7 +263,7 @@ Defaults to `"1.22.11"`
 nodejs_binary(name, chdir, configuration_env_vars, data, default_env_vars, entry_point, env,
-              link_workspace_root, node, templated_args)
+              link_workspace_root, node, templated_args, toolchain)
 
Runs some JavaScript code in NodeJS. @@ -484,6 +484,12 @@ Predefined genrule variables are not supported in this context. Defaults to `[]` +

toolchain

+ +(*Label*) + +Defaults to `None` + ## nodejs_test @@ -491,7 +497,7 @@ Defaults to `[]`
 nodejs_test(name, chdir, configuration_env_vars, data, default_env_vars, entry_point, env,
-            expected_exit_code, link_workspace_root, node, templated_args)
+            expected_exit_code, link_workspace_root, node, templated_args, toolchain)
 
@@ -744,6 +750,12 @@ Predefined genrule variables are not supported in this context. Defaults to `[]` +

toolchain

+ +(*Label*) + +Defaults to `None` + ## npm_install diff --git a/docs/Cypress.md b/docs/Cypress.md index b5e33248b9..20943a59c7 100755 --- a/docs/Cypress.md +++ b/docs/Cypress.md @@ -126,7 +126,7 @@ Defaults to `None`
 cypress_web_test(name, chdir, config_file, configuration_env_vars, cypress_npm_package, data,
                  default_env_vars, entry_point, env, expected_exit_code, link_workspace_root, node,
-                 plugin_file, srcs, templated_args)
+                 plugin_file, srcs, templated_args, toolchain)
 
@@ -349,6 +349,12 @@ Predefined genrule variables are not supported in this context. Defaults to `[]` +

toolchain

+ +(*Label*) + +Defaults to `None` + ## cypress_repositories diff --git a/internal/node/node.bzl b/internal/node/node.bzl index 58628d1614..e3a518f491 100644 --- a/internal/node/node.bzl +++ b/internal/node/node.bzl @@ -250,9 +250,12 @@ fi # when building the image as that will reflect the selected --platform. node_tool_files = ctx.files.node[:] - # this should be resolved the same as above - node_tool_files.extend(ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo.tool_files) + if ctx.attr.toolchain: + node_toolchain = ctx.attr.toolchain[platform_common.ToolchainInfo] + else: + node_toolchain = ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"] + node_tool_files.extend(node_toolchain.nodeinfo.tool_files) node_tool_files.append(ctx.file._link_modules_script) node_tool_files.append(ctx.file._runfile_helpers_bundle) node_tool_files.append(ctx.file._runfile_helpers_main) @@ -597,14 +600,15 @@ Predefined genrule variables are not supported in this context. default = Label("@nodejs//:node_bin"), allow_single_file = True, ), - "_node_patches_script": attr.label( - default = Label("//internal/node:node_patches.js"), - allow_single_file = True, - ), + "toolchain": attr.label(), "_repository_args": attr.label( default = Label("@nodejs//:bin/node_repo_args.sh"), allow_single_file = True, ), + "_node_patches_script": attr.label( + default = Label("//internal/node:node_patches.js"), + allow_single_file = True, + ), "_require_patch_template": attr.label( default = Label("//internal/node:require_patch.js"), allow_single_file = True, diff --git a/internal/node/test/BUILD.bazel b/internal/node/test/BUILD.bazel index 51946cdd6f..6632188430 100644 --- a/internal/node/test/BUILD.bazel +++ b/internal/node/test/BUILD.bazel @@ -604,3 +604,74 @@ nodejs_test( "@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_toolchain", }), ) + +# these rules create different targets for each toolchain that is supplied in the list for toolchains +# this makes it easy to build or test against multiple versions of a toolchain at the same time with less duplicate work +# this will create 2 nodejs_binary targets +# bazel build //internal/node/test:main_multiple_loop_node16 +# bazel build //internal/node/test:main_multiple_loop_node15 +# the numbers are based on the order that the toolchains are passed to the rule +[ + nodejs_binary( + name = "main_multiple_loop_" + id, + entry_point = "binary_version.js", + node = tc, + toolchain = tc, + ) + for id, tc in zip( + [ + "node16", + "node15", + ], + [ + # using the select statement will download toolchains for all platforms + # you can also just provide an individual toolchain if you don't want to download them all + select({ + "@bazel_tools//src/conditions:linux_x86_64": "@node16_linux_amd64//:node_toolchain", + "@bazel_tools//src/conditions:darwin": "@node16_darwin_amd64//:node_toolchain", + "@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_toolchain", + }), + select({ + "@bazel_tools//src/conditions:linux_x86_64": "@node15_linux_amd64//:node_toolchain", + "@bazel_tools//src/conditions:darwin": "@node15_darwin_amd64//:node_toolchain", + "@bazel_tools//src/conditions:windows": "@node15_windows_amd64//:node_toolchain", + }), + ], + ) +] + +# this will create 2 test: +# bazel test //internal/node/test:main_test_multiple_loop_node16 +# bazel test //internal/node/test:main_test_multiple_loop_node15 +# the numbers are based on the order that the toolchains are passed to the rule +[ + nodejs_test( + name = "main_test_multiple_loop_" + id, + data = [ + "data/data.json", + ], + entry_point = ":data_resolution.spec.js", + node = tc, + toolchain = tc, + ) + for id, tc in zip( + [ + "node16", + "node15", + ], + [ + # using the select statement will download toolchains for all platforms + # you can also just provide an individual toolchain if you don't want to download them all + select({ + "@bazel_tools//src/conditions:linux_x86_64": "@node16_linux_amd64//:node_toolchain", + "@bazel_tools//src/conditions:darwin": "@node16_darwin_amd64//:node_toolchain", + "@bazel_tools//src/conditions:windows": "@node16_windows_amd64//:node_toolchain", + }), + select({ + "@bazel_tools//src/conditions:linux_x86_64": "@node15_linux_amd64//:node_toolchain", + "@bazel_tools//src/conditions:darwin": "@node15_darwin_amd64//:node_toolchain", + "@bazel_tools//src/conditions:windows": "@node15_windows_amd64//:node_toolchain", + }), + ], + ) +]