From dd4c4f3dfd5a312ec13fd14068380a771fcfd94d Mon Sep 17 00:00:00 2001 From: Matt Mackay Date: Tue, 25 May 2021 17:53:23 -0400 Subject: [PATCH] fix(esbuild): prefer finding entry_point files in deps rather than srcs (#2692) When using esbuild with `js_library`, the entry_point may be part of the deps, therefore the path will end up in `bazel-out/` inside the sandbox where esbuild is running, however the entrypoint path will be in the sources path, leaving any relative path inputs to no longer be relative to the entrypoint file. The reordering here forces the `resolve_entry_point` method to prefer the file that resides in the `bazel-out/` path in the sandbox. A possible future breaking change would be to force users to list the entrypoint file(s) in either srcs or deps for the correct pathing, or remove `srcs` from esbuild, meaning all input files must be in the output tree. --- packages/esbuild/esbuild.bzl | 4 +- packages/esbuild/test/js-library/BUILD.bazel | 41 +++++++++++++++++++ packages/esbuild/test/js-library/lib.jsx | 1 + packages/esbuild/test/js-library/main.js | 3 ++ .../esbuild/test/js-library/out.golden.txt | 1 + 5 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 packages/esbuild/test/js-library/BUILD.bazel create mode 100644 packages/esbuild/test/js-library/lib.jsx create mode 100644 packages/esbuild/test/js-library/main.js create mode 100644 packages/esbuild/test/js-library/out.golden.txt diff --git a/packages/esbuild/esbuild.bzl b/packages/esbuild/esbuild.bzl index 7bc2054a12..ba9b0cebee 100644 --- a/packages/esbuild/esbuild.bzl +++ b/packages/esbuild/esbuild.bzl @@ -41,7 +41,9 @@ def _esbuild_impl(ctx): entry_points = desugar_entry_point_names(ctx.file.entry_point, ctx.files.entry_points) deps_inputs = depset(transitive = deps_depsets).to_list() - inputs = filter_files(entry_points) + ctx.files.srcs + deps_inputs + + # TODO(mattem): 4.0.0 breaking change, entry_points must exist in deps, and are not considered additional srcs + inputs = deps_inputs + ctx.files.srcs + filter_files(entry_points) metafile = ctx.actions.declare_file("%s_metadata.json" % ctx.attr.name) outputs = [metafile] diff --git a/packages/esbuild/test/js-library/BUILD.bazel b/packages/esbuild/test/js-library/BUILD.bazel new file mode 100644 index 0000000000..508e65d9da --- /dev/null +++ b/packages/esbuild/test/js-library/BUILD.bazel @@ -0,0 +1,41 @@ +load("//:index.bzl", "generated_file_test", "js_library", "nodejs_binary", "npm_package_bin") +load("//packages/esbuild/test:tests.bzl", "esbuild") + +js_library( + name = "lib", + srcs = ["lib.jsx"], +) + +js_library( + name = "main", + srcs = ["main.js"], + deps = [":lib"], +) + +esbuild( + name = "bundle", + entry_point = "main.js", + deps = [ + ":main", + ], +) + +nodejs_binary( + name = "bin", + data = [ + ":bundle", + ], + entry_point = "bundle.js", +) + +npm_package_bin( + name = "runner", + stdout = "out.txt", + tool = ":bin", +) + +generated_file_test( + name = "test", + src = "out.golden.txt", + generated = "out.txt", +) diff --git a/packages/esbuild/test/js-library/lib.jsx b/packages/esbuild/test/js-library/lib.jsx new file mode 100644 index 0000000000..df4f3fbc6e --- /dev/null +++ b/packages/esbuild/test/js-library/lib.jsx @@ -0,0 +1 @@ +export const NAME = 'rules_nodejs'; \ No newline at end of file diff --git a/packages/esbuild/test/js-library/main.js b/packages/esbuild/test/js-library/main.js new file mode 100644 index 0000000000..c3648e3162 --- /dev/null +++ b/packages/esbuild/test/js-library/main.js @@ -0,0 +1,3 @@ +import {NAME as name} from './lib'; + +console.log(name); \ No newline at end of file diff --git a/packages/esbuild/test/js-library/out.golden.txt b/packages/esbuild/test/js-library/out.golden.txt new file mode 100644 index 0000000000..0c23e83a65 --- /dev/null +++ b/packages/esbuild/test/js-library/out.golden.txt @@ -0,0 +1 @@ +rules_nodejs