Skip to content

Commit

Permalink
refactor: npm_install and yarn_install no longer create node reposito…
Browse files Browse the repository at this point in the history
…ries

This convenience is now too magical and confusing.
If the user registers nodejs toolchains, this will create an extra toolchain they don't use,
but can be fetched by rules that reference the default.
It also makes a @Yarn workspace available even for users who only need npm.

BREAKING CHANGE:
you now need to explicitly register nodejs toolchains or use the node_repositories helper
  • Loading branch information
alexeagle committed Jan 16, 2022
1 parent 5ad9753 commit b350579
Show file tree
Hide file tree
Showing 26 changed files with 119 additions and 15 deletions.
11 changes: 10 additions & 1 deletion WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ nodejs_register_toolchains(
node_version = "15.14.0",
)

load("@rules_nodejs//nodejs:yarn_repositories.bzl", "yarn_repositories")

yarn_repositories(
name = "yarn",
node_repository = "node16",
)

load("@build_bazel_rules_nodejs//:npm_deps.bzl", "npm_deps")

npm_deps()
Expand Down Expand Up @@ -131,7 +138,9 @@ browser_repositories(
# Setup esbuild dependencies
load("//toolchains/esbuild:esbuild_repositories.bzl", "esbuild_repositories")

esbuild_repositories()
esbuild_repositories(
node_repository = "node16",
)

#
# Dependencies to run stardoc & generating documentation
Expand Down
8 changes: 7 additions & 1 deletion docs/Toolchains.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Defaults to `""`
**USAGE**

<pre>
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>, <a href="#esbuild_repositories-npm_args">npm_args</a>)
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>, <a href="#esbuild_repositories-npm_args">npm_args</a>, <a href="#esbuild_repositories-kwargs">kwargs</a>)
</pre>

Helper for fetching and setting up the esbuild versions and toolchains
Expand Down Expand Up @@ -228,4 +228,10 @@ additional args to pass to the npm install rule

Defaults to `[]`

<h4 id="esbuild_repositories-kwargs">kwargs</h4>

additional named parameters to the npm_install rule




8 changes: 7 additions & 1 deletion docs/esbuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ Any other common attributes
**USAGE**

<pre>
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>, <a href="#esbuild_repositories-npm_args">npm_args</a>)
esbuild_repositories(<a href="#esbuild_repositories-name">name</a>, <a href="#esbuild_repositories-npm_repository">npm_repository</a>, <a href="#esbuild_repositories-npm_args">npm_args</a>, <a href="#esbuild_repositories-kwargs">kwargs</a>)
</pre>

Helper for fetching and setting up the esbuild versions and toolchains
Expand Down Expand Up @@ -476,4 +476,10 @@ additional args to pass to the npm install rule

Defaults to `[]`

<h4 id="esbuild_repositories-kwargs">kwargs</h4>

additional named parameters to the npm_install rule




4 changes: 4 additions & 0 deletions e2e/bazel_managed_deps/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions e2e/concatjs_devserver/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions e2e/coverage/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

# Load web_test_suite
http_archive(
name = "io_bazel_rules_webtesting",
Expand Down
4 changes: 4 additions & 0 deletions e2e/jasmine/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions e2e/node_loader_preserve_symlinks/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions e2e/nodejs_host/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
6 changes: 4 additions & 2 deletions e2e/nodejs_image/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down Expand Up @@ -53,8 +57,6 @@ load("@io_bazel_rules_docker//nodejs:image.bzl", nodejs_image_repositories = "re

nodejs_image_repositories()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

# The order matters because Bazel will provide the first registered toolchain when a rule asks Bazel to select it
# This applies to the resolved_toolchain
nodejs_register_toolchains(
Expand Down
4 changes: 4 additions & 0 deletions e2e/typescript/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

# Load web_test_suite
http_archive(
name = "io_bazel_rules_webtesting",
Expand Down
4 changes: 4 additions & 0 deletions e2e/webapp/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
6 changes: 4 additions & 2 deletions examples/angular_bazel_architect/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

# The yarn_install rule runs yarn anytime the package.json or yarn.lock file changes.
# It also extracts and installs any Bazel rules distributed in an npm package.
load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")
Expand All @@ -32,8 +36,6 @@ yarn_install(
yarn_lock = "//:yarn.lock",
)

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(
name = "node16",
node_version = "16.9.0",
Expand Down
4 changes: 4 additions & 0 deletions examples/app/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/closure/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/create-react-app/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/cypress/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install")

node_repositories(
Expand Down
4 changes: 4 additions & 0 deletions examples/esbuild/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/jest/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/kotlin/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

# Install external npm dependencies
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")

Expand Down
4 changes: 4 additions & 0 deletions examples/nestjs/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/web_testing/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/webapp/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
4 changes: 4 additions & 0 deletions examples/worker/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ load("@build_bazel_rules_nodejs//:repositories.bzl", "build_bazel_rules_nodejs_d

build_bazel_rules_nodejs_dependencies()

load("@rules_nodejs//nodejs:repositories.bzl", "nodejs_register_toolchains")

nodejs_register_toolchains(name = "nodejs")

load("@build_bazel_rules_nodejs//:index.bzl", "yarn_install")

yarn_install(
Expand Down
15 changes: 8 additions & 7 deletions index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
Users should not load files under "/internal"
"""

load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@rules_nodejs//nodejs:yarn_repositories.bzl", _yarn_repositories = "yarn_repositories")
load("//:version.bzl", "VERSION")
load("//internal/common:check_version.bzl", "check_version")
load("//internal/common:copy_to_bin.bzl", _copy_to_bin = "copy_to_bin")
Expand Down Expand Up @@ -44,6 +46,7 @@ copy_to_bin = _copy_to_bin
params_file = _params_file
generated_file_test = _generated_file_test
js_library = _js_library
npm_install = _npm_install
# ANY RULES ADDED HERE SHOULD BE DOCUMENTED, see index.for_docs.bzl

# @unsorted-dict-items
Expand All @@ -55,14 +58,12 @@ COMMON_REPLACEMENTS = {
"\"@bazel/([a-zA-Z_-]+)\":\\s+\"(file|bazel)[^\"]+\"": "\"@bazel/$1\": \"0.0.0-PLACEHOLDER\"",
}

def npm_install(**kwargs):
# Just in case the user didn't install nodejs, do it now
_node_repositories()
_npm_install(**kwargs)

def yarn_install(**kwargs):
# Just in case the user didn't install nodejs, do it now
_node_repositories()
# Just in case the user didn't install yarn, and if they aren't using a custom yarn binary,
# install the default "@yarn" one for them here.
if "yarn" not in kwargs.keys():
maybe(_yarn_repositories, name = "yarn")

_yarn_install(**kwargs)

# Currently used Bazel version. This version is what the rules here are tested
Expand Down
4 changes: 3 additions & 1 deletion toolchains/esbuild/esbuild_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("@build_bazel_rules_nodejs//:index.bzl", "npm_install")
load(":esbuild_packages.bzl", "ESBUILD_PACKAGES")

def esbuild_repositories(name = "", npm_repository = "npm", npm_args = []):
def esbuild_repositories(name = "", npm_repository = "npm", npm_args = [], **kwargs):
"""Helper for fetching and setting up the esbuild versions and toolchains
This uses Bazel's downloader (via `http_archive`) to fetch the esbuild package
Expand All @@ -29,6 +29,7 @@ def esbuild_repositories(name = "", npm_repository = "npm", npm_args = []):
npm_repository: the name of the repository where the @bazel/esbuild package is installed
by npm_install or yarn_install.
npm_args: additional args to pass to the npm install rule
**kwargs: additional named parameters to the npm_install rule
"""

maybe(
Expand Down Expand Up @@ -78,4 +79,5 @@ def esbuild_repositories(name = "", npm_repository = "npm", npm_args = []):
"--ignore-scripts",
] + npm_args,
package_path = package_path,
**kwargs
)

0 comments on commit b350579

Please sign in to comment.