diff --git a/docs/js_binary.md b/docs/js_binary.md index 28e88d3d5..87af1662f 100644 --- a/docs/js_binary.md +++ b/docs/js_binary.md @@ -92,7 +92,7 @@ The following environment variables are made available to the Node.js runtime ba | log_level | Set the logging level.

Log from are written to stderr. They will be supressed on success when running as the tool of a js_run_binary when silent_on_success is True. In that case, they will be shown only on a build failure along with the stdout & stderr of the node tool being run.

Log levels: fatal, error, warn, info, debug | String | optional | `"error"` | | no_copy_to_bin | List of files to not copy to the Bazel output tree when `copy_data_to_bin` is True.

This is useful for exceptional cases where a `copy_to_bin` is not possible or not suitable for an input file such as a file in an external repository. In most cases, this option is not needed. See `copy_data_to_bin` docstring for more info. | List of labels | optional | `[]` | | node_options | Options to pass to the node invocation on the command line.

https://nodejs.org/api/cli.html

These options are passed directly to the node invocation on the command line. Options passed here will take precendence over options passed via the NODE_OPTIONS environment variable. Options passed here are not added to the NODE_OPTIONS environment variable so will not be automatically picked up by child processes that inherit that enviroment variable. | List of strings | optional | `[]` | -| node_toolchain | The Node.js toolchain to use for this target.

See https://bazel-contrib.github.io/rules_nodejs/Toolchains.html

Typically this is left unset so that Bazel automatically selects the right Node.js toolchain for the target platform. See https://bazel.build/extending/toolchains#toolchain-resolution for more information. | Label | optional | `None` | +| node_toolchain | The Node.js runtime toolchain to use for this target.

See https://bazel-contrib.github.io/rules_nodejs/Toolchains.html

Typically this is left unset so that Bazel automatically selects the right Node.js toolchain for the target platform. See https://bazel.build/extending/toolchains#toolchain-resolution for more information. | Label | optional | `None` | | patch_node_fs | Patch the to Node.js `fs` API (https://nodejs.org/api/fs.html) for this node program to prevent the program from following symlinks out of the execroot, runfiles and the sandbox.

When enabled, `js_binary` patches the Node.js sync and async `fs` API functions `lstat`, `readlink`, `realpath`, `readdir` and `opendir` so that the node program being run cannot resolve symlinks out of the execroot and the runfiles tree. When in the sandbox, these patches prevent the program being run from resolving symlinks out of the sandbox.

When disabled, node programs can leave the execroot, runfiles and sandbox by following symlinks which can lead to non-hermetic behavior. | Boolean | optional | `True` | | preserve_symlinks_main | When True, the --preserve-symlinks-main flag is passed to node.

This prevents node from following an ESM entry script out of runfiles and the sandbox. This can happen for `.mjs` ESM entry points where the fs node patches, which guard the runfiles and sandbox, are not applied. See https://github.com/aspect-build/rules_js/issues/362 for more information. Once #362 is resolved, the default for this attribute can be set to False.

This flag was added in Node.js v10.2.0 (released 2018-05-23). If your node toolchain is configured to use a Node.js version older than this you'll need to set this attribute to False.

See https://nodejs.org/api/cli.html#--preserve-symlinks-main for more information. | Boolean | optional | `True` | @@ -159,7 +159,7 @@ the contract between Bazel and a test runner. | log_level | Set the logging level.

Log from are written to stderr. They will be supressed on success when running as the tool of a js_run_binary when silent_on_success is True. In that case, they will be shown only on a build failure along with the stdout & stderr of the node tool being run.

Log levels: fatal, error, warn, info, debug | String | optional | `"error"` | | no_copy_to_bin | List of files to not copy to the Bazel output tree when `copy_data_to_bin` is True.

This is useful for exceptional cases where a `copy_to_bin` is not possible or not suitable for an input file such as a file in an external repository. In most cases, this option is not needed. See `copy_data_to_bin` docstring for more info. | List of labels | optional | `[]` | | node_options | Options to pass to the node invocation on the command line.

https://nodejs.org/api/cli.html

These options are passed directly to the node invocation on the command line. Options passed here will take precendence over options passed via the NODE_OPTIONS environment variable. Options passed here are not added to the NODE_OPTIONS environment variable so will not be automatically picked up by child processes that inherit that enviroment variable. | List of strings | optional | `[]` | -| node_toolchain | The Node.js toolchain to use for this target.

See https://bazel-contrib.github.io/rules_nodejs/Toolchains.html

Typically this is left unset so that Bazel automatically selects the right Node.js toolchain for the target platform. See https://bazel.build/extending/toolchains#toolchain-resolution for more information. | Label | optional | `None` | +| node_toolchain | The Node.js runtime toolchain to use for this target.

See https://bazel-contrib.github.io/rules_nodejs/Toolchains.html

Typically this is left unset so that Bazel automatically selects the right Node.js toolchain for the target platform. See https://bazel.build/extending/toolchains#toolchain-resolution for more information. | Label | optional | `None` | | patch_node_fs | Patch the to Node.js `fs` API (https://nodejs.org/api/fs.html) for this node program to prevent the program from following symlinks out of the execroot, runfiles and the sandbox.

When enabled, `js_binary` patches the Node.js sync and async `fs` API functions `lstat`, `readlink`, `realpath`, `readdir` and `opendir` so that the node program being run cannot resolve symlinks out of the execroot and the runfiles tree. When in the sandbox, these patches prevent the program being run from resolving symlinks out of the sandbox.

When disabled, node programs can leave the execroot, runfiles and sandbox by following symlinks which can lead to non-hermetic behavior. | Boolean | optional | `True` | | preserve_symlinks_main | When True, the --preserve-symlinks-main flag is passed to node.

This prevents node from following an ESM entry script out of runfiles and the sandbox. This can happen for `.mjs` ESM entry points where the fs node patches, which guard the runfiles and sandbox, are not applied. See https://github.com/aspect-build/rules_js/issues/362 for more information. Once #362 is resolved, the default for this attribute can be set to False.

This flag was added in Node.js v10.2.0 (released 2018-05-23). If your node toolchain is configured to use a Node.js version older than this you'll need to set this attribute to False.

See https://nodejs.org/api/cli.html#--preserve-symlinks-main for more information. | Boolean | optional | `True` | diff --git a/e2e/pnpm_workspace/BUILD.bazel b/e2e/pnpm_workspace/BUILD.bazel index 4f07456d7..f82681433 100644 --- a/e2e/pnpm_workspace/BUILD.bazel +++ b/e2e/pnpm_workspace/BUILD.bazel @@ -19,7 +19,7 @@ sh_test( "@nodejs_linux_amd64//:node_files", "@nodejs_linux_arm64//:node_files", ], - toolchains = ["@nodejs_toolchains//:resolved_toolchain"], + toolchains = ["@rules_nodejs//nodejs:current_node_runtime"], ) build_test( diff --git a/e2e/pnpm_workspace_rerooted/BUILD.bazel b/e2e/pnpm_workspace_rerooted/BUILD.bazel index 4f07456d7..f82681433 100644 --- a/e2e/pnpm_workspace_rerooted/BUILD.bazel +++ b/e2e/pnpm_workspace_rerooted/BUILD.bazel @@ -19,7 +19,7 @@ sh_test( "@nodejs_linux_amd64//:node_files", "@nodejs_linux_arm64//:node_files", ], - toolchains = ["@nodejs_toolchains//:resolved_toolchain"], + toolchains = ["@rules_nodejs//nodejs:current_node_runtime"], ) build_test( diff --git a/e2e/vendored_node/toolchains/BUILD.bazel b/e2e/vendored_node/toolchains/BUILD.bazel index 43bd7bbc2..e048c1fff 100644 --- a/e2e/vendored_node/toolchains/BUILD.bazel +++ b/e2e/vendored_node/toolchains/BUILD.bazel @@ -26,6 +26,23 @@ load("@rules_nodejs//nodejs:toolchain.bzl", "nodejs_toolchain") ] ] +[ + toolchain( + name = "node_vendored_%s_runtime" % os, + target_compatible_with = [ + "@platforms//os:" + os, + "@platforms//cpu:x86_64", + ], + toolchain = ":node_" + os, + toolchain_type = "@rules_nodejs//nodejs:runtime_toolchain_type", + ) + for os in [ + "linux", + "macos", + "windows", + ] +] + nodejs_toolchain( name = "node_linux", node = "@vendored_node_linux_amd64//:bin/node", diff --git a/examples/genrule/BUILD.bazel b/examples/genrule/BUILD.bazel index 24b922a95..6a1c7519f 100644 --- a/examples/genrule/BUILD.bazel +++ b/examples/genrule/BUILD.bazel @@ -43,8 +43,8 @@ genrule( # $@ is bazel shorthand for the path of the output file ">$@", ]), - toolchains = ["@nodejs_toolchains//:resolved_toolchain"], - tools = ["@nodejs_toolchains//:resolved_toolchain"], + toolchains = ["@rules_nodejs//nodejs:current_node_toolchain"], + tools = ["@rules_nodejs//nodejs:current_node_toolchain"], ) diff_test( @@ -74,8 +74,8 @@ genrule( $(NODE_PATH) \\ ./$(execpath :require_acorn_js) \\ $@""", - toolchains = ["@nodejs_toolchains//:resolved_toolchain"], - tools = ["@nodejs_toolchains//:resolved_toolchain"], + toolchains = ["@rules_nodejs//nodejs:current_node_toolchain"], + tools = ["@rules_nodejs//nodejs:current_node_toolchain"], ) diff_test( diff --git a/js/private/js_binary.bzl b/js/private/js_binary.bzl index 70c8395d1..5569c978c 100644 --- a/js/private/js_binary.bzl +++ b/js/private/js_binary.bzl @@ -288,7 +288,7 @@ _ATTRS = { """, ), "node_toolchain": attr.label( - doc = """The Node.js toolchain to use for this target. + doc = """The Node.js runtime toolchain to use for this target. See https://bazel-contrib.github.io/rules_nodejs/Toolchains.html @@ -490,7 +490,7 @@ def _create_launcher(ctx, log_prefix_rule_set, log_prefix_rule, fixed_args = [], if ctx.attr.node_toolchain: nodeinfo = ctx.attr.node_toolchain[platform_common.ToolchainInfo].nodeinfo else: - nodeinfo = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo + nodeinfo = ctx.toolchains["@rules_nodejs//nodejs:runtime_toolchain_type"].nodeinfo directory_path_provider = DirectoryPathInfo if DirectoryPathInfo in ctx.attr.entry_point else _LegacyDirectoryPathInfo if directory_path_provider in ctx.attr.entry_point: @@ -618,7 +618,7 @@ js_binary_lib = struct( toolchains = [ # TODO: on Windows this toolchain is never referenced "@bazel_tools//tools/sh:toolchain_type", - "@rules_nodejs//nodejs:toolchain_type", + "@rules_nodejs//nodejs:runtime_toolchain_type", ] + COPY_FILE_TO_BIN_TOOLCHAINS, ) diff --git a/js/private/js_image_layer.bzl b/js/private/js_image_layer.bzl index dcd8d6d3c..6e3d8a74c 100644 --- a/js/private/js_image_layer.bzl +++ b/js/private/js_image_layer.bzl @@ -408,7 +408,7 @@ else { transitive = [files], ) - nodeinfo = ctx.attr._current_node[platform_common.ToolchainInfo].nodeinfo + nodeinfo = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo node_exec = nodeinfo.node ctx.actions.run( inputs = inputs, @@ -418,6 +418,7 @@ else { executable = node_exec, progress_message = "Computing Layer Groups %{label}", mnemonic = "JsImageLayerGroups", + toolchain = "@rules_nodejs//nodejs:toolchain_type", ) return expected_layer_groups @@ -623,10 +624,6 @@ js_image_layer_lib = struct( default = "//js/private:js_image_layer.mjs", allow_single_file = True, ), - "_current_node": attr.label( - default = "@nodejs_toolchains//:resolved_toolchain", - cfg = "exec", - ), "binary": attr.label( mandatory = True, cfg = _js_image_layer_transition, diff --git a/npm/private/test/BUILD.bazel b/npm/private/test/BUILD.bazel index 880525786..4502445f3 100644 --- a/npm/private/test/BUILD.bazel +++ b/npm/private/test/BUILD.bazel @@ -85,5 +85,5 @@ sh_test( "@nodejs_linux_amd64//:node_files", "@nodejs_linux_arm64//:node_files", ], - toolchains = ["@nodejs_toolchains//:resolved_toolchain"], + toolchains = ["@rules_nodejs//nodejs:current_node_runtime"], )