Skip to content

Commit

Permalink
fix(esbuild): prefer finding entry_point files in deps rather than sr…
Browse files Browse the repository at this point in the history
…cs (#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.
  • Loading branch information
mattem authored May 25, 2021
1 parent 936ed97 commit dd4c4f3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/esbuild/esbuild.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
41 changes: 41 additions & 0 deletions packages/esbuild/test/js-library/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -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",
)
1 change: 1 addition & 0 deletions packages/esbuild/test/js-library/lib.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const NAME = 'rules_nodejs';
3 changes: 3 additions & 0 deletions packages/esbuild/test/js-library/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {NAME as name} from './lib';

console.log(name);
1 change: 1 addition & 0 deletions packages/esbuild/test/js-library/out.golden.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rules_nodejs

0 comments on commit dd4c4f3

Please sign in to comment.