Skip to content

Commit

Permalink
fix(builtin): fix linker bug when there are no third-party modules
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored and alexeagle committed Jul 1, 2020
1 parent 91d81b3 commit becd9bc
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
4 changes: 2 additions & 2 deletions internal/linker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -341,8 +341,8 @@ function main(args, runfiles) {
log_verbose('resolved node_modules root', root, 'to', rootDir);
log_verbose('cwd', process.cwd());
if (!(yield exists(rootDir))) {
log_verbose('no third-party packages; mkdir node_modules at ', root);
yield fs.promises.mkdir(rootDir);
log_verbose('no third-party packages; mkdir node_modules at', root);
yield mkdirp(rootDir);
}
yield symlink(rootDir, 'node_modules');
process.chdir(rootDir);
Expand Down
4 changes: 2 additions & 2 deletions internal/linker/link_node_modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,8 +556,8 @@ export async function main(args: string[], runfiles: Runfiles) {
// Create rootDir if it does not exists. This will be the case if there are no third-party deps
// for this target or if outside of the sandbox and there are no node_modules installed.
if (!(await exists(rootDir))) {
log_verbose('no third-party packages; mkdir node_modules at ', root);
await fs.promises.mkdir(rootDir);
log_verbose('no third-party packages; mkdir node_modules at', root);
await mkdirp(rootDir);
}

// Create the node_modules symlink to the node_modules root that node will resolve from
Expand Down
11 changes: 11 additions & 0 deletions internal/linker/test/no_npm_deps/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load(":no_npm_deps.bzl", "no_npm_deps")

# Tests the
# ```
# [link_node_modules.js] no third-party packages; mkdir node_modules at npm/node_modules
# ```
# path in the linker
no_npm_deps(
name = "no_npm_deps_linker_test",
src = "input.js",
)
1 change: 1 addition & 0 deletions internal/linker/test/no_npm_deps/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('here');
42 changes: 42 additions & 0 deletions internal/linker/test/no_npm_deps/no_npm_deps.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
"""
This rule runs terser executable directly via ctx.actions.run instead of using the run_node function.
This ensures that there are no third-party npm deps in runfiles since run_node will add any
NodeRuntimeDepsInfo deps from the executable terser.
"""

_ATTRS = {
"src": attr.label(
allow_single_file = True,
mandatory = True,
),
"terser": attr.label(
executable = True,
cfg = "host",
default = Label("@npm//terser/bin:terser"),
),
}

_OUTPUTS = {
"minified": "%{name}.js",
}

def _impl(ctx):
args = ctx.actions.args()
args.add(ctx.file.src.path)
args.add_all(["--output", ctx.outputs.minified.path])

ctx.actions.run(
progress_message = "Optimizing JavaScript %s [terser]" % ctx.outputs.minified.short_path,
executable = ctx.executable.terser,
inputs = [ctx.file.src],
outputs = [ctx.outputs.minified],
arguments = [args],
)

return [DefaultInfo()]

no_npm_deps = rule(
implementation = _impl,
attrs = _ATTRS,
outputs = _OUTPUTS,
)

0 comments on commit becd9bc

Please sign in to comment.