Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Multiple Node Versions in Single Workspace #2148

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions internal/node/node_labels.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,28 @@ load("//internal/common:os_name.bzl", "is_windows_os", "os_name")

def get_node_label(rctx):
if is_windows_os(rctx):
label = Label("@nodejs_%s//:bin/node.cmd" % os_name(rctx))
label = Label("@%s_%s//:bin/node.cmd" % (rctx.name, os_name(rctx)))
else:
label = Label("@nodejs_%s//:bin/node" % os_name(rctx))
label = Label("@%s_%s//:bin/node" % (rctx.name, os_name(rctx)))
return label

def get_npm_label(rctx):
if is_windows_os(rctx):
label = Label("@nodejs_%s//:bin/npm.cmd" % os_name(rctx))
label = Label("@%s_%s//:bin/npm.cmd" % (rctx.name, os_name(rctx)))
else:
label = Label("@nodejs_%s//:bin/npm" % os_name(rctx))
label = Label("@%s_%s//:bin/npm" % (rctx.name, os_name(rctx)))
return label

def get_npm_node_repositories_label(rctx):
if is_windows_os(rctx):
label = Label("@nodejs_%s//:bin/npm_node_repositories.cmd" % os_name(rctx))
label = Label("@%s_%s//:bin/npm_node_repositories.cmd" % (rctx.name, os_name(rctx)))
else:
label = Label("@nodejs_%s//:bin/npm_node_repositories" % os_name(rctx))
label = Label("@%s_%s//:bin/npm_node_repositories" % (rctx.name, os_name(rctx)))
return label

def get_yarn_label(rctx):
if is_windows_os(rctx):
label = Label("@nodejs_%s//:bin/yarn.cmd" % os_name(rctx))
label = Label("@%s_%s//:bin/yarn.cmd" % (rctx.name, os_name(rctx)))
else:
label = Label("@nodejs_%s//:bin/yarn" % os_name(rctx))
label = Label("@%s_%s//:bin/yarn" % (rctx.name, os_name(rctx)))
return label
14 changes: 10 additions & 4 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def _download_node(repository_ctx):
# The host is baked into the repository name by design.
# Current these workspaces are:
# @nodejs_PLATFORM where PLATFORM is one of BUILT_IN_NODE_PLATFORMS
host_os = repository_ctx.name.split("nodejs_", 1)[1]
host_os = repository_ctx.name.split("_", 1)[1]

node_version = repository_ctx.attr.node_version
node_repositories = repository_ctx.attr.node_repositories
Expand Down Expand Up @@ -695,7 +695,7 @@ alias(name = "node_files", actual = "{node_repository}//:node_files")
alias(name = "yarn_files", actual = "{node_repository}//:yarn_files")
alias(name = "npm_files", actual = "{node_repository}//:npm_files")
exports_files(["index.bzl"])
""".format(node_repository = "@nodejs_%s" % os_name(repository_ctx)))
""".format(node_repository = "@%s_%s" % (repository_ctx.name, os_name(repository_ctx))))

# index.bzl file for this repository
repository_ctx.file("index.bzl", content = """# Generated by node_repositories.bzl
Expand Down Expand Up @@ -724,10 +724,16 @@ def node_repositories(**kwargs):
minimum_bazel_version = "2.1.0",
)

# allow user to set a custom name (eg: node11, node12) to support multiple versions
name = kwargs.get('name', 'nodejs')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do this in one step - pop returns the element


# prevent downstream errors from duplicate name
kwargs.pop('name', None)

# This needs to be setup so toolchains can access nodejs for all different versions
for os_arch_name in OS_ARCH_NAMES:
os_name = "_".join(os_arch_name)
node_repository_name = "nodejs_%s" % os_name
node_repository_name = "%s_%s" % (name, os_name)
_maybe(
node_repositories_rule,
name = node_repository_name,
Expand All @@ -743,7 +749,7 @@ def node_repositories(**kwargs):
# All it does is create aliases to the @nodejs_<host_os>_<host_arch> repository
_maybe(
_nodejs_repo_host_os_alias,
name = "nodejs",
name = name,
)

def _maybe(repo_rule, name, **kwargs):
Expand Down
8 changes: 6 additions & 2 deletions internal/npm_install/npm_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ repository so all files that the package manager depends on must be listed.
doc = """Environment variables to set before calling the package manager.""",
default = {},
),
"node_repository": attr.string(
doc = """If a custom name was provided to node_repositories, specify it here.""",
default = """nodejs""",
),
"included_files": attr.string_list(
doc = """List of file extensions to be included in the npm package targets.

Expand Down Expand Up @@ -158,11 +162,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("@%s_%s//:node_info" % (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("@%s_%s//:yarn_info" % (repository_ctx.attr.node_repository, os_name(repository_ctx))),
repository_ctx.path("_yarn_info"),
)

Expand Down