Skip to content

Commit

Permalink
chore: stop locally depending on old toolchain
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Dec 22, 2021
1 parent 0a7d5c7 commit 5dfdb35
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
6 changes: 1 addition & 5 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ local_repository(
# Setup node repositories
#

load("//:index.bzl", "BAZEL_VERSION", "SUPPORTED_BAZEL_VERSIONS", "node_repositories")

node_repositories(
node_version = "16.5.0",
)
load("//:index.bzl", "BAZEL_VERSION", "SUPPORTED_BAZEL_VERSIONS")

#
# Install rules_nodejs dev dependencies
Expand Down
12 changes: 2 additions & 10 deletions index.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ params_file = _params_file
generated_file_test = _generated_file_test
js_library = _js_library
directory_file_path = _directory_file_path
npm_install = _npm_install
yarn_install = _yarn_install
# ANY RULES ADDED HERE SHOULD BE DOCUMENTED, see index.for_docs.bzl

# @unsorted-dict-items
Expand All @@ -62,16 +64,6 @@ 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()
_yarn_install(**kwargs)

# Currently used Bazel version. This version is what the rules here are tested
# against.
# This version should be updated together with the version of the Bazel
Expand Down
7 changes: 4 additions & 3 deletions internal/linker/test/integration/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ genrule(
name = "replace_node_path",
srcs = [":run_program.sh"],
outs = ["run_program_with_node.sh"],
cmd = "sed s#NODE_PATH#$(NODE_PATH)# $< > $@",
toolchains = ["@build_bazel_rules_nodejs//toolchains/node:toolchain"],
# Remove leading external/ segment to account for --nolegacy_external_runfiles
cmd = "sed s#NODE_PATH#$(NODE_PATH)# $< | sed s#external/## > $@",
toolchains = ["@node16_toolchains//:resolved_toolchain"],
)

# A program from a *_test rule with a module mapped package that will
Expand Down Expand Up @@ -45,7 +46,7 @@ sh_binary(
"//internal/linker/test/integration/static_linked_scoped_pkg",
"//internal/linker/test/integration/absolute_import:index.js",
"//third_party/github.com/bazelbuild/bazel/tools/bash/runfiles",
"//toolchains/node:node_bin",
"@node16_toolchains//:resolved_toolchain",
],
)

Expand Down
8 changes: 4 additions & 4 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -241,18 +241,18 @@ fi

# Add both the node executable for the user's local machine which is in ctx.files._node and comes
# from @nodejs//:node_bin and the node executable from the selected node --platform which comes from
# ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo.
# ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo.
# In most cases these are the same files but for RBE and when explitely setting --platform for cross-compilation
# any given nodejs_binary should be able to run on both the user's local machine and on the RBE or selected
# platform.
#
# Rules such as nodejs_image should use only ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"].nodeinfo
# Rules such as nodejs_image should use only ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"].nodeinfo
# when building the image as that will reflect the selected --platform.

if ctx.attr.toolchain:
node_toolchain = ctx.attr.toolchain[platform_common.ToolchainInfo]
else:
node_toolchain = ctx.toolchains["@build_bazel_rules_nodejs//toolchains/node:toolchain_type"]
node_toolchain = ctx.toolchains["@rules_nodejs//nodejs:toolchain_type"]

node_tool_files = []
node_tool_files.extend(node_toolchain.nodeinfo.tool_files)
Expand Down Expand Up @@ -636,7 +636,7 @@ This will pass --preserve-symlinks and --no-warnings flags to nodejs. Available
"implementation": _nodejs_binary_impl,
"outputs": _NODEJS_EXECUTABLE_OUTPUTS,
"toolchains": [
"@build_bazel_rules_nodejs//toolchains/node:toolchain_type",
"@rules_nodejs//nodejs:toolchain_type",
"@bazel_tools//tools/sh:toolchain_type",
],
}
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 5dfdb35

Please sign in to comment.