Skip to content

Commit

Permalink
feat(builtin): npm_install/yarn_install node repo
Browse files Browse the repository at this point in the history
Repository rules need to know which nodejs repository to get tools from
because they run before toolchain resolution.

Removes another hardcoded usage of "@nodejs"
  • Loading branch information
alexeagle committed Dec 22, 2021
1 parent 0a7d5c7 commit c47667a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 22 deletions.
25 changes: 20 additions & 5 deletions docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -746,9 +746,10 @@ Defaults to `None`
<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.
Expand Down Expand Up @@ -976,6 +977,13 @@ fine grained npm dependencies.

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.
Expand Down Expand Up @@ -1413,8 +1421,8 @@ Defaults to `{}`
<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>

Expand Down Expand Up @@ -1658,6 +1666,13 @@ fine grained npm dependencies.

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*)
Expand Down
20 changes: 8 additions & 12 deletions internal/node/node_labels.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,18 @@ Labels are different on windows and linux/OSX.

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")
11 changes: 7 additions & 4 deletions internal/npm_install/npm_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ node_modules target it is recommended to switch to using
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,
Expand Down Expand Up @@ -627,11 +632,11 @@ def _add_node_repositories_info_deps(repository_ctx):
# 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"),
)

Expand Down Expand Up @@ -667,7 +672,6 @@ def _check_min_bazel_version(rule, repository_ctx):

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)
Expand Down Expand Up @@ -807,7 +811,6 @@ check if yarn is being run by the `npm_install` repository rule.""",

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)
Expand Down
14 changes: 13 additions & 1 deletion npm_deps.bzl
Original file line number Diff line number Diff line change
@@ -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"""
Expand Down

0 comments on commit c47667a

Please sign in to comment.