Skip to content

Commit

Permalink
fix(builtin): rerun yarn_install and npm_install when node version ch…
Browse files Browse the repository at this point in the history
…anges

Also always symlink package.json and lock file and copy over data files to ensure that yarn_install and npm_install rerun when any of these change and to ensure that all of the labels passed to `data` are evaluated by Bazel to ensure they are regular files. A typo in a label is a very easy error to make as the label must be `//:patches/jest-haste-map+25.3.0.patch` and not `//patches:jest-haste-map+25.3.0.patch` for example if `//patches` is not a package. With this fix, Bazel will check that the labels passed to `data` are valid.
  • Loading branch information
gregmagolan committed May 5, 2020
1 parent 7ac0ada commit 81bba0c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 20 deletions.
1 change: 1 addition & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ load("@build_bazel_rules_nodejs//:index.bzl", "npm_install", "yarn_install")
yarn_install(
name = "npm",
data = [
"//:patches/jest-haste-map+25.3.0.patch",
"//internal/npm_install/test:postinstall.js",
],
environment = {
Expand Down
2 changes: 1 addition & 1 deletion examples/vendored_node_and_yarn/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ yarn_install(
name = "npm",
data = [
"@vendored_node_10_12_0//:node-v10.12.0-linux-x64/bin/node",
"@vendored_yarn_1_10_0//:vendored_yarn_1_10_0/yarn-v1.10.0/bin/yarn.js",
"@vendored_yarn_1_10_0//:yarn-v1.10.0/bin/yarn.js",
],
package_json = "//:package.json",
yarn_lock = "//:yarn.lock",
Expand Down
24 changes: 24 additions & 0 deletions internal/node/node_repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ def _download_node(repository_ctx):
repository_ctx: The repository rule context
"""
if repository_ctx.attr.vendored_node:
repository_ctx.file("node_info", content = "# vendored_node: {vendored_node}".format(
vendored_node = repository_ctx.attr.vendored_node,
))
return

# The host is baked into the repository name by design.
Expand All @@ -304,13 +307,25 @@ def _download_node(repository_ctx):
sha256 = sha256,
)

repository_ctx.file("node_info", content = """# filename: {filename}
# strip_prefix: {strip_prefix}
# sha256: {sha256}
""".format(
filename = filename,
strip_prefix = strip_prefix,
sha256 = sha256,
))

def _download_yarn(repository_ctx):
"""Used to download a yarn tool package.
Args:
repository_ctx: The repository rule context
"""
if repository_ctx.attr.vendored_yarn:
repository_ctx.file("yarn_info", content = "# vendored_yarn: {vendored_yarn}".format(
vendored_yarn = repository_ctx.attr.vendored_yarn,
))
return

yarn_version = repository_ctx.attr.yarn_version
Expand All @@ -329,6 +344,15 @@ def _download_yarn(repository_ctx):
sha256 = sha256,
)

repository_ctx.file("yarn_info", content = """# filename: {filename}
# strip_prefix: {strip_prefix}
# sha256: {sha256}
""".format(
filename = filename,
strip_prefix = strip_prefix,
sha256 = sha256,
))

def _prepare_node(repository_ctx):
"""Sets up BUILD files and shell wrappers for the versions of NodeJS, npm & yarn just set up.
Expand Down
54 changes: 35 additions & 19 deletions internal/npm_install/npm_install.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ See discussion in the README.

load("//:version.bzl", "VERSION")
load("//internal/common:check_bazel_version.bzl", "check_bazel_version")
load("//internal/common:os_name.bzl", "is_windows_os")
load("//internal/common:os_name.bzl", "is_windows_os", "os_name")
load("//internal/node:node_labels.bzl", "get_node_label", "get_npm_label", "get_yarn_label")

COMMON_ATTRIBUTES = dict(dict(), **{
Expand Down Expand Up @@ -70,8 +70,14 @@ error.
"data": attr.label_list(
doc = """Data files required by this rule.
If symlink_node_modules is True, this attribute is ignored since
the dependency manager will run in the package.json location.
If symlink_node_modules is True, this attribute is optional since the package manager
will run in your workspace folder. It is recommended, however, that all files that the
package manager depends on, such as `.rc` files or files used in `postinstall`, are added
symlink_node_modules is True so that the repository rule is rerun when any of these files
change.
If symlink_node_modules is False, the package manager is run in the bazel external
repository so all files that the package manager depends on must be listed.
""",
),
"environment": attr.string_dict(
Expand Down Expand Up @@ -187,6 +193,18 @@ def _add_data_dependencies(repository_ctx):
# files as npm file:// packages
repository_ctx.template("/".join(to), f, {})

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)),
repository_ctx.path("_node_info"),
)
repository_ctx.symlink(
Label("@nodejs_%s//:yarn_info" % os_name(repository_ctx)),
repository_ctx.path("_yarn_info"),
)

def _symlink_node_modules(repository_ctx):
package_json_dir = repository_ctx.path(repository_ctx.attr.package_json).dirname
repository_ctx.symlink(repository_ctx.path(str(package_json_dir) + "/node_modules"), repository_ctx.path("node_modules"))
Expand Down Expand Up @@ -255,15 +273,14 @@ cd /D "{root}" && "{npm}" {npm_args}
executable = True,
)

if not repository_ctx.attr.symlink_node_modules:
repository_ctx.symlink(
repository_ctx.attr.package_lock_json,
repository_ctx.path("package-lock.json"),
)
_add_package_json(repository_ctx)
_add_data_dependencies(repository_ctx)

repository_ctx.symlink(
repository_ctx.attr.package_lock_json,
repository_ctx.path("package-lock.json"),
)
_add_package_json(repository_ctx)
_add_data_dependencies(repository_ctx)
_add_scripts(repository_ctx)
_add_node_repositories_info_deps(repository_ctx)

result = repository_ctx.execute(
[node, "pre_process_package_json.js", repository_ctx.path(repository_ctx.attr.package_json), "npm"],
Expand Down Expand Up @@ -393,15 +410,14 @@ cd /D "{root}" && "{yarn}" {yarn_args}
executable = True,
)

if not repository_ctx.attr.symlink_node_modules:
repository_ctx.symlink(
repository_ctx.attr.yarn_lock,
repository_ctx.path("yarn.lock"),
)
_add_package_json(repository_ctx)
_add_data_dependencies(repository_ctx)

repository_ctx.symlink(
repository_ctx.attr.yarn_lock,
repository_ctx.path("yarn.lock"),
)
_add_package_json(repository_ctx)
_add_data_dependencies(repository_ctx)
_add_scripts(repository_ctx)
_add_node_repositories_info_deps(repository_ctx)

result = repository_ctx.execute(
[node, "pre_process_package_json.js", repository_ctx.path(repository_ctx.attr.package_json), "yarn"],
Expand Down
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
"homepage": "https://github.com/bazelbuild/rules_nodejs",
"repository": "https://github.com/bazelbuild/rules_nodejs",
"license": "Apache-2.0",
"engines": {
"node": ">=12.0.0 < 13",
"yarn": ">=1.13.0"
},
"devDependencies": {
"@angular/common": "^9.1.0",
"@angular/compiler": "^9.1.0",
Expand Down

0 comments on commit 81bba0c

Please sign in to comment.