Skip to content

Commit

Permalink
fix: allow root repositories to override node toolchain version under
Browse files Browse the repository at this point in the history
bzlmod
  • Loading branch information
kormide authored and alexeagle committed Feb 24, 2023
1 parent 663af8d commit ce13837
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 13 additions & 2 deletions nodejs/extensions.bzl
@@ -1,12 +1,19 @@
"extensions for bzlmod"

load(":repositories.bzl", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")
load(":repositories.bzl", "DEFAULT_NODE_REPOSITORY", "DEFAULT_NODE_VERSION", "nodejs_register_toolchains")

def _toolchain_extension(module_ctx):
registrations = {}
for mod in module_ctx.modules:
for toolchain in mod.tags.toolchain:
if toolchain.name != DEFAULT_NODE_REPOSITORY and not mod.is_root:
fail("Only the root module may provide a name for the node toolchain.")

if toolchain.name in registrations.keys():
if toolchain.name == DEFAULT_NODE_REPOSITORY:
# Prioritize the root-most registration of the default node toolchain version and
# ignore any further registrations (modules are processed breadth-first)
continue
if toolchain.node_version == registrations[toolchain.name]:
# No problem to register a matching toolchain twice
continue
Expand All @@ -17,6 +24,7 @@ def _toolchain_extension(module_ctx):
))
else:
registrations[toolchain.name] = toolchain.node_version

for name, node_version in registrations.items():
nodejs_register_toolchains(
name = name,
Expand All @@ -28,7 +36,10 @@ node = module_extension(
implementation = _toolchain_extension,
tag_classes = {
"toolchain": tag_class(attrs = {
"name": attr.string(doc = "Base name for generated repositories"),
"name": attr.string(
doc = "Base name for generated repositories",
default = DEFAULT_NODE_REPOSITORY,
),
"node_version": attr.string(
doc = "Version of the Node.js interpreter",
default = DEFAULT_NODE_VERSION,
Expand Down
4 changes: 4 additions & 0 deletions nodejs/repositories.bzl
Expand Up @@ -8,6 +8,10 @@ load("@bazel_skylib//lib:paths.bzl", "paths")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")

# Default base name for node toolchain repositories
# created by the module extension
DEFAULT_NODE_REPOSITORY = "nodejs"

# Currently v16 is the default.
# We can only change that in a major release of rules_nodejs,
# as it's a semver-breaking change for our users who rely on it.
Expand Down

0 comments on commit ce13837

Please sign in to comment.