-
-
Notifications
You must be signed in to change notification settings - Fork 44
feat: make esbuild_bundle path-mapping-friendly #289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+247
−31
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # Path mapping: | ||
| # https://bazel.build/reference/command-line-reference#flag--experimental_output_paths | ||
| common --experimental_output_paths=strip | ||
|
|
||
| # Build without the bytes | ||
| common:ci --remote_download_outputs=minimal | ||
| common:ci --nobuild_runfile_links | ||
|
|
||
| # Override the preset's `--lockfile_mode=error` on CI since we don't | ||
| # yet commit MODULE.bazel.lock — see .gitignore. | ||
| common:ci --lockfile_mode=off |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| ../../.bazelversion |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| load("@aspect_rules_esbuild//esbuild:defs.bzl", "esbuild") | ||
| load("@bazel_skylib//rules:build_test.bzl", "build_test") | ||
|
|
||
| esbuild( | ||
| name = "bundle", | ||
| entry_point = "index.js", | ||
| ) | ||
|
|
||
| # A location expansion such as $(execpath ...) can bake a real, config-specific | ||
| # path into the action's args-file content, so this target must not advertise | ||
| # supports-path-mapping. See test.sh. | ||
| esbuild( | ||
| name = "bundle_with_location_expansion", | ||
| srcs = ["index.js"], | ||
| define = {"__PATH__": "'$(execpath index.js)'"}, | ||
| entry_point = "index.js", | ||
| ) | ||
|
|
||
| # Gives the `bazel test //...` step in CI something to run; the actual test | ||
| # assertions live in test.sh, which is run as a separate CI step. | ||
| build_test( | ||
| name = "bundle_test", | ||
| targets = [ | ||
| ":bundle", | ||
| ":bundle_with_location_expansion", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| module(name = "e2e_path_mapping") | ||
|
|
||
| bazel_dep(name = "aspect_rules_esbuild", version = "0.0.0", dev_dependency = True) | ||
| local_path_override( | ||
| module_name = "aspect_rules_esbuild", | ||
| path = "../..", | ||
| ) | ||
|
|
||
| bazel_dep(name = "bazel_lib", version = "3.0.0", dev_dependency = True) | ||
| bazel_dep(name = "bazel_skylib", version = "1.5.0", dev_dependency = True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| console.log('path mapping e2e test') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| #!/usr/bin/env bash | ||
| # Proves a few things about esbuild_bundle's `supports-path-mapping` execution | ||
| # requirement (esbuild/private/esbuild.bzl): | ||
| # | ||
| # 1. It lets Bazel's path mapping share a single cached action between two | ||
| # builds that differ only in compilation mode. | ||
| # 2. It is advertised in the ordinary case. | ||
| # 3. It is not advertised when doing so would be unsafe, i.e. when a location | ||
| # expansion in `define` produced a real, non-path-mapped path. | ||
| # | ||
| # A shared --disk_cache is required: -c opt and -c fastbuild are different | ||
| # configurations, so each gets its own action instance the first time Bazel | ||
| # visits it in a given build graph -- the incremental "did anything change" | ||
| # check within one build never gets a chance to compare across them. Only an | ||
| # explicit disk (or remote) cache lookup, keyed by the path-mapped action | ||
| # digest, can serve the second build's action from the first build's result. | ||
| set -o errexit -o nounset -o pipefail | ||
|
|
||
| cd "$(dirname "${BASH_SOURCE[0]}")" | ||
|
|
||
| scratch="$(mktemp -d)" | ||
| trap 'rm -rf "$scratch"' EXIT | ||
| disk_cache="$scratch/disk_cache" | ||
| exec_log="$scratch/exec_log.json" | ||
|
|
||
| # Force the esbuild action to be treated as new on every run of this script, | ||
| # so that a prior local build of the same target/config (e.g. from an earlier | ||
| # run of this script, or from another CI step) can't let Bazel skip it as | ||
| # already up-to-date -- we want every build here to genuinely consult (and | ||
| # thus prove something about) the shared --disk_cache. This is forwarded into | ||
| # the action's environment for free since the esbuild action already sets | ||
| # use_default_shell_env = True. | ||
| invalidate="$(date +%s)" | ||
|
|
||
| bazel build -c fastbuild //:bundle \ | ||
| --disk_cache="$disk_cache" \ | ||
| --action_env="ESBUILD_PATH_MAPPING_TEST_INVALIDATE=$invalidate" | ||
|
|
||
| bazel build -c opt //:bundle \ | ||
| --disk_cache="$disk_cache" \ | ||
| --action_env="ESBUILD_PATH_MAPPING_TEST_INVALIDATE=$invalidate" \ | ||
| --execution_log_json_file="$exec_log" | ||
|
|
||
| matches="$(jq -s '[.[] | select(.mnemonic == "esbuild")]' "$exec_log")" | ||
| count="$(echo "$matches" | jq 'length')" | ||
| if [ "$count" -eq 0 ]; then | ||
| echo "FAIL: no esbuild entry found in the -c opt execution log" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| cache_hit="$(echo "$matches" | jq -r '.[0].cacheHit')" | ||
| if [ "$cache_hit" != "true" ]; then | ||
| echo "FAIL: action was re-executed under -c opt (cacheHit=$cache_hit); path mapping did not share the cache entry from -c fastbuild" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "PASS: action was cache-shared across -c fastbuild and -c opt" | ||
|
|
||
| # We should find via bazel aquery that the action advertises path-mapping | ||
| # support. | ||
| aquery_output="$(bazel aquery 'mnemonic("esbuild", //:bundle)')" | ||
| if ! echo "$aquery_output" | grep -q "supports-path-mapping"; then | ||
| echo "FAIL: supports-path-mapping was not advertised for //:bundle" >&2 | ||
| echo "$aquery_output" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "PASS: supports-path-mapping is advertised for //:bundle" | ||
|
|
||
| # A location expansion in `define` (e.g. $(execpath ...)) can produce a real, | ||
| # config-specific path that isn't safe under path mapping, so esbuild_bundle | ||
| # must not advertise support for it in that case. | ||
| aquery_output="$(bazel aquery 'mnemonic("esbuild", //:bundle_with_location_expansion)')" | ||
| if echo "$aquery_output" | grep -q "supports-path-mapping"; then | ||
| echo "FAIL: supports-path-mapping was advertised even though define used a location expansion" >&2 | ||
| echo "$aquery_output" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "PASS: path mapping is not advertised when define uses a location expansion" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.