From becd9bc4aa1031ced6620c488a97442469eaee7d Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Tue, 30 Jun 2020 14:53:38 -0700 Subject: [PATCH] fix(builtin): fix linker bug when there are no third-party modules --- internal/linker/index.js | 4 +- internal/linker/link_node_modules.ts | 4 +- internal/linker/test/no_npm_deps/BUILD.bazel | 11 +++++ internal/linker/test/no_npm_deps/input.js | 1 + .../linker/test/no_npm_deps/no_npm_deps.bzl | 42 +++++++++++++++++++ 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 internal/linker/test/no_npm_deps/BUILD.bazel create mode 100644 internal/linker/test/no_npm_deps/input.js create mode 100644 internal/linker/test/no_npm_deps/no_npm_deps.bzl diff --git a/internal/linker/index.js b/internal/linker/index.js index a020394d67..805544510e 100644 --- a/internal/linker/index.js +++ b/internal/linker/index.js @@ -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); diff --git a/internal/linker/link_node_modules.ts b/internal/linker/link_node_modules.ts index bade9338ac..5c2f5237e2 100644 --- a/internal/linker/link_node_modules.ts +++ b/internal/linker/link_node_modules.ts @@ -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 diff --git a/internal/linker/test/no_npm_deps/BUILD.bazel b/internal/linker/test/no_npm_deps/BUILD.bazel new file mode 100644 index 0000000000..bc7466585c --- /dev/null +++ b/internal/linker/test/no_npm_deps/BUILD.bazel @@ -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", +) diff --git a/internal/linker/test/no_npm_deps/input.js b/internal/linker/test/no_npm_deps/input.js new file mode 100644 index 0000000000..60d0eb835a --- /dev/null +++ b/internal/linker/test/no_npm_deps/input.js @@ -0,0 +1 @@ +console.log('here'); \ No newline at end of file diff --git a/internal/linker/test/no_npm_deps/no_npm_deps.bzl b/internal/linker/test/no_npm_deps/no_npm_deps.bzl new file mode 100644 index 0000000000..7328acc931 --- /dev/null +++ b/internal/linker/test/no_npm_deps/no_npm_deps.bzl @@ -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, +)