Skip to content

Commit 7f0102e

Browse files
authored
fix(builtin): linker test should run program as an action (#1113)
We were accidentally using runfiles resolution to read all user inputs, because we ran as an sh_test
1 parent 162e436 commit 7f0102e

23 files changed

Lines changed: 760 additions & 295 deletions

.bazelrc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,20 @@ import %workspace%/third_party/github.com/bazelbuild/bazel-toolchains/bazelrc/ba
3535
# Remote instance, borrow the one used by Angular devs
3636
build:remote --remote_instance_name=projects/internal-200822/instances/default_instance
3737
build:remote --project_id=internal-200822
38+
39+
# To reproduce Windows issues where there is no runfiles symlink there
40+
build:no-runfiles --noenable_runfiles
41+
# workaround https://github.com/bazelbuild/bazel/issues/7994
42+
build:no-runfiles --spawn_strategy=standalone
43+
# This config is probably only used while debugging
44+
build:no-runfiles --define=VERBOSE_LOG=1
45+
46+
# Docker Sandbox Mode
47+
# Useful for troubleshooting Remote Build Execution problems
48+
# See https://docs.bazel.build/versions/master/remote-execution-sandbox.html#prerequisites
49+
build:docker-sandbox --spawn_strategy=docker --strategy=Javac=docker --genrule_strategy=docker
50+
build:docker-sandbox --define=EXECUTOR=remote
51+
build:docker-sandbox --experimental_docker_verbose
52+
build:docker-sandbox --experimental_enable_docker_sandbox
53+
# This is the same image used on BazelCI rbe_ubuntu1604 job
54+
build:docker-sandbox --experimental_docker_image=gcr.io/cloud-marketplace/google/rbe-ubuntu16-04

internal/js_library/js_library.bzl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ def write_amd_names_shim(actions, amd_names_shim, targets):
4848

4949
def _js_library(ctx):
5050
return [
51-
DefaultInfo(files = depset(ctx.files.srcs)),
51+
DefaultInfo(
52+
files = depset(ctx.files.srcs),
53+
runfiles = ctx.runfiles(files = ctx.files.srcs),
54+
),
5255
AmdNamesInfo(names = ctx.attr.amd_names),
5356
]
5457

internal/linker/BUILD.bazel

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,19 @@
1-
exports_files(["link_node_modules.js"])
1+
# BEGIN-INTERNAL
2+
load("@npm_bazel_typescript//:index.from_src.bzl", "checked_in_ts_library")
3+
4+
# We can't bootstrap the ts_library rule using the linker itself,
5+
# because the implementation of ts_library depends on the linker so that would be a cycle.
6+
# So we compile it to JS and check in the result as index.js
7+
checked_in_ts_library(
8+
name = "linker_lib",
9+
srcs = ["link_node_modules.ts"],
10+
checked_in_js = "index.js",
11+
visibility = ["//internal/linker:__subpackages__"],
12+
deps = ["@npm//@types/node"],
13+
)
14+
15+
# END-INTERNAL
16+
exports_files(["index.js"])
217

318
filegroup(
419
name = "package_contents",

internal/linker/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ Under Bazel, we have exactly this monorepo feature. But, we want users to have a
1212
To make this seamless, we run a linker as a separate program inside the Bazel action, right before node.
1313
It does essentially the same job as Lerna: make sure there is a `$PWD/node_modules` tree and that all the semantics from Bazel (such as `module_name`/`module_root` attributes) are mapped to the node module resolution algorithm, so that the node runtime behaves the same way as if the packages had been installed from npm.
1414

15+
Note that the behavior of the linker depends on whether the package to link was declared as:
16+
17+
1. a runtime dependency of a binary run by Bazel, which we call "statically linked", and which is resolved from Bazel's Runfiles tree or manifest
18+
1. a dependency declared by a user of that binary, which we call "dynamically linked", and which is resolved from the execution root
19+
1520
In the future the linker should also generate `package.json` files so that things like `main` and `typings` fields are present and reflect the Bazel semantics, so that we can entirely eliminate custom loading and pathmapping logic from binaries we execute.
1621

1722
[lerna]: https://github.com/lerna/lerna

internal/linker/index.js

Lines changed: 206 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/linker/link_node_modules.bzl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def _debug(vars, *args):
2020
_ASPECT_RESULT_NAME = "link_node_modules__aspect_result"
2121

2222
# Traverse 'srcs' in addition so that we can go across a genrule
23-
_MODULE_MAPPINGS_DEPS_NAMES = ["deps", "srcs"]
23+
_MODULE_MAPPINGS_DEPS_NAMES = ["data", "deps", "srcs"]
2424

2525
def register_node_modules_linker(ctx, args, inputs):
2626
"""Helps an action to run node by setting up the node_modules linker as a pre-process
@@ -55,7 +55,12 @@ def register_node_modules_linker(ctx, args, inputs):
5555
# Write the result to a file, and use the magic node option --bazel_node_modules_manifest
5656
# The node_launcher.sh will peel off this argument and pass it to the linker rather than the program.
5757
modules_manifest = ctx.actions.declare_file("_%s.module_mappings.json" % ctx.label.name)
58-
ctx.actions.write(modules_manifest, str({"modules": mappings, "root": node_modules_root}))
58+
content = {
59+
"modules": mappings,
60+
"root": node_modules_root,
61+
"workspace": ctx.workspace_name,
62+
}
63+
ctx.actions.write(modules_manifest, str(content))
5964
args.add("--bazel_node_modules_manifest=%s" % modules_manifest.path)
6065
inputs.append(modules_manifest)
6166

internal/linker/link_node_modules.js

Lines changed: 0 additions & 156 deletions
This file was deleted.

0 commit comments

Comments
 (0)