feat(builtin): npm_install/yarn_install node repo
Repository rules need to know which nodejs repository to get tools from
because they run before toolchain resolution.
Removes another hardcoded usage of "@nodejs"
diff --git a/docs/Built-ins.md b/docs/Built-ins.md
index 0f7a32f..f78db9c 100755
--- a/docs/Built-ins.md
+++ b/docs/Built-ins.md
@@ -746,9 +746,10 @@
<pre>
npm_install(<a href="#npm_install-name">name</a>, <a href="#npm_install-args">args</a>, <a href="#npm_install-data">data</a>, <a href="#npm_install-environment">environment</a>, <a href="#npm_install-exports_directories_only">exports_directories_only</a>,
<a href="#npm_install-generate_local_modules_build_files">generate_local_modules_build_files</a>, <a href="#npm_install-included_files">included_files</a>, <a href="#npm_install-links">links</a>, <a href="#npm_install-manual_build_file_contents">manual_build_file_contents</a>,
- <a href="#npm_install-npm_command">npm_command</a>, <a href="#npm_install-package_json">package_json</a>, <a href="#npm_install-package_json_remove">package_json_remove</a>, <a href="#npm_install-package_json_replace">package_json_replace</a>, <a href="#npm_install-package_lock_json">package_lock_json</a>,
- <a href="#npm_install-package_path">package_path</a>, <a href="#npm_install-patch_args">patch_args</a>, <a href="#npm_install-patch_tool">patch_tool</a>, <a href="#npm_install-post_install_patches">post_install_patches</a>, <a href="#npm_install-pre_install_patches">pre_install_patches</a>, <a href="#npm_install-quiet">quiet</a>,
- <a href="#npm_install-repo_mapping">repo_mapping</a>, <a href="#npm_install-strict_visibility">strict_visibility</a>, <a href="#npm_install-symlink_node_modules">symlink_node_modules</a>, <a href="#npm_install-timeout">timeout</a>)
+ <a href="#npm_install-node_repository">node_repository</a>, <a href="#npm_install-npm_command">npm_command</a>, <a href="#npm_install-package_json">package_json</a>, <a href="#npm_install-package_json_remove">package_json_remove</a>, <a href="#npm_install-package_json_replace">package_json_replace</a>,
+ <a href="#npm_install-package_lock_json">package_lock_json</a>, <a href="#npm_install-package_path">package_path</a>, <a href="#npm_install-patch_args">patch_args</a>, <a href="#npm_install-patch_tool">patch_tool</a>, <a href="#npm_install-post_install_patches">post_install_patches</a>,
+ <a href="#npm_install-pre_install_patches">pre_install_patches</a>, <a href="#npm_install-quiet">quiet</a>, <a href="#npm_install-repo_mapping">repo_mapping</a>, <a href="#npm_install-strict_visibility">strict_visibility</a>, <a href="#npm_install-symlink_node_modules">symlink_node_modules</a>,
+ <a href="#npm_install-timeout">timeout</a>)
</pre>
Runs npm install during workspace setup.
@@ -976,6 +977,13 @@
Defaults to `""`
+<h4 id="npm_install-node_repository">node_repository</h4>
+
+(*String*): The basename for nodejs toolchains.
+ Usually this is the value of the `name` attribute given to a nodejs_register_toolchains call in WORKSPACE
+
+Defaults to `"nodejs"`
+
<h4 id="npm_install-npm_command">npm_command</h4>
(*String*): The npm command to run, to install dependencies.
@@ -1413,8 +1421,8 @@
<pre>
yarn_install(<a href="#yarn_install-name">name</a>, <a href="#yarn_install-args">args</a>, <a href="#yarn_install-data">data</a>, <a href="#yarn_install-environment">environment</a>, <a href="#yarn_install-exports_directories_only">exports_directories_only</a>, <a href="#yarn_install-frozen_lockfile">frozen_lockfile</a>,
<a href="#yarn_install-generate_local_modules_build_files">generate_local_modules_build_files</a>, <a href="#yarn_install-included_files">included_files</a>, <a href="#yarn_install-links">links</a>, <a href="#yarn_install-manual_build_file_contents">manual_build_file_contents</a>,
- <a href="#yarn_install-package_json">package_json</a>, <a href="#yarn_install-package_json_remove">package_json_remove</a>, <a href="#yarn_install-package_json_replace">package_json_replace</a>, <a href="#yarn_install-package_path">package_path</a>, <a href="#yarn_install-patch_args">patch_args</a>,
- <a href="#yarn_install-patch_tool">patch_tool</a>, <a href="#yarn_install-post_install_patches">post_install_patches</a>, <a href="#yarn_install-pre_install_patches">pre_install_patches</a>, <a href="#yarn_install-quiet">quiet</a>, <a href="#yarn_install-repo_mapping">repo_mapping</a>,
+ <a href="#yarn_install-node_repository">node_repository</a>, <a href="#yarn_install-package_json">package_json</a>, <a href="#yarn_install-package_json_remove">package_json_remove</a>, <a href="#yarn_install-package_json_replace">package_json_replace</a>, <a href="#yarn_install-package_path">package_path</a>,
+ <a href="#yarn_install-patch_args">patch_args</a>, <a href="#yarn_install-patch_tool">patch_tool</a>, <a href="#yarn_install-post_install_patches">post_install_patches</a>, <a href="#yarn_install-pre_install_patches">pre_install_patches</a>, <a href="#yarn_install-quiet">quiet</a>, <a href="#yarn_install-repo_mapping">repo_mapping</a>,
<a href="#yarn_install-strict_visibility">strict_visibility</a>, <a href="#yarn_install-symlink_node_modules">symlink_node_modules</a>, <a href="#yarn_install-timeout">timeout</a>, <a href="#yarn_install-use_global_yarn_cache">use_global_yarn_cache</a>, <a href="#yarn_install-yarn_lock">yarn_lock</a>)
</pre>
@@ -1658,6 +1666,13 @@
Defaults to `""`
+<h4 id="yarn_install-node_repository">node_repository</h4>
+
+(*String*): The basename for nodejs toolchains.
+ Usually this is the value of the `name` attribute given to a nodejs_register_toolchains call in WORKSPACE
+
+Defaults to `"nodejs"`
+
<h4 id="yarn_install-package_json">package_json</h4>
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>, mandatory*)
diff --git a/internal/node/node_labels.bzl b/internal/node/node_labels.bzl
index 393661c..8c67761 100644
--- a/internal/node/node_labels.bzl
+++ b/internal/node/node_labels.bzl
@@ -19,22 +19,18 @@
load("//nodejs/private:os_name.bzl", "is_windows_os", "os_name")
+def _get_label(rctx, tool):
+ ext = ".cmd" if is_windows_os(rctx) else ""
+ return Label("@{}_{}//:{}{}".format(rctx.attr.node_repository, os_name(rctx), tool, ext))
+
def get_node_label(rctx):
- if is_windows_os(rctx):
- return Label("@nodejs_%s//:bin/node.cmd" % os_name(rctx))
- return Label("@nodejs_%s//:bin/node" % os_name(rctx))
+ return _get_label(rctx, "bin/node")
def get_npm_label(rctx):
- if is_windows_os(rctx):
- return Label("@nodejs_%s//:bin/npm.cmd" % os_name(rctx))
- return Label("@nodejs_%s//:bin/npm" % os_name(rctx))
+ return _get_label(rctx, "bin/npm")
def get_npm_node_repositories_label(rctx):
- if is_windows_os(rctx):
- return Label("@nodejs_%s//:bin/npm_node_repositories.cmd" % os_name(rctx))
- return Label("@nodejs_%s//:bin/npm_node_repositories" % os_name(rctx))
+ return _get_label(rctx, "bin/npm_node_repositories")
def get_yarn_label(rctx):
- if is_windows_os(rctx):
- return Label("@nodejs_%s//:bin/yarn.cmd" % os_name(rctx))
- return Label("@nodejs_%s//:bin/yarn" % os_name(rctx))
+ return _get_label(rctx, "bin/yarn")
diff --git a/internal/npm_install/npm_install.bzl b/internal/npm_install/npm_install.bzl
index adfcef1..03c6e8a 100644
--- a/internal/npm_install/npm_install.bzl
+++ b/internal/npm_install/npm_install.bzl
@@ -219,6 +219,11 @@
fine grained npm dependencies.
""",
),
+ "node_repository": attr.string(
+ default = "nodejs",
+ doc = """The basename for nodejs toolchains.
+ Usually this is the value of the `name` attribute given to a nodejs_register_toolchains call in WORKSPACE""",
+ ),
"package_json": attr.label(
mandatory = True,
allow_single_file = True,
@@ -627,11 +632,11 @@
# Add a dep to the node_info & yarn_info files from node_repositories
# so that if the node or yarn versions change we re-run the repository rule
repository_ctx.symlink(
- Label("@nodejs_%s//:node_info" % os_name(repository_ctx)),
+ Label("@{}_{}//:node_info".format(repository_ctx.attr.node_repository, os_name(repository_ctx))),
repository_ctx.path("_node_info"),
)
repository_ctx.symlink(
- Label("@nodejs_%s//:yarn_info" % os_name(repository_ctx)),
+ Label("@{}_{}//:yarn_info".format(repository_ctx.attr.node_repository, os_name(repository_ctx))),
repository_ctx.path("_yarn_info"),
)
@@ -667,7 +672,6 @@
def _npm_install_impl(repository_ctx):
"""Core implementation of npm_install."""
-
_check_min_bazel_version("npm_install", repository_ctx)
is_windows_host = is_windows_os(repository_ctx)
@@ -807,7 +811,6 @@
def _yarn_install_impl(repository_ctx):
"""Core implementation of yarn_install."""
-
_check_min_bazel_version("yarn_install", repository_ctx)
is_windows_host = is_windows_os(repository_ctx)
diff --git a/npm_deps.bzl b/npm_deps.bzl
index 3fda83d..31632c3 100644
--- a/npm_deps.bzl
+++ b/npm_deps.bzl
@@ -1,6 +1,18 @@
"""Define npm deps in yarn_install && npm_install repositories"""
-load("//:index.bzl", "npm_install", "yarn_install")
+load("//:index.bzl", _npm_install = "npm_install", _yarn_install = "yarn_install")
+
+def npm_install(**kwargs):
+ _npm_install(
+ node_repository = "node16",
+ **kwargs
+ )
+
+def yarn_install(**kwargs):
+ _yarn_install(
+ node_repository = "node16",
+ **kwargs
+ )
def npm_deps():
"""Organize all yarn_install and npm_install calls here to prevent WORKSPACE bloat"""