bazel-contrib / rules_nodejs

NodeJS toolchain for Bazel.
https://bazelbuild.github.io/rules_nodejs/
Apache License 2.0
724 stars 523 forks source link

fix: register Node.js toolchains in correct order #3750

Closed gregmagolan closed 3 months ago

gregmagolan commented 3 months ago

Fixes https://github.com/aspect-build/rules_js/issues/1530.

Related to https://github.com/bazelbuild/bazel/issues/19645.

Toolchain registration ordering matters since it affects which toolchain Bazel resolves. This PR makes the bzlmod registration match the registration order in WORKSPACE.

In particular, the :<PLATFORM>_toolchain_target (which defines target_compatible_with) should be registered before :<PLATFORM>_toolchain (which defines exec_compatible_with) for each platform. In WORKSPACE this is done here: https://github.com/bazelbuild/rules_nodejs/blob/4c373209b058d46f2a5f9ab9f8abf11b161ae459/nodejs/repositories.bzl#L461/. This is important so that Bazel selects the target compatible toolchain before it selects the execution compatible toolchain. The opposite ordering causes the issue seen in https://github.com/aspect-build/rules_js/issues/1530.

toolchain(
  name = "linux_amd64_toolchain_target",
  toolchain_type = "//nodejs:toolchain_type",
  target_compatible_with = ["@platforms//os:linux", "@platforms//cpu:x86_64"],
  toolchain = "@@_main~node~nodejs_linux_amd64//:toolchain",
)
toolchain(
  name = "linux_amd64_toolchain",
  toolchain_type = "//nodejs:toolchain_type",
  exec_compatible_with = ["@platforms//os:linux", "@platforms//cpu:x86_64"],
  toolchain = "@@_main~node~nodejs_linux_amd64//:toolchain",
)