Skip to content

Commit 5358d56

Browse files
committed
feat(builtin): support $(rootpath), $(execpath), predefined & custom variables in templated_args
* patch runfiles.bash helper to support $(rootpath) * add support for $(rootpath), $(execpath), predefined & custom variables to expand_location_into_runfiles helper function This applies to `templated_args` in nodejs_binary, nodejs_test & jasmine_node_test and to `args` in params_file Also fixes ambiguity of `$(rlocation $(location ...))` expansion as it is not clear that `$(rlocation ...)` is not meant to be expanded but rather passed down to the shell script for execution. This is now more clear with `$$(rlocation $(manifestpath ...))` which follows the Bazel "Make" variable expansion convention. For example, `templated_args = ["--node_options=--require=$$(rlocation $(manifestpath :jasmine_shared_env_bootstrap.js))",],`. `templated_args` in nodejs_binary, nodejs_test & jasmine_node_test is now… Subject to 'Make variable' substitution. See https://docs.bazel.build/versions/master/be/make-variables.html. 1. Predefined source/output path substitions is applied first: Expands all `$(execpath ...)`, `$(rootpath ...)` and legacy `$(location ...)` templates in the given string by replacing with the expanded path. Expansion only works for labels that point to direct dependencies of this rule or that are explicitly listed in the optional argument targets. See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables. Use `$(rootpath)` and `$(rootpaths)` to expand labels to the runfiles path that a built binary can use to find its dependencies. This path is of the format: - `./file` - `path/to/file` - `../external_repo/path/to/file` Use `$(execpath)` and `$(execpaths)` to expand labels to the execroot (where Bazel runs build actions). This is of the format: - `./file` - `path/to/file` - `external/external_repo/path/to/file` - `<bin_dir>/path/to/file` - `<bin_dir>/external/external_repo/path/to/file` The legacy `$(location)` and `$(locations)` expansion is DEPRECATED as it returns the runfiles manifest path of the format `repo/path/to/file` which behaves differently than the built-in `$(location)` expansion in args of *_binary and *_test rules which returns the rootpath. See https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-binaries. The legacy `$(location)` and `$(locations)` expansion also differs from how the builtin `ctx.expand_location()` expansions of `$(location)` and `$(locations)` behave as that function returns either the execpath or rootpath depending on the context. See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables. The behavior of `$(location)` and `$(locations)` expansion may change in the future with support either being removed entirely or the expansion changed to return the same path as `ctx.expand_location()` returns for these. The recommended approach is to now use `$(rootpath)` where you previously used $(location). To get from a `$(rootpath)` to the absolute path that `$$(rlocation $(location))` returned you can either use `$$(rlocation $(rootpath))` if you are in the `templated_args` of a `nodejs_binary` or `nodejs_test`: BUILD.bazel: ``` nodejs_test( name = "my_test", data = [":bootstrap.js"], templated_args = ["--node_options=--require=$$(rlocation $(rootpath :bootstrap.js))"], ) ``` or if you're in the context of a .js script you can pass the $(rootpath) as an argument to the script and use the javascript runfiles helper to resolve to the absolute path: BUILD.bazel: ``` nodejs_test( name = "my_test", data = [":some_file"], entry_point = ":my_test.js", templated_args = ["$(rootpath :some_file)"], ) ``` my_test.js ``` const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']); const args = process.argv.slice(2); const some_file = runfiles.resolveWorkspaceRelative(args[0]); ``` NB: Bazel will error if it sees the single dollar sign $(rlocation path) in `templated_args` as it will try to example `$(rlocation)` since we now expand predefined & custom "make" variables such as `$(COMPILATION_MODE)`, `$(BINDIR)` & `$(TARGET_CPU)` using `ctx.expand_make_variables`. See https://docs.bazel.build/versions/master/be/make-variables.html. To prevent expansion of `$(rlocation)` write it as `$$(rlocation)`. Bazel understands `$$` to be the string literal `$` and the expansion results in `$(rlocation)` being passed as an arg instead of being expanded. `$(rlocation)` is then evaluated by the bash node launcher script and it calls the `rlocation` function in the runfiles.bash helper. For example, the templated arg `$$(rlocation $(rootpath //:some_file))` is expanded by Bazel to `$(rlocation ./some_file)` which is then converted in bash to the absolute path of `//:some_file` in runfiles by the runfiles.bash helper before being passed as an argument to the program 2. Predefined variables & Custom variables are expanded second: Predefined "Make" variables such as $(COMPILATION_MODE) and $(TARGET_CPU) are expanded. See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_variables. Custom variables are also expanded including variables set through the Bazel CLI with --define=SOME_VAR=SOME_VALUE. See https://docs.bazel.build/versions/master/be/make-variables.html#custom_variables. Predefined genrule variables are not supported in this context.
1 parent 8fd0bc2 commit 5358d56

File tree

21 files changed

+384
-124
lines changed

21 files changed

+384
-124
lines changed

e2e/jasmine/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jasmine_node_test(
1010
srcs = ["jasmine_shared_env_test.spec.js"],
1111
data = ["jasmine_shared_env_bootstrap.js"],
1212
templated_args = [
13-
"--node_options=--require=$(rlocation $(location :jasmine_shared_env_bootstrap.js))",
13+
"--node_options=--require=$$(rlocation $(rootpath :jasmine_shared_env_bootstrap.js))",
1414
],
1515
deps = [
1616
"@npm//jasmine",

examples/angular/src/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ history_server(
209209
# / => src/prodapp
210210
templated_args = [
211211
"-a",
212-
"$(rlocation examples_angular/src/prodapp)",
212+
"$$(rlocation examples_angular/src/prodapp)",
213213
],
214214
)
215215

examples/angular_view_engine/src/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,6 @@ history_server(
217217
# / => src/prodapp
218218
templated_args = [
219219
"-a",
220-
"$(rlocation examples_angular_view_engine/src/prodapp)",
220+
"$$(rlocation examples_angular_view_engine/src/prodapp)",
221221
],
222222
)

examples/jest/jest.bzl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,18 @@ load("@npm//jest-cli:index.bzl", _jest_test = "jest_test")
44

55
def jest_test(name, srcs, deps, jest_config, **kwargs):
66
"A macro around the autogenerated jest_test rule"
7-
args = [
7+
templated_args = [
88
"--no-cache",
99
"--no-watchman",
1010
"--ci",
1111
]
12-
args.extend(["--config", "$(location %s)" % jest_config])
12+
templated_args.extend(["--config", "$(rootpath %s)" % jest_config])
13+
for src in srcs:
14+
templated_args.extend(["--runTestsByPath", "$(rootpath %s)" % src])
1315

1416
_jest_test(
1517
name = name,
1618
data = [jest_config] + srcs + deps,
17-
args = args,
19+
templated_args = templated_args,
1820
**kwargs
1921
)

internal/common/expand_into_runfiles.bzl

Lines changed: 58 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,62 @@
1515
"""Helper functions to expand paths into runfiles
1616
"""
1717

18-
# Expand $(location) and $(locations) to runfiles manifest path
19-
def _expand_mlocations(ctx, input, targets):
18+
# Expand $(rootpath) and $(rootpaths) to runfiles manifest path.
19+
# Runfiles manifest path is of the form:
20+
# - repo/path/to/file
21+
def _expand_rootpath_to_manifest_path(ctx, input, targets):
2022
paths = ctx.expand_location(input, targets)
21-
return " ".join([_short_path_to_runfiles_manifest_path(ctx, p, targets) for p in paths.split(" ")])
23+
return " ".join([_rootpath_to_runfiles_manifest_path(ctx, p, targets) for p in paths.split(" ")])
2224

23-
# Convert a short_path in the execroot to the runfiles manifest path
24-
def _short_path_to_runfiles_manifest_path(ctx, path, targets):
25+
# Convert an runfiles rootpath to a runfiles manifestpath.
26+
# Runfiles rootpath is returned from ctx.expand_location $(rootpath) and $(rootpaths):
27+
# - ./file
28+
# - path/to/file
29+
# - ../external_repo/path/to/file
30+
# This is converted to the runfiles manifest path of:
31+
# - repo/path/to/file
32+
def _rootpath_to_runfiles_manifest_path(ctx, path, targets):
2533
if path.startswith("../"):
2634
return path[len("../"):]
2735
if path.startswith("./"):
2836
path = path[len("./"):]
29-
elif path.startswith(ctx.bin_dir.path):
30-
path = path[len(ctx.bin_dir.path + "/"):]
31-
elif path.startswith(ctx.genfiles_dir.path):
32-
path = path[len(ctx.genfiles_dir.path + "/"):]
3337
return ctx.workspace_name + "/" + path
3438

35-
# Expand $(location) and $(locations) to runfiles short path
36-
def _expand_locations(ctx, input, targets):
37-
paths = ctx.expand_location(input, targets)
38-
return " ".join([_short_path_to_runfiles_short_path(ctx, p, targets) for p in paths.split(" ")])
39-
40-
# Convert a short_path in the execroot to the runfiles short path
41-
def _short_path_to_runfiles_short_path(ctx, path, targets):
42-
path = path.replace(ctx.bin_dir.path + "/external/", "../", 1)
43-
path = path.replace(ctx.bin_dir.path + "/", "", 1)
44-
path = path.replace(ctx.genfiles_dir.path + "/external/", "../", 1)
45-
path = path.replace(ctx.genfiles_dir.path + "/", "", 1)
46-
return path
47-
4839
def expand_location_into_runfiles(ctx, input, targets = []):
49-
"""Expands all $(location ...) templates in the given string by replacing $(location //x) with the path
50-
in runfiles of the output file of target //x. Expansion only works for labels that point to direct dependencies
40+
"""Expands all `$(execpath ...)`, `$(rootpath ...)` and legacy `$(location ...)` templates in the
41+
given string by replacing with the expanded path. Expansion only works for labels that point to direct dependencies
5142
of this rule or that are explicitly listed in the optional argument targets.
5243
53-
Path is returned in runfiles manifest path format such as `repo/path/to/file`. This differs from how $(location)
54-
and $(locations) expansion behaves in expansion the `args` attribute of a *_binary or *_test which returns
55-
the runfiles short path of the format `./path/to/file` for user repo and `../external_repo/path/to/file` for external
56-
repositories.
44+
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables.
45+
46+
Use `$(rootpath)` and `$(rootpaths)` to expand labels to the runfiles path that a built binary can use
47+
to find its dependencies. This path is of the format:
48+
- `./file`
49+
- `path/to/file`
50+
- `../external_repo/path/to/file`
51+
52+
Use `$(execpath)` and `$(execpaths)` to expand labels to the execroot (where Bazel runs build actions).
53+
This is of the format:
54+
- `./file`
55+
- `path/to/file`
56+
- `external/external_repo/path/to/file`
57+
- `<bin_dir>/path/to/file`
58+
- `<bin_dir>/external/external_repo/path/to/file`
5759
58-
This will be fixed in a future release major release as well as adding support for $(execpath) and $(rootpath)
59-
substitions: https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables.
60+
The legacy `$(location)` and `$(locations)` expansion is DEPRECATED as it returns the runfiles manifest path of the
61+
format `repo/path/to/file` which behaves differently than the built-in `$(location)` expansion in args of *_binary
62+
and *_test rules which returns the rootpath.
63+
See https://docs.bazel.build/versions/master/be/common-definitions.html#common-attributes-binaries.
64+
65+
The legacy `$(location)` and `$(locations)` expansion also differs from how the builtin `ctx.expand_location()` expansions
66+
of `$(location)` and `$(locations)` behave as that function returns either the execpath or rootpath depending on the context.
67+
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables.
68+
69+
The behavior of `$(location)` and `$(locations)` expansion will be fixed in a future major release to match the
70+
to default Bazel behavior and return the same path as `ctx.expand_location()` returns for these.
71+
72+
The recommended approach is to now use `$(rootpath)` where you previously used $(location). See the docstrings
73+
of `nodejs_binary` or `params_file` for examples of how to use `$(rootpath)` in `templated_args` and `args` respectively.
6074
6175
Args:
6276
ctx: context
@@ -68,28 +82,32 @@ def expand_location_into_runfiles(ctx, input, targets = []):
6882
"""
6983
target = "@%s//%s:%s" % (ctx.workspace_name, "/".join(ctx.build_file_path.split("/")[:-1]), ctx.attr.name)
7084

71-
# Loop through input an expand all $(location) and $(locations) using _expand_to_mlocation()
85+
# Loop through input an expand all predefined source/output path variables
86+
# See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables.
7287
path = ""
7388
length = len(input)
7489
last = 0
7590
for i in range(length):
76-
if (input[i:i + 12] == "$(mlocation ") or (input[i:i + 13] == "$(mlocations "):
91+
# Support legacy $(location) and $(locations) expansions which return the runfiles manifest path
92+
# in the format `repo/path/to/file`. This expansion is DEPRECATED. See docstring above.
93+
# TODO: Change location to behave the same as the built-in $(location) expansion for args of *_binary
94+
# and *_test rules. This would be a BREAKING CHANGE.
95+
if input[i:].startswith("$(location ") or input[i:].startswith("$(locations "):
7796
j = input.find(")", i) + 1
7897
if (j == 0):
79-
fail("invalid $(mlocation) expansion in string \"%s\" part of target %s" % (input, target))
98+
fail("invalid \"%s\" expansion in string \"%s\" part of target %s" % (input[i:j], input, target))
8099
path += input[last:i]
81-
path += _expand_mlocations(ctx, "$(" + input[i + 3:j], targets)
100+
path += _expand_rootpath_to_manifest_path(ctx, "$(rootpath" + input[i + 10:j], targets)
82101
last = j
83102
i = j
84-
if (input[i:i + 11] == "$(location ") or (input[i:i + 12] == "$(locations "):
103+
104+
# Expand $(execpath) $(execpaths) $(rootpath) $(rootpaths) with plain ctx.expand_location()
105+
if input[i:].startswith("$(execpath ") or input[i:].startswith("$(execpaths ") or input[i:].startswith("$(rootpath ") or input[i:].startswith("$(rootpaths "):
85106
j = input.find(")", i) + 1
86107
if (j == 0):
87-
fail("invalid $(location) expansion in string \"%s\" part of target %s" % (input, target))
108+
fail("invalid \"%s\" expansion in string \"%s\" part of target %s" % (input[i:j], input, target))
88109
path += input[last:i]
89-
90-
# TODO(gmagolan): flip to _expand_locations in the future so $(location) expands to runfiles short
91-
# path which is more Bazel idiomatic and $(mlocation) can be used for runfiles manifest path
92-
path += _expand_mlocations(ctx, input[i:j], targets)
110+
path += ctx.expand_location(input[i:j], targets)
93111
last = j
94112
i = j
95113
path += input[last:]

internal/common/params_file.bzl

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def _impl(ctx):
4747

4848
expanded_args = []
4949

50-
# First expand $(location) args
50+
# First expand predefined source/output path variables
5151
for a in ctx.attr.args:
5252
expanded_args += _expand_location_into_runfiles(ctx, a)
5353

@@ -87,17 +87,27 @@ def params_file(
8787
out: Path of the output file, relative to this package.
8888
args: Arguments to concatenate into a params file.
8989
90-
1. Subject to $(location) substitutions.
90+
Subject to 'Make variable' substitution. See https://docs.bazel.build/versions/master/be/make-variables.html.
9191
92-
NB: This substition returns the manifest file path which differs from the *_binary & *_test
92+
1. Subject to predefined source/output path variables substitutions.
93+
94+
The predefined variables `execpath`, `execpaths`, `rootpath`, `rootpaths`, `location`, and `locations` take
95+
label parameters (e.g. `$(execpath //foo:bar)`) and substitute the file paths denoted by that label.
96+
97+
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_label_variables for more info.
98+
99+
NB: This $(location) substition returns the manifest file path which differs from the *_binary & *_test
93100
args and genrule bazel substitions. This will be fixed in a future major release.
94-
See docs string of `expand_location_into_runfiles` macro in
95-
`internal/common/expand_into_runfiles.bzl` for more info.
101+
See docs string of `expand_location_into_runfiles` macro in `internal/common/expand_into_runfiles.bzl`
102+
for more info.
96103
97104
2. Subject to predefined variables & custom variable substitutions.
98105
99-
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_variables
100-
and https://docs.bazel.build/versions/master/be/make-variables.html#custom_variables.
106+
Predefined "Make" variables such as $(COMPILATION_MODE) and $(TARGET_CPU) are expanded.
107+
See https://docs.bazel.build/versions/master/be/make-variables.html#predefined_variables.
108+
109+
Custom variables are also expanded including variables set through the Bazel CLI with --define=SOME_VAR=SOME_VALUE.
110+
See https://docs.bazel.build/versions/master/be/make-variables.html#custom_variables.
101111
102112
Predefined genrule variables are not supported in this context.
103113

internal/common/test/BUILD.bazel

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,12 @@ params_file(
3636
out = ":params_file.out",
3737
args = [
3838
"$(SOME_TEST_ENV)",
39-
"$(location //:package.json)",
39+
"$(execpaths :locations_in)",
40+
"$(execpath //:package.json)",
41+
"$(rootpaths :locations_in)",
42+
"$(rootpath //:package.json)",
4043
"$(locations :locations_in)",
44+
"$(location //:package.json)",
4145
],
4246
data = [
4347
":locations_in",
@@ -49,6 +53,10 @@ jasmine_node_test(
4953
name = "params_file_test",
5054
srcs = [":params_file.spec.js"],
5155
data = [":params_file.out"],
56+
templated_args = [
57+
"$(TARGET_CPU)",
58+
"$(COMPILATION_MODE)",
59+
],
5260
)
5361

5462
nodejs_binary(
@@ -65,3 +73,7 @@ sh_test(
6573
],
6674
deps = ["@bazel_tools//tools/bash/runfiles"],
6775
)
76+
77+
load(":expand_into_runfiles_test.bzl", "expand_into_runfiles_test_suite")
78+
79+
expand_into_runfiles_test_suite()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"Unit tests for //internal/common:expand_into_runfiles.bzl"
2+
3+
load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
4+
load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles")
5+
6+
def _impl(ctx):
7+
env = unittest.begin(ctx)
8+
9+
conversions = {
10+
"$(location //:package.json)": "build_bazel_rules_nodejs/package.json",
11+
"$(location :a)": "build_bazel_rules_nodejs/internal/common/test/foo/bar/a.txt",
12+
"$(location params_file.spec.js)": "build_bazel_rules_nodejs/internal/common/test/params_file.spec.js",
13+
"$(locations :locations_in)": "build_bazel_rules_nodejs/package.json build_bazel_rules_nodejs/internal/common/test/foo/bar/a.txt build_bazel_rules_nodejs/internal/common/test/params_file.spec.js",
14+
"$(rootpath //:package.json)": "./package.json",
15+
"$(rootpath :a)": "internal/common/test/foo/bar/a.txt",
16+
"$(rootpath params_file.spec.js)": "internal/common/test/params_file.spec.js",
17+
"$(rootpaths :locations_in)": "./package.json internal/common/test/foo/bar/a.txt internal/common/test/params_file.spec.js",
18+
}
19+
20+
for key in conversions:
21+
asserts.equals(env, "%s" % conversions[key], expand_location_into_runfiles(ctx, "%s" % key))
22+
asserts.equals(env, " %s " % conversions[key], expand_location_into_runfiles(ctx, " %s " % key))
23+
asserts.equals(env, "%s%s" % (conversions[key], conversions[key]), expand_location_into_runfiles(ctx, "%s%s" % (key, key)))
24+
asserts.equals(env, "%s %s" % (conversions[key], conversions[key]), expand_location_into_runfiles(ctx, "%s %s" % (key, key)))
25+
asserts.equals(env, " %s %s " % (conversions[key], conversions[key]), expand_location_into_runfiles(ctx, " %s %s " % (key, key)))
26+
asserts.equals(env, "a%sb%sc" % (conversions[key], conversions[key]), expand_location_into_runfiles(ctx, "a%sb%sc" % (key, key)))
27+
28+
return unittest.end(env)
29+
30+
expand_into_runfiles_test = unittest.make(
31+
impl = _impl,
32+
attrs = {
33+
"deps": attr.label_list(default = [
34+
"//:package.json",
35+
"params_file.spec.js",
36+
":a",
37+
":locations_in",
38+
], allow_files = True),
39+
},
40+
)
41+
42+
def expand_into_runfiles_test_suite():
43+
unittest.suite("expand_into_runfiles_tests", expand_into_runfiles_test)

internal/common/test/params_file.spec.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
2020

21+
// The arguments are passed via a params file
22+
const TARGET_CPU = process.argv[2];
23+
const COMPILATION_MODE = process.argv[3];
24+
2125
// The arguments that were outputted to the params file
2226
const actual = require('fs')
2327
.readFileSync(runfiles.resolvePackageRelative('params_file.out'), 'utf-8')
@@ -26,12 +30,24 @@ const actual = require('fs')
2630
// The argument we expect to find in the params file
2731
const expected = [
2832
'some_value',
29-
// $location (expands to runfiles manifest path of format repo/path/to/file)
30-
'build_bazel_rules_nodejs/package.json',
33+
// $execpaths
34+
'./package.json',
35+
`bazel-out/${TARGET_CPU}-${COMPILATION_MODE}/bin/internal/common/test/foo/bar/a.txt`,
36+
'internal/common/test/params_file.spec.js',
37+
// $execpath
38+
'./package.json',
39+
// $rootpaths
40+
'./package.json',
41+
'internal/common/test/foo/bar/a.txt',
42+
'internal/common/test/params_file.spec.js',
43+
// $rootpath
44+
'./package.json',
3145
// $locations (expands to runfiles manifest path of format repo/path/to/file)
3246
'build_bazel_rules_nodejs/package.json',
3347
'build_bazel_rules_nodejs/internal/common/test/foo/bar/a.txt',
3448
'build_bazel_rules_nodejs/internal/common/test/params_file.spec.js',
49+
// $location (expands to runfiles manifest path of format repo/path/to/file)
50+
'build_bazel_rules_nodejs/package.json',
3551
];
3652

3753
describe('params_file', function() {

internal/golden_file_test/bin.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
const fs = require('fs');
22
const path = require('path');
3+
const runfiles = require(process.env['BAZEL_NODE_RUNFILES_HELPER']);
34

45
function main(args) {
56
const [mode, golden_no_debug, golden_debug, actual] = args;
6-
const actualPath = require.resolve(actual);
7+
const actualPath = runfiles.resolveWorkspaceRelative(actual);
78
const debugBuild = /\/bazel-out\/[^/\s]*-dbg\//.test(actualPath);
89
const golden = debugBuild ? golden_debug : golden_no_debug;
910
const actualContents = fs.readFileSync(actualPath, 'utf-8').replace(/\r\n/g, '\n');
10-
const goldenContents = fs.readFileSync(require.resolve(golden), 'utf-8').replace(/\r\n/g, '\n');
11+
const goldenContents =
12+
fs.readFileSync(runfiles.resolveWorkspaceRelative(golden), 'utf-8').replace(/\r\n/g, '\n');
1113

1214
if (actualContents !== goldenContents) {
1315
if (mode === '--out') {
1416
// Write to golden file
15-
fs.writeFileSync(require.resolve(golden), actualContents);
17+
fs.writeFileSync(runfiles.resolveWorkspaceRelative(golden), actualContents);
1618
console.error(`Replaced ${path.join(process.cwd(), golden)}`);
1719
} else if (mode === '--verify') {
1820
const unidiff = require('unidiff');

0 commit comments

Comments
 (0)