From 1bcf00914a00cc9a8bab26fb292f314dc5d21a5e Mon Sep 17 00:00:00 2001 From: Gunnar Wagenknecht Date: Thu, 21 Aug 2025 19:11:46 +0200 Subject: [PATCH 1/3] Introduce runtime_toolchain_type According to discussions in #3854 having two toolchains of the same type for different things is troublesome. It's better to have separate runtime as well as compile toolchains. This commit creates a new runtime_toolchain_type and registers toolchains without execution constraints for this type. Once merged, rules_ts can start consuming the new toolchain type in its js_binary rule to ensure the correct Node for the correct target environment is selected. Fixed [Bug]: Execution toolchain defined without `target_compatible_with` makes it a candidate to selection Fixes #3854 Work towards #3795 --- MODULE.bazel | 18 ++--- docs/Toolchains.md | 16 ++++- e2e/smoke/BUILD.bazel | 18 +++-- e2e/smoke/MODULE.bazel | 4 ++ e2e/smoke/defs.bzl | 4 +- nodejs/BUILD.bazel | 44 +++++++++--- nodejs/node_toolchain_alias.bzl | 84 +++++++++++++++++++++++ nodejs/private/nodejs_toolchains_repo.bzl | 49 +++++-------- nodejs/repositories.bzl | 2 +- nodejs/semantics.bzl | 30 ++++++++ nodejs/toolchain.bzl | 11 ++- 11 files changed, 218 insertions(+), 62 deletions(-) create mode 100644 nodejs/node_toolchain_alias.bzl create mode 100644 nodejs/semantics.bzl diff --git a/MODULE.bazel b/MODULE.bazel index 525f2ee2f0..567f751ba9 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -19,37 +19,37 @@ use_repo(node, "nodejs_toolchains") # Toolchain registration under bzlmod should match the order of WORKSPACE registration # which is the order specified in the PLATFORMS dict https://github.com/bazel-contrib/rules_nodejs/blob/4c373209b058d46f2a5f9ab9f8abf11b161ae459/nodejs/private/nodejs_toolchains_repo.bzl#L20. -# For each platform, `:_toolchain_target` should be registered before `:_toolchain`, +# For each platform, `:_runtime_toolchain` should be registered before `:_toolchain`, # https://github.com/bazel-contrib/rules_nodejs/blob/4c373209b058d46f2a5f9ab9f8abf11b161ae459/nodejs/repositories.bzl#L461/. # See https://github.com/bazelbuild/bazel/issues/19645 and https://github.com/bazel-contrib/rules_nodejs/pull/3750 for more context. -register_toolchains("@nodejs_toolchains//:linux_amd64_toolchain_target") +register_toolchains("@nodejs_toolchains//:linux_amd64_runtime_toolchain") register_toolchains("@nodejs_toolchains//:linux_amd64_toolchain") -register_toolchains("@nodejs_toolchains//:linux_arm64_toolchain_target") +register_toolchains("@nodejs_toolchains//:linux_arm64_runtime_toolchain") register_toolchains("@nodejs_toolchains//:linux_arm64_toolchain") -register_toolchains("@nodejs_toolchains//:linux_s390x_toolchain_target") +register_toolchains("@nodejs_toolchains//:linux_s390x_runtime_toolchain") register_toolchains("@nodejs_toolchains//:linux_s390x_toolchain") -register_toolchains("@nodejs_toolchains//:linux_ppc64le_toolchain_target") +register_toolchains("@nodejs_toolchains//:linux_ppc64le_runtime_toolchain") register_toolchains("@nodejs_toolchains//:linux_ppc64le_toolchain") -register_toolchains("@nodejs_toolchains//:darwin_amd64_toolchain_target") +register_toolchains("@nodejs_toolchains//:darwin_amd64_runtime_toolchain") register_toolchains("@nodejs_toolchains//:darwin_amd64_toolchain") -register_toolchains("@nodejs_toolchains//:darwin_arm64_toolchain_target") +register_toolchains("@nodejs_toolchains//:darwin_arm64_runtime_toolchain") register_toolchains("@nodejs_toolchains//:darwin_arm64_toolchain") -register_toolchains("@nodejs_toolchains//:windows_amd64_toolchain_target") +register_toolchains("@nodejs_toolchains//:windows_amd64_runtime_toolchain") register_toolchains("@nodejs_toolchains//:windows_amd64_toolchain") -register_toolchains("@nodejs_toolchains//:windows_arm64_toolchain_target") +register_toolchains("@nodejs_toolchains//:windows_arm64_runtime_toolchain") register_toolchains("@nodejs_toolchains//:windows_arm64_toolchain") diff --git a/docs/Toolchains.md b/docs/Toolchains.md index 5b94c6a694..69229df901 100644 --- a/docs/Toolchains.md +++ b/docs/Toolchains.md @@ -3,6 +3,16 @@ API docs for [Toolchain](https://docs.bazel.build/versions/main/toolchains.html) support. When you call `nodejs_register_toolchains()` in your `WORKSPACE` file it will setup a node toolchain for executing tools on all currently supported platforms. +In `bzlmod` default toolchains are registered automatically when you depend on `rules_nodejs`. + +There are two toolchain types: + +1) The transpilation toolchain, which provides the Node runtime used to execute the transpiler (and type checker), as well as various helper tools and settings. + (`@rules_nodejs//nodejs:toolchain_type`) +2) The Node runtime that executable Node outputs (e.g., js_binary) will run on. + (`@rules_nodejs//nodejs:runtime_toolchain_type`) + +See [//nodjes/BUILD.bazel](https://github.com/bazel-contrib/rules_nodejs/blob/main/nodejs/BUILD.bazel) for details. If you have an advanced use-case and want to use a version of node not supported by this repository, you can also register your own toolchains. @@ -13,9 +23,9 @@ To run a custom toolchain (i.e., to run a node binary not supported by the built 1) A rule which can build or load a node binary from your repository (a checked-in binary or a build using a relevant [`rules_foreign_cc` build rule](https://bazelbuild.github.io/rules_foreign_cc/) will do nicely). 2) A [`nodejs_toolchain` rule](Core.html#nodejs_toolchain) which depends on your binary defined in step 1 as its `node`. -3) A [`toolchain` rule](https://bazel.build/reference/be/platform#toolchain) that depends on your `nodejs_toolchain` rule defined in step 2 as its `toolchain` - and on `@rules_nodejs//nodejs:toolchain_type` as its `toolchain_type`. Make sure to define appropriate platform restrictions as described in the - documentation for the `toolchain` rule. +3) Two [`toolchain` rules](https://bazel.build/reference/be/platform#toolchain) each depends on your `nodejs_toolchain` rule defined in step 2 as its `toolchain` + and one on `@rules_nodejs//nodejs:toolchain_type` as its `toolchain_type` and the other one `@rules_nodejs//nodejs:runtime_toolchain_type`. + Make sure to define appropriate platform restrictions as described in the documentation for the `toolchain` rule. 4) A call to [the `register_toolchains` function](https://bazel.build/rules/lib/globals#register_toolchains) in your `WORKSPACE` that refers to the `toolchain` rule defined in step 3. diff --git a/e2e/smoke/BUILD.bazel b/e2e/smoke/BUILD.bazel index afbe9664e1..f19879be13 100644 --- a/e2e/smoke/BUILD.bazel +++ b/e2e/smoke/BUILD.bazel @@ -9,6 +9,12 @@ not_windows = select({ "//conditions:default": [], }) +not_silicon = select({ + # There isn't a published arm64 binary for Node 15 + "@platforms//cpu:arm64": ["@platforms//:incompatible"], + "//conditions:default": [], +}) + # Trivial test fixture: a nodejs program that writes to a file write_file( name = "js", @@ -67,7 +73,6 @@ genrule( outs = ["actual1"], cmd = "$(NODE_PATH) $(execpath some.js) $@", toolchains = ["@nodejs_toolchains//:resolved_toolchain"], - tools = ["@nodejs_toolchains//:resolved_toolchain"], ) diff_test( @@ -253,7 +258,9 @@ my_nodejs( # you can also just provide an individual toolchain if you don't want to download them all toolchain = select({ "@bazel_tools//src/conditions:linux_x86_64": "@node17_linux_amd64//:toolchain", - "@bazel_tools//src/conditions:darwin": "@node17_darwin_amd64//:toolchain", + "@bazel_tools//src/conditions:linux_aarch64": "@node17_linux_arm64//:toolchain", + "@bazel_tools//src/conditions:darwin_x86_64": "@node17_darwin_amd64//:toolchain", + "@bazel_tools//src/conditions:darwin_arm64": "@node17_darwin_arm64//:toolchain", "@bazel_tools//src/conditions:windows": "@node17_windows_amd64//:toolchain", }), ) @@ -273,7 +280,9 @@ my_nodejs( # you can also just provide an individual toolchain if you don't want to download them all toolchain = select({ "@bazel_tools//src/conditions:linux_x86_64": "@nodejs_linux_amd64//:toolchain", - "@bazel_tools//src/conditions:darwin": "@nodejs_darwin_amd64//:toolchain", + "@bazel_tools//src/conditions:linux_aarch64": "@nodejs_linux_arm64//:toolchain", + "@bazel_tools//src/conditions:darwin_x86_64": "@nodejs_darwin_amd64//:toolchain", + "@bazel_tools//src/conditions:darwin_arm64": "@nodejs_darwin_arm64//:toolchain", "@bazel_tools//src/conditions:windows": "@nodejs_windows_amd64//:toolchain", }), ) @@ -288,11 +297,12 @@ my_nodejs( name = "run_15", out = "thing_toolchain_15", entry_point = "version.js", + target_compatible_with = not_silicon, # using the select statement will download toolchains for all three platforms # you can also just provide an individual toolchain if you don't want to download them all toolchain = select({ "@bazel_tools//src/conditions:linux_x86_64": "@node15_linux_amd64//:toolchain", - "@bazel_tools//src/conditions:darwin": "@node15_darwin_amd64//:toolchain", + "@bazel_tools//src/conditions:darwin_x86_64": "@node15_darwin_amd64//:toolchain", "@bazel_tools//src/conditions:windows": "@node15_windows_amd64//:toolchain", }), ) diff --git a/e2e/smoke/MODULE.bazel b/e2e/smoke/MODULE.bazel index 33cb85b75c..7221a4468e 100644 --- a/e2e/smoke/MODULE.bazel +++ b/e2e/smoke/MODULE.bazel @@ -30,10 +30,14 @@ use_repo( "node15_linux_amd64", "node15_windows_amd64", "node17_darwin_amd64", + "node17_darwin_arm64", "node17_linux_amd64", + "node17_linux_arm64", "node17_windows_amd64", "nodejs_darwin_amd64", + "nodejs_darwin_arm64", "nodejs_linux_amd64", + "nodejs_linux_arm64", "nodejs_toolchains", "nodejs_windows_amd64", ) diff --git a/e2e/smoke/defs.bzl b/e2e/smoke/defs.bzl index c17f6197b2..0f38f9dcc4 100644 --- a/e2e/smoke/defs.bzl +++ b/e2e/smoke/defs.bzl @@ -4,7 +4,7 @@ def _my_nodejs_impl(ctx): if ctx.attr.toolchain: nodeinfo = ctx.attr.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 ctx.actions.run( inputs = [ctx.file.entry_point], executable = nodeinfo.node, @@ -20,5 +20,5 @@ my_nodejs = rule( "out": attr.output(), "toolchain": attr.label(), }, - toolchains = ["@rules_nodejs//nodejs:toolchain_type"], + toolchains = ["@rules_nodejs//nodejs:runtime_toolchain_type"], ) diff --git a/nodejs/BUILD.bazel b/nodejs/BUILD.bazel index fd1784dfb3..90a0b10da7 100644 --- a/nodejs/BUILD.bazel +++ b/nodejs/BUILD.bazel @@ -2,6 +2,7 @@ load("@bazel_lib//:bzl_library.bzl", "bzl_library") load("//nodejs/private:nodejs_toolchains_repo.bzl", "PLATFORMS") load("//nodejs/private:user_build_settings.bzl", "user_args") +load(":node_toolchain_alias.bzl", "node_host_runtime_alias", "node_runtime_alias", "node_toolchain_alias") package(default_visibility = ["//visibility:public"]) @@ -25,23 +26,44 @@ bzl_library( ], ) -bzl_library( - name = "toolchain", - srcs = ["toolchain.bzl"], -) - -bzl_library( - name = "extensions", - srcs = ["extensions.bzl"], - deps = ["repositories"], -) - # This is the target rule authors should put in their "toolchains" # attribute in order to get a node interpreter for the correct # platform. # See https://docs.bazel.build/versions/main/toolchains.html#writing-rules-that-use-toolchains +# A single binary distribution of a Node provides two different types of toolchains from the +# perspective of Bazel: + +# (1) The transpilation toolchain, which provides the Node runtime used to execute the transpiler +# (and type checker), as well as various helper tools and settings. +# +# Toolchains of this type typically have constraints on the execution platform so that their Node +# runtime can run the transpiler, but not on the target platform as Node transpilation outputs are +# platform independent. +# +# Obtain the associated NodeInfo via: +# ctx.toolchains["@bazel_tools//tools/jdk:toolchain_type"].nodeinfo toolchain_type(name = "toolchain_type") +# (2) The Node runtime that executable Node outputs (e.g., js_binary) will run on. +# +# Toolchains of this type typically have constraints on the target platform so that the runtime's +# native 'node' binary can be run there, but not on the execution platform as building an executable +# Node target only requires copying or symlinking the runtime, which can be done on any platform. +# +# Obtain the associated NodeRuntimeInfo via: +# ctx.toolchains["@bazel_tools//tools/jdk:runtime_toolchain_type"].nodeinfo +toolchain_type(name = "runtime_toolchain_type") + +# Points to toolchain[":runtime_toolchain_type"] +node_runtime_alias(name = "current_node_runtime") + +# Host configuration of ":current_node_runtime" +node_host_runtime_alias(name = "current_host_node_runtime") + +# Points to toolchain[":toolchain_type"] +node_toolchain_alias(name = "current_node_toolchain") + +# The platforms that are supported by the Node toolchains. [ platform( name = key, diff --git a/nodejs/node_toolchain_alias.bzl b/nodejs/node_toolchain_alias.bzl new file mode 100644 index 0000000000..7e7bcc94b1 --- /dev/null +++ b/nodejs/node_toolchain_alias.bzl @@ -0,0 +1,84 @@ +# Copyright 2019 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +"""Node toolchain aliases using toolchain resolution.""" + +load(":semantics.bzl", "semantics") +load(":toolchain.bzl", "NodeInfo") + +def _node_runtime_alias(ctx): + """Implementation of node_runtime_alias using toolchain resolution.""" + toolchain_info = ctx.toolchains[semantics.NODE_RUNTIME_TOOLCHAIN_TYPE] + toolchain = toolchain_info.nodeinfo + template_variable_info = toolchain_info.template_variables + default_info = toolchain_info.default + return [ + toolchain_info, + toolchain, + template_variable_info, + default_info, + ] + +node_runtime_alias = rule( + implementation = _node_runtime_alias, + toolchains = [semantics.NODE_RUNTIME_TOOLCHAIN], +) + +def _node_host_runtime_alias(ctx): + """Implementation of node_host_runtime_alias using toolchain resolution.""" + runtime = ctx.attr._runtime + toolchain = runtime[NodeInfo] + template_variable_info = runtime[platform_common.TemplateVariableInfo] + default_info = runtime[DefaultInfo] + toolchain_info = platform_common.ToolchainInfo(nodeinfo = toolchain) + return [ + toolchain, + template_variable_info, + toolchain_info, + default_info, + ] + +node_host_runtime_alias = rule( + implementation = _node_host_runtime_alias, + attrs = { + "_runtime": attr.label( + default = Label("//nodejs:current_node_runtime"), + providers = [ + NodeInfo, + platform_common.TemplateVariableInfo, + ], + cfg = "exec", + ), + }, + provides = [ + NodeInfo, + platform_common.TemplateVariableInfo, + platform_common.ToolchainInfo, + ], +) + +def _node_toolchain_alias(ctx): + """An implementation of node_toolchain_alias using toolchain resolution.""" + toolchain_info = ctx.toolchains[semantics.NODE_TOOLCHAIN_TYPE] + toolchain = toolchain_info.nodeinfo + + return [ + toolchain_info, + toolchain, + ] + +node_toolchain_alias = rule( + implementation = _node_toolchain_alias, + toolchains = [semantics.NODE_TOOLCHAIN], +) diff --git a/nodejs/private/nodejs_toolchains_repo.bzl b/nodejs/private/nodejs_toolchains_repo.bzl index 7758f3edb4..4e546fdec1 100644 --- a/nodejs/private/nodejs_toolchains_repo.bzl +++ b/nodejs/private/nodejs_toolchains_repo.bzl @@ -69,55 +69,42 @@ PLATFORMS = { } def _nodejs_toolchains_repo_impl(repository_ctx): - # Expose a concrete toolchain which is the result of Bazel resolving the toolchain - # for the execution or target platform. - # Workaround for https://github.com/bazelbuild/bazel/issues/14009 - starlark_content = """# Generated by nodejs_toolchains_repo.bzl - -# Forward all the providers -def _resolved_toolchain_impl(ctx): - toolchain_info = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"] - return [ - toolchain_info, - toolchain_info.default, - toolchain_info.nodeinfo, - toolchain_info.template_variables, - ] - -# Copied from java_toolchain_alias -# https://cs.opensource.google/bazel/bazel/+/master:tools/jdk/java_toolchain_alias.bzl -resolved_toolchain = rule( - implementation = _resolved_toolchain_impl, - toolchains = ["@rules_nodejs//nodejs:toolchain_type"], -) -""" - repository_ctx.file("defs.bzl", starlark_content) - - build_content = """# Generated by nodejs_toolchains_repo.bzl + # TODO(7.0) Drop support for deprecated alias + build_content = '''# Generated by nodejs_toolchains_repo.bzl # # These can be registered in the workspace file or passed to --extra_toolchains flag. # By default all these toolchains are registered by the nodejs_register_toolchains macro # so you don't normally need to interact with these targets. -load(":defs.bzl", "resolved_toolchain") - -resolved_toolchain(name = "resolved_toolchain", visibility = ["//visibility:public"]) +alias( + name = "resolved_toolchain", + actual = "@rules_nodejs//nodejs:current_node_runtime", + deprecation = """ +Use one of the following instead: +- @rules_nodejs//nodejs:current_node_runtime +- @rules_nodejs//nodejs:current_host_node_runtime +- @rules_nodejs//nodejs:current_node_toolchain +See https://github.com/bazel-contrib/rules_nodejs/issues/3795. +""", + visibility = ["//visibility:public"], +) -""" +''' for [platform, meta] in PLATFORMS.items(): build_content += """ toolchain( name = "{platform}_toolchain", exec_compatible_with = {compatible_with}, + target_compatible_with = {compatible_with}, # https://github.com/bazel-contrib/rules_nodejs/issues/3854 toolchain = "@{user_node_repository_name}_{platform}//:toolchain", toolchain_type = "@rules_nodejs//nodejs:toolchain_type", ) toolchain( - name = "{platform}_toolchain_target", + name = "{platform}_runtime_toolchain", target_compatible_with = {compatible_with}, toolchain = "@{user_node_repository_name}_{platform}//:toolchain", - toolchain_type = "@rules_nodejs//nodejs:toolchain_type", + toolchain_type = "@rules_nodejs//nodejs:runtime_toolchain_type", ) """.format( platform = platform, diff --git a/nodejs/repositories.bzl b/nodejs/repositories.bzl index 7d0cfbb76e..ac927a6a78 100644 --- a/nodejs/repositories.bzl +++ b/nodejs/repositories.bzl @@ -476,7 +476,7 @@ def nodejs_register_toolchains(name = DEFAULT_NODE_REPOSITORY, register = True, ) if register: native.register_toolchains( - "@%s_toolchains//:%s_toolchain_target" % (name, platform), + "@%s_toolchains//:%s_runtime_toolchain" % (name, platform), "@%s_toolchains//:%s_toolchain" % (name, platform), ) diff --git a/nodejs/semantics.bzl b/nodejs/semantics.bzl new file mode 100644 index 0000000000..39a451f3f6 --- /dev/null +++ b/nodejs/semantics.bzl @@ -0,0 +1,30 @@ +# Copyright 2021 The Bazel Authors. All rights reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""Rules NodeJS Semantics""" + +def _find_node_toolchain(ctx): + return ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo + +def _find_node_runtime_toolchain(ctx): + return ctx.toolchains["@rules_nodejs//nodejs:runtime_toolchain_type"].nodeinfo + +semantics = struct( + NODE_TOOLCHAIN_LABEL = "@rules_nodejs//nodejs:current_node_toolchain", + NODE_TOOLCHAIN_TYPE = "@rules_nodejs//nodejs:toolchain_type", + NODE_TOOLCHAIN = config_common.toolchain_type("@rules_nodejs//nodejs:toolchain_type", mandatory = True), + find_node_toolchain = _find_node_toolchain, + NODE_RUNTIME_TOOLCHAIN_TYPE = "@rules_nodejs//nodejs:runtime_toolchain_type", + NODE_RUNTIME_TOOLCHAIN = config_common.toolchain_type("@rules_nodejs//nodejs:runtime_toolchain_type", mandatory = True), + find_node_runtime_toolchain = _find_node_runtime_toolchain, +) diff --git a/nodejs/toolchain.bzl b/nodejs/toolchain.bzl index 804f8a3c95..9ebe8bda6c 100644 --- a/nodejs/toolchain.bzl +++ b/nodejs/toolchain.bzl @@ -162,7 +162,7 @@ def nodejs_toolchain( ``` Next, declare which execution platforms or target platforms the toolchain should be selected for - based on constraints. + based on constraints. A separate toolchain type is used for runtime target platform selection. ```starlark toolchain( @@ -174,6 +174,15 @@ def nodejs_toolchain( toolchain = ":toolchain", toolchain_type = "@rules_nodejs//nodejs:toolchain_type", ) + toolchain( + name = "my_nodejs_runtime", + target_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", + ], + toolchain = ":toolchain", + toolchain_type = "@rules_nodejs//nodejs:runtime_toolchain_type", + ) ``` See https://bazel.build/extending/toolchains#toolchain-resolution for more information on toolchain From afa293ab2987cba18f8e777b5cf506d4afc4bd4c Mon Sep 17 00:00:00 2001 From: Gunnar Wagenknecht Date: Sat, 23 Aug 2025 08:08:38 +0200 Subject: [PATCH 2/3] Fix and run //docs:update --- MODULE.bazel | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MODULE.bazel b/MODULE.bazel index 567f751ba9..37314a2e4e 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -10,6 +10,9 @@ bazel_dep(name = "bazel_lib", version = "3.0.0-beta.1") bazel_dep(name = "bazel_skylib", version = "1.4.1") bazel_dep(name = "platforms", version = "0.0.5") +# workaround for https://github.com/bazelbuild/bazel/issues/25124 +bazel_dep(name = "zlib", version = "1.3.1.bcr.6", dev_dependency = True) + node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node") # Note, this gets the default version of Node.js from From 72f5f23364de29112a700ac99404ba1b3eef0560 Mon Sep 17 00:00:00 2001 From: Gunnar Wagenknecht Date: Sun, 24 Aug 2025 08:27:18 +0200 Subject: [PATCH 3/3] Address PR feedback, drop host runtime --- e2e/smoke/defs.bzl | 4 +-- nodejs/BUILD.bazel | 7 ++-- nodejs/node_toolchain_alias.bzl | 39 +++-------------------- nodejs/private/nodejs_toolchains_repo.bzl | 2 +- nodejs/semantics.bzl | 17 ++++++---- 5 files changed, 20 insertions(+), 49 deletions(-) diff --git a/e2e/smoke/defs.bzl b/e2e/smoke/defs.bzl index 0f38f9dcc4..c17f6197b2 100644 --- a/e2e/smoke/defs.bzl +++ b/e2e/smoke/defs.bzl @@ -4,7 +4,7 @@ def _my_nodejs_impl(ctx): if ctx.attr.toolchain: nodeinfo = ctx.attr.toolchain[platform_common.ToolchainInfo].nodeinfo else: - nodeinfo = ctx.toolchains["@rules_nodejs//nodejs:runtime_toolchain_type"].nodeinfo + nodeinfo = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo ctx.actions.run( inputs = [ctx.file.entry_point], executable = nodeinfo.node, @@ -20,5 +20,5 @@ my_nodejs = rule( "out": attr.output(), "toolchain": attr.label(), }, - toolchains = ["@rules_nodejs//nodejs:runtime_toolchain_type"], + toolchains = ["@rules_nodejs//nodejs:toolchain_type"], ) diff --git a/nodejs/BUILD.bazel b/nodejs/BUILD.bazel index 90a0b10da7..e6cae44281 100644 --- a/nodejs/BUILD.bazel +++ b/nodejs/BUILD.bazel @@ -2,7 +2,7 @@ load("@bazel_lib//:bzl_library.bzl", "bzl_library") load("//nodejs/private:nodejs_toolchains_repo.bzl", "PLATFORMS") load("//nodejs/private:user_build_settings.bzl", "user_args") -load(":node_toolchain_alias.bzl", "node_host_runtime_alias", "node_runtime_alias", "node_toolchain_alias") +load(":node_toolchain_alias.bzl", "node_runtime_alias", "node_toolchain_alias") package(default_visibility = ["//visibility:public"]) @@ -55,12 +55,11 @@ toolchain_type(name = "toolchain_type") toolchain_type(name = "runtime_toolchain_type") # Points to toolchain[":runtime_toolchain_type"] +# Use this for executing and packaging Node applications for target platform (eg., js_binary, js_test or js_image_oci). node_runtime_alias(name = "current_node_runtime") -# Host configuration of ":current_node_runtime" -node_host_runtime_alias(name = "current_host_node_runtime") - # Points to toolchain[":toolchain_type"] +# Use this for tools (eg., when action execution is needed). node_toolchain_alias(name = "current_node_toolchain") # The platforms that are supported by the Node toolchains. diff --git a/nodejs/node_toolchain_alias.bzl b/nodejs/node_toolchain_alias.bzl index 7e7bcc94b1..a4170a8960 100644 --- a/nodejs/node_toolchain_alias.bzl +++ b/nodejs/node_toolchain_alias.bzl @@ -15,7 +15,6 @@ """Node toolchain aliases using toolchain resolution.""" load(":semantics.bzl", "semantics") -load(":toolchain.bzl", "NodeInfo") def _node_runtime_alias(ctx): """Implementation of node_runtime_alias using toolchain resolution.""" @@ -35,47 +34,17 @@ node_runtime_alias = rule( toolchains = [semantics.NODE_RUNTIME_TOOLCHAIN], ) -def _node_host_runtime_alias(ctx): - """Implementation of node_host_runtime_alias using toolchain resolution.""" - runtime = ctx.attr._runtime - toolchain = runtime[NodeInfo] - template_variable_info = runtime[platform_common.TemplateVariableInfo] - default_info = runtime[DefaultInfo] - toolchain_info = platform_common.ToolchainInfo(nodeinfo = toolchain) - return [ - toolchain, - template_variable_info, - toolchain_info, - default_info, - ] - -node_host_runtime_alias = rule( - implementation = _node_host_runtime_alias, - attrs = { - "_runtime": attr.label( - default = Label("//nodejs:current_node_runtime"), - providers = [ - NodeInfo, - platform_common.TemplateVariableInfo, - ], - cfg = "exec", - ), - }, - provides = [ - NodeInfo, - platform_common.TemplateVariableInfo, - platform_common.ToolchainInfo, - ], -) - def _node_toolchain_alias(ctx): """An implementation of node_toolchain_alias using toolchain resolution.""" toolchain_info = ctx.toolchains[semantics.NODE_TOOLCHAIN_TYPE] toolchain = toolchain_info.nodeinfo - + template_variable_info = toolchain_info.template_variables + default_info = toolchain_info.default return [ toolchain_info, toolchain, + template_variable_info, + default_info, ] node_toolchain_alias = rule( diff --git a/nodejs/private/nodejs_toolchains_repo.bzl b/nodejs/private/nodejs_toolchains_repo.bzl index 4e546fdec1..1550cab767 100644 --- a/nodejs/private/nodejs_toolchains_repo.bzl +++ b/nodejs/private/nodejs_toolchains_repo.bzl @@ -96,7 +96,7 @@ See https://github.com/bazel-contrib/rules_nodejs/issues/3795. toolchain( name = "{platform}_toolchain", exec_compatible_with = {compatible_with}, - target_compatible_with = {compatible_with}, # https://github.com/bazel-contrib/rules_nodejs/issues/3854 + target_compatible_with = {compatible_with}, # prevent Node from this toolchain being bundled by js_image_oci to incompatible target platforms (https://github.com/bazel-contrib/rules_nodejs/issues/3854) toolchain = "@{user_node_repository_name}_{platform}//:toolchain", toolchain_type = "@rules_nodejs//nodejs:toolchain_type", ) diff --git a/nodejs/semantics.bzl b/nodejs/semantics.bzl index 39a451f3f6..830f84c64b 100644 --- a/nodejs/semantics.bzl +++ b/nodejs/semantics.bzl @@ -13,18 +13,21 @@ # limitations under the License. """Rules NodeJS Semantics""" +_NODE_TOOLCHAIN_TYPE = Label("//nodejs:toolchain_type") +_NODE_RUNTIME_TOOLCHAIN_TYPE = Label("//nodejs:runtime_toolchain_type") + def _find_node_toolchain(ctx): - return ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo + return ctx.toolchains[_NODE_TOOLCHAIN_TYPE].nodeinfo def _find_node_runtime_toolchain(ctx): - return ctx.toolchains["@rules_nodejs//nodejs:runtime_toolchain_type"].nodeinfo + return ctx.toolchains[_NODE_RUNTIME_TOOLCHAIN_TYPE].nodeinfo semantics = struct( - NODE_TOOLCHAIN_LABEL = "@rules_nodejs//nodejs:current_node_toolchain", - NODE_TOOLCHAIN_TYPE = "@rules_nodejs//nodejs:toolchain_type", - NODE_TOOLCHAIN = config_common.toolchain_type("@rules_nodejs//nodejs:toolchain_type", mandatory = True), + NODE_TOOLCHAIN_LABEL = Label("//nodejs:current_node_toolchain"), + NODE_TOOLCHAIN_TYPE = _NODE_TOOLCHAIN_TYPE, + NODE_TOOLCHAIN = config_common.toolchain_type(_NODE_TOOLCHAIN_TYPE, mandatory = True), find_node_toolchain = _find_node_toolchain, - NODE_RUNTIME_TOOLCHAIN_TYPE = "@rules_nodejs//nodejs:runtime_toolchain_type", - NODE_RUNTIME_TOOLCHAIN = config_common.toolchain_type("@rules_nodejs//nodejs:runtime_toolchain_type", mandatory = True), + NODE_RUNTIME_TOOLCHAIN_TYPE = _NODE_RUNTIME_TOOLCHAIN_TYPE, + NODE_RUNTIME_TOOLCHAIN = config_common.toolchain_type(_NODE_RUNTIME_TOOLCHAIN_TYPE, mandatory = True), find_node_runtime_toolchain = _find_node_runtime_toolchain, )