Skip to content

Commit

Permalink
feat: provide @nodejs repository
Browse files Browse the repository at this point in the history
Generally it's a bad practice that the user calls a repository rule (nodejs_register_toolchains) with a name, and this doesn't result in a repository with that name.

Leave the old _host variant around to make this a non-breaking change.

We did the same fix in python: bazelbuild/rules_python#656

Fixes #3375
  • Loading branch information
alexeagle committed Dec 12, 2022
1 parent 916ae55 commit a5755eb
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
6 changes: 3 additions & 3 deletions docs/dependencies.md
Expand Up @@ -232,13 +232,13 @@ To run the version of node fetched by Bazel which defined in your WORKSPACE you
can use:

```sh
$ bazel run @nodejs_host//:node -- <arguments passed to node>
$ bazel run @nodejs//:node -- <arguments passed to node>
```

For example,

```
$ bazel run @nodejs_host//:node -- --version
$ bazel run @nodejs//:node -- --version
v16.12.0
```

Expand All @@ -248,7 +248,7 @@ To run the Bazel fetched npm and/or yarn you can use:

```sh
$ bazel run @yarn//:yarn -- <arguments passed to yarn>
$ bazel run @nodejs_host//:npm -- <arguments passed to npm>
$ bazel run @nodejs//:npm -- <arguments passed to npm>
```

This will run yarn/npm in the current working directory.
Expand Down
14 changes: 8 additions & 6 deletions docs/repositories.md
Expand Up @@ -4,25 +4,27 @@ rules_nodejs produces several repositories for you to reference.
Bazel represents your workspace as one repository, and code fetched or installed from outside your workspace lives in other repositories.
These are referenced with the `@repo//` syntax in your BUILD files.

## @nodejs_host
## @nodejs

This repository is created by calling the `node_repositories` function in your `WORKSPACE` file.
It contains the node, npm, and npx programs.

As always, `bazel query` is useful for learning about what targets are available.

```sh
$ bazel query @nodejs_host//...
@nodejs_host//:node
$ bazel query @nodejs//...
@nodejs//:node
...
```

You don't typically need to reference the `@nodejs_host` repository from your BUILD files because it's used behind the scenes to run node and fetch dependencies.
You don't typically need to reference the `@nodejs` repository from your BUILD files because it's used behind the scenes to run node and fetch dependencies.

Some ways you can use this:

- Run the Bazel-managed version of node: `bazel run @nodejs_host//:node path/to/program.js`
- Run the Bazel-managed version of npm: `bazel run @nodejs_host//:npm`
- Run the Bazel-managed version of node: `bazel run @nodejs//:node path/to/program.js`
- Run the Bazel-managed version of npm: `bazel run @nodejs//:npm`

(Note: for backward-compatibility, the `@nodejs` repository can also be referenced as `@nodejs_host`).

## @yarn

Expand Down
13 changes: 7 additions & 6 deletions e2e/nodejs_host/BUILD.bazel
Expand Up @@ -7,12 +7,13 @@ nodejs_test(
name = "test_default",
data = [
"index.spec.js",
"@nodejs_host//:node",
"@nodejs_host//:node_bin",
"@nodejs_host//:node_files",
"@nodejs_host//:npm",
"@nodejs_host//:npm_bin",
"@nodejs_host//:npm_files",
"@nodejs//:node",
"@nodejs//:node_bin",
"@nodejs//:node_files",
"@nodejs//:npm",
"@nodejs//:npm_bin",
"@nodejs//:npm_files",
# Use the old name for one of the labels, as a test that it still works
"@nodejs_host//:npx_bin",
"@npm//:node_modules",
"@yarn",
Expand Down
6 changes: 6 additions & 0 deletions nodejs/repositories.bzl
Expand Up @@ -404,6 +404,12 @@ def nodejs_register_toolchains(name, register = True, **kwargs):
"@%s_toolchains//:%s_toolchain" % (name, platform),
)

nodejs_repo_host_os_alias(
name = name,
user_node_repository_name = name,
)

# For backwards compatibility, also provide it under the name with _host suffix.
nodejs_repo_host_os_alias(
name = name + "_host",
user_node_repository_name = name,
Expand Down

0 comments on commit a5755eb

Please sign in to comment.