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/index.bzl b/index.bzl index 9d36662b07..8cdf14971a 100644 --- a/index.bzl +++ b/index.bzl @@ -27,7 +27,9 @@ load("//internal/js_library:js_library.bzl", _js_library = "js_library") load( "//internal/node:node.bzl", _nodejs_binary = "nodejs_binary_macro", + _nodejs_binary_toolchains = "nodejs_binary_toolchain_macro", _nodejs_test = "nodejs_test_macro", + _nodejs_test_toolchains = "nodejs_test_toolchain_macro", ) load("//internal/node:node_repositories.bzl", _node_repositories = "node_repositories") load("//internal/node:npm_package_bin.bzl", _npm_bin = "npm_package_bin") @@ -41,6 +43,8 @@ load( check_bazel_version = _check_bazel_version nodejs_binary = _nodejs_binary +nodejs_binary_toolchains = _nodejs_binary_toolchains +nodejs_test_toolchains = _nodejs_test_toolchains nodejs_test = _nodejs_test node_repositories = _node_repositories pkg_npm = _pkg_npm diff --git a/internal/node/node.bzl b/internal/node/node.bzl index 58628d1614..82d6b25ef0 100644 --- a/internal/node/node.bzl +++ b/internal/node/node.bzl @@ -250,8 +250,10 @@ 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_tool_files.extend(ctx.attr.toolchain[platform_common.ToolchainInfo].nodeinfo.tool_files) + else: + node_tool_files.extend(ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo.tool_files) node_tool_files.append(ctx.file._link_modules_script) node_tool_files.append(ctx.file._runfile_helpers_bundle) @@ -597,14 +599,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, @@ -658,6 +661,27 @@ def nodejs_binary_macro(name, **kwargs): **kwargs ) +def nodejs_binary_toolchain_macro(name, toolchains = [], **kwargs): + """This extens nodejs binary and allows for multiple toolchains to be passed as a list and + a target to be created for each one automatically + """ + entry = maybe_directory_file_path(name, kwargs.pop("entry_point", None)) + rule = 0 + + # Is there a good way to resolve the naming for the version chosen for each select statement? + # ideally able to produce the target name name_@toolchain ex: main_multiple_@node16_linux_amd64 + # Is there a different outcome that would be better? + print(toolchains) + for toolchain in toolchains: + nodejs_binary( + name = "{name}_{toolchain}".format(name = name, toolchain = rule), + entry_point = entry, + node = toolchain, + toolchain = toolchain, + **kwargs + ) + rule += 1 + nodejs_test_kwargs = dict( nodejs_binary_kwargs, attrs = dict(nodejs_binary_kwargs["attrs"], **{ @@ -710,3 +734,23 @@ def nodejs_test_macro(name, **kwargs): entry_point = maybe_directory_file_path(name, kwargs.pop("entry_point", None)), **kwargs ) + +def nodejs_test_toolchain_macro(name, toolchains = [], **kwargs): + """This extens nodejs test and allows for multiple toolchains to be passed as a list and + a target to be created for each one automatically + """ + entry = maybe_directory_file_path(name, kwargs.pop("entry_point", None)) + rule = 0 + + # Is there a good way to resolve the naming for the version chosen for each select statement? + # ideally able to produce the target name name_@toolchain ex: main_multiple_test_@node16_linux_amd64 + # Is there a different outcome that would be better? + for toolchain in toolchains: + nodejs_test( + name = "{name}_{toolchain}".format(name = name, toolchain = rule), + entry_point = entry, + node = toolchain, + toolchain = toolchain, + **kwargs + ) + rule += 1 diff --git a/internal/node/test/BUILD.bazel b/internal/node/test/BUILD.bazel index 51946cdd6f..968ca255e7 100644 --- a/internal/node/test/BUILD.bazel +++ b/internal/node/test/BUILD.bazel @@ -1,4 +1,4 @@ -load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "nodejs_binary", "nodejs_test", "npm_package_bin") +load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test", "nodejs_binary", "nodejs_binary_toolchains", "nodejs_test", "nodejs_test_toolchains", "npm_package_bin") load("@npm//typescript:index.bzl", "tsc") load("//:index.bzl", "js_library") load("//nodejs:repositories.bzl", "BUILT_IN_NODE_PLATFORMS") @@ -604,3 +604,75 @@ 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_0 +# bazel build //internal/node/test:main_multiple_1 +# the numbers are based on the order that the toolchains are passed to the rule +nodejs_binary_toolchains( + name = "main_multiple", + entry_point = "binary_version.js", + toolchains = [ + # 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 tests: +# bazel test //internal/node/test:main_multiple_test_15_0 +# bazel test //internal/node/test:main_multiple_test_15_1 +# the numbers are based on the order that the toolchains are passed to the rule +nodejs_test_toolchains( + name = "main_multiple_test_15", + entry_point = "toolchain_test_v15.14.0.js", + toolchains = [ + # 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": "@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", + }), + 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_multiple_test_16_0 +# bazel test //internal/node/test:main_multiple_test_16_1 +# the numbers are based on the order that the toolchains are passed to the rule +nodejs_test_toolchains( + name = "main_multiple_test_16", + entry_point = "toolchain_test_v16.js", + toolchains = [ + # 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": "@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", + }), + ], +)