Skip to content

[Bug]: @nodejs//:node_bin does not resolve to the correct path in repository_rule #3809

Open
@allsey87

Description

@allsey87

What happened?

The label @nodejs//:node_bin does not resolve to the correct path to the Node.js binary when used inside of repository_rule. The path I get is:

external/rules_nodejs~~node~nodejs/node_bin

While the correct path (on my system) should be:

external/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/bin/node

Version

Development (host) and target OS/architectures:

x86_64 GNU/Linux

Output of bazel --version:

bazel 7.2.1

Version of rules_nodejs, or other relevant rules from your WORKSPACE or MODULE.bazel file:

6.3.2

Language(s) and/or frameworks involved:

None

How to reproduce

I have created a template repository that can be opened in Github Codespaces here (just click the "Use this template" and "Open in a codespace" buttons): https://github.com/allsey87/nodejs_bazel_mre

Once the Codespace has been built, run: bazel build //:repository_nodejs and note the incorrect path printed inside of bazel-bin/repository_nodejs.txt.

Alternatively, these three files make up the same MRE of the problem:

MODULE.bazel

bazel_dep(name = "rules_nodejs", version = "6.3.2")

node = use_extension("@rules_nodejs//nodejs:extensions.bzl", "node")
node.toolchain(node_version = "20.14.0")
use_repo(node, "nodejs", "nodejs_toolchains")

test_repository = use_repo_rule("//:repository.bzl", "test_repository")
test_repository(name = "repository_nodejs")

repository.bzl

def _test_repository_impl(ctx):
    ctx.file(".config", "{}".format(ctx.path(ctx.attr._nodejs)))
    ctx.file("BUILD.bazel", """
filegroup(
    name = "config",
    srcs = [".config"],
    visibility = ["//visibility:public"],
)
""")

test_repository = repository_rule(
    _test_repository_impl,
    attrs = {
        "_nodejs": attr.label(
            default = Label("@nodejs//:node_bin"),
            executable = True,
            cfg = "exec"
        )
    }
)

BUILD.bazel

# This rule does not result in nodejs being fetched and gives us an absolute path that ends with:
# external/rules_nodejs~~node~nodejs/node_bin (incorrect)
# The correct path should be:
# external/rules_nodejs~~node~nodejs_linux_amd64/bin/nodejs/bin/node
genrule(
    name = "repository_nodejs",
    outs = ["repository_nodejs.txt"],
    cmd = "cat $(execpath @repository_nodejs//:config) >> $(OUTS)",
    tools = [
        "@repository_nodejs//:config"
    ]
)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions