From a5755eb458c2dd8e0e2cf9b92d8304d9e77ea117 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 12 Dec 2022 13:58:44 -0800 Subject: [PATCH] feat: provide @nodejs repository 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: https://github.com/bazelbuild/rules_python/pull/656 Fixes #3375 --- docs/dependencies.md | 6 +++--- docs/repositories.md | 14 ++++++++------ e2e/nodejs_host/BUILD.bazel | 13 +++++++------ nodejs/repositories.bzl | 6 ++++++ 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/docs/dependencies.md b/docs/dependencies.md index fa4f88c10e..34f7a75e15 100644 --- a/docs/dependencies.md +++ b/docs/dependencies.md @@ -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 -- +$ bazel run @nodejs//:node -- ``` For example, ``` -$ bazel run @nodejs_host//:node -- --version +$ bazel run @nodejs//:node -- --version v16.12.0 ``` @@ -248,7 +248,7 @@ To run the Bazel fetched npm and/or yarn you can use: ```sh $ bazel run @yarn//:yarn -- -$ bazel run @nodejs_host//:npm -- +$ bazel run @nodejs//:npm -- ``` This will run yarn/npm in the current working directory. diff --git a/docs/repositories.md b/docs/repositories.md index 8df63061d1..dc524ad6f6 100644 --- a/docs/repositories.md +++ b/docs/repositories.md @@ -4,7 +4,7 @@ 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. @@ -12,17 +12,19 @@ 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 diff --git a/e2e/nodejs_host/BUILD.bazel b/e2e/nodejs_host/BUILD.bazel index 94d272dbc6..c1c7cb2f1e 100644 --- a/e2e/nodejs_host/BUILD.bazel +++ b/e2e/nodejs_host/BUILD.bazel @@ -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", diff --git a/nodejs/repositories.bzl b/nodejs/repositories.bzl index 86940a9d6d..39a3edf0ae 100644 --- a/nodejs/repositories.bzl +++ b/nodejs/repositories.bzl @@ -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,