Skip to content

Commit becd9bc

Browse files
gregmagolanalexeagle
authored andcommitted
fix(builtin): fix linker bug when there are no third-party modules
1 parent 91d81b3 commit becd9bc

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

internal/linker/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ function main(args, runfiles) {
341341
log_verbose('resolved node_modules root', root, 'to', rootDir);
342342
log_verbose('cwd', process.cwd());
343343
if (!(yield exists(rootDir))) {
344-
log_verbose('no third-party packages; mkdir node_modules at ', root);
345-
yield fs.promises.mkdir(rootDir);
344+
log_verbose('no third-party packages; mkdir node_modules at', root);
345+
yield mkdirp(rootDir);
346346
}
347347
yield symlink(rootDir, 'node_modules');
348348
process.chdir(rootDir);

internal/linker/link_node_modules.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -556,8 +556,8 @@ export async function main(args: string[], runfiles: Runfiles) {
556556
// Create rootDir if it does not exists. This will be the case if there are no third-party deps
557557
// for this target or if outside of the sandbox and there are no node_modules installed.
558558
if (!(await exists(rootDir))) {
559-
log_verbose('no third-party packages; mkdir node_modules at ', root);
560-
await fs.promises.mkdir(rootDir);
559+
log_verbose('no third-party packages; mkdir node_modules at', root);
560+
await mkdirp(rootDir);
561561
}
562562

563563
// Create the node_modules symlink to the node_modules root that node will resolve from
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
load(":no_npm_deps.bzl", "no_npm_deps")
2+
3+
# Tests the
4+
# ```
5+
# [link_node_modules.js] no third-party packages; mkdir node_modules at npm/node_modules
6+
# ```
7+
# path in the linker
8+
no_npm_deps(
9+
name = "no_npm_deps_linker_test",
10+
src = "input.js",
11+
)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('here');
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
This rule runs terser executable directly via ctx.actions.run instead of using the run_node function.
3+
This ensures that there are no third-party npm deps in runfiles since run_node will add any
4+
NodeRuntimeDepsInfo deps from the executable terser.
5+
"""
6+
7+
_ATTRS = {
8+
"src": attr.label(
9+
allow_single_file = True,
10+
mandatory = True,
11+
),
12+
"terser": attr.label(
13+
executable = True,
14+
cfg = "host",
15+
default = Label("@npm//terser/bin:terser"),
16+
),
17+
}
18+
19+
_OUTPUTS = {
20+
"minified": "%{name}.js",
21+
}
22+
23+
def _impl(ctx):
24+
args = ctx.actions.args()
25+
args.add(ctx.file.src.path)
26+
args.add_all(["--output", ctx.outputs.minified.path])
27+
28+
ctx.actions.run(
29+
progress_message = "Optimizing JavaScript %s [terser]" % ctx.outputs.minified.short_path,
30+
executable = ctx.executable.terser,
31+
inputs = [ctx.file.src],
32+
outputs = [ctx.outputs.minified],
33+
arguments = [args],
34+
)
35+
36+
return [DefaultInfo()]
37+
38+
no_npm_deps = rule(
39+
implementation = _impl,
40+
attrs = _ATTRS,
41+
outputs = _OUTPUTS,
42+
)

0 commit comments

Comments
 (0)