Skip to content

Commit 8c1e035

Browse files
gregmagolanalexeagle
authored andcommitted
fix(builtin): rerun yarn_install and npm_install when node version changes
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.
1 parent c0a8b36 commit 8c1e035

5 files changed

Lines changed: 65 additions & 20 deletions

File tree

WORKSPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ load("@build_bazel_rules_nodejs//:index.bzl", "npm_install", "yarn_install")
5151
yarn_install(
5252
name = "npm",
5353
data = [
54+
"//:patches/jest-haste-map+25.3.0.patch",
5455
"//internal/npm_install/test:postinstall.js",
5556
],
5657
environment = {

examples/vendored_node_and_yarn/WORKSPACE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ yarn_install(
5252
name = "npm",
5353
data = [
5454
"@vendored_node_10_12_0//:node-v10.12.0-linux-x64/bin/node",
55-
"@vendored_yarn_1_10_0//:vendored_yarn_1_10_0/yarn-v1.10.0/bin/yarn.js",
55+
"@vendored_yarn_1_10_0//:yarn-v1.10.0/bin/yarn.js",
5656
],
5757
package_json = "//:package.json",
5858
yarn_lock = "//:yarn.lock",

internal/node/node_repositories.bzl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,9 @@ def _download_node(repository_ctx):
280280
repository_ctx: The repository rule context
281281
"""
282282
if repository_ctx.attr.vendored_node:
283+
repository_ctx.file("node_info", content = "# vendored_node: {vendored_node}".format(
284+
vendored_node = repository_ctx.attr.vendored_node,
285+
))
283286
return
284287

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

310+
repository_ctx.file("node_info", content = """# filename: {filename}
311+
# strip_prefix: {strip_prefix}
312+
# sha256: {sha256}
313+
""".format(
314+
filename = filename,
315+
strip_prefix = strip_prefix,
316+
sha256 = sha256,
317+
))
318+
307319
def _download_yarn(repository_ctx):
308320
"""Used to download a yarn tool package.
309321
310322
Args:
311323
repository_ctx: The repository rule context
312324
"""
313325
if repository_ctx.attr.vendored_yarn:
326+
repository_ctx.file("yarn_info", content = "# vendored_yarn: {vendored_yarn}".format(
327+
vendored_yarn = repository_ctx.attr.vendored_yarn,
328+
))
314329
return
315330

316331
yarn_version = repository_ctx.attr.yarn_version
@@ -329,6 +344,15 @@ def _download_yarn(repository_ctx):
329344
sha256 = sha256,
330345
)
331346

347+
repository_ctx.file("yarn_info", content = """# filename: {filename}
348+
# strip_prefix: {strip_prefix}
349+
# sha256: {sha256}
350+
""".format(
351+
filename = filename,
352+
strip_prefix = strip_prefix,
353+
sha256 = sha256,
354+
))
355+
332356
def _prepare_node(repository_ctx):
333357
"""Sets up BUILD files and shell wrappers for the versions of NodeJS, npm & yarn just set up.
334358

internal/npm_install/npm_install.bzl

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ See discussion in the README.
2323

2424
load("//:version.bzl", "VERSION")
2525
load("//internal/common:check_bazel_version.bzl", "check_bazel_version")
26-
load("//internal/common:os_name.bzl", "is_windows_os")
26+
load("//internal/common:os_name.bzl", "is_windows_os", "os_name")
2727
load("//internal/node:node_labels.bzl", "get_node_label", "get_npm_label", "get_yarn_label")
2828

2929
COMMON_ATTRIBUTES = dict(dict(), **{
@@ -34,8 +34,14 @@ COMMON_ATTRIBUTES = dict(dict(), **{
3434
"data": attr.label_list(
3535
doc = """Data files required by this rule.
3636
37-
If symlink_node_modules is True, this attribute is ignored since
38-
the dependency manager will run in the package.json location.
37+
If symlink_node_modules is True, this attribute is optional since the package manager
38+
will run in your workspace folder. It is recommended, however, that all files that the
39+
package manager depends on, such as `.rc` files or files used in `postinstall`, are added
40+
symlink_node_modules is True so that the repository rule is rerun when any of these files
41+
change.
42+
43+
If symlink_node_modules is False, the package manager is run in the bazel external
44+
repository so all files that the package manager depends on must be listed.
3945
""",
4046
),
4147
"environment": attr.string_dict(
@@ -148,6 +154,18 @@ def _add_data_dependencies(repository_ctx):
148154
# files as npm file:// packages
149155
repository_ctx.template("/".join(to), f, {})
150156

157+
def _add_node_repositories_info_deps(repository_ctx):
158+
# Add a dep to the node_info & yarn_info files from node_repositories
159+
# so that if the node or yarn versions change we re-run the repository rule
160+
repository_ctx.symlink(
161+
Label("@nodejs_%s//:node_info" % os_name(repository_ctx)),
162+
repository_ctx.path("_node_info"),
163+
)
164+
repository_ctx.symlink(
165+
Label("@nodejs_%s//:yarn_info" % os_name(repository_ctx)),
166+
repository_ctx.path("_yarn_info"),
167+
)
168+
151169
def _symlink_node_modules(repository_ctx):
152170
package_json_dir = repository_ctx.path(repository_ctx.attr.package_json).dirname
153171
repository_ctx.symlink(repository_ctx.path(str(package_json_dir) + "/node_modules"), repository_ctx.path("node_modules"))
@@ -216,15 +234,14 @@ cd /D "{root}" && "{npm}" {npm_args}
216234
executable = True,
217235
)
218236

219-
if not repository_ctx.attr.symlink_node_modules:
220-
repository_ctx.symlink(
221-
repository_ctx.attr.package_lock_json,
222-
repository_ctx.path("package-lock.json"),
223-
)
224-
_add_package_json(repository_ctx)
225-
_add_data_dependencies(repository_ctx)
226-
237+
repository_ctx.symlink(
238+
repository_ctx.attr.package_lock_json,
239+
repository_ctx.path("package-lock.json"),
240+
)
241+
_add_package_json(repository_ctx)
242+
_add_data_dependencies(repository_ctx)
227243
_add_scripts(repository_ctx)
244+
_add_node_repositories_info_deps(repository_ctx)
228245

229246
result = repository_ctx.execute(
230247
[node, "pre_process_package_json.js", repository_ctx.path(repository_ctx.attr.package_json), "npm"],
@@ -354,15 +371,14 @@ cd /D "{root}" && "{yarn}" {yarn_args}
354371
executable = True,
355372
)
356373

357-
if not repository_ctx.attr.symlink_node_modules:
358-
repository_ctx.symlink(
359-
repository_ctx.attr.yarn_lock,
360-
repository_ctx.path("yarn.lock"),
361-
)
362-
_add_package_json(repository_ctx)
363-
_add_data_dependencies(repository_ctx)
364-
374+
repository_ctx.symlink(
375+
repository_ctx.attr.yarn_lock,
376+
repository_ctx.path("yarn.lock"),
377+
)
378+
_add_package_json(repository_ctx)
379+
_add_data_dependencies(repository_ctx)
365380
_add_scripts(repository_ctx)
381+
_add_node_repositories_info_deps(repository_ctx)
366382

367383
result = repository_ctx.execute(
368384
[node, "pre_process_package_json.js", repository_ctx.path(repository_ctx.attr.package_json), "yarn"],

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
"homepage": "https://github.com/bazelbuild/rules_nodejs",
1010
"repository": "https://github.com/bazelbuild/rules_nodejs",
1111
"license": "Apache-2.0",
12+
"engines": {
13+
"node": ">=12.0.0 < 13",
14+
"yarn": ">=1.13.0"
15+
},
1216
"devDependencies": {
1317
"@angular/common": "^9.1.0",
1418
"@angular/compiler": "^9.1.0",

0 commit comments

Comments
 (0)