Skip to content

Commit

Permalink
fix: set sourcemap root to the workspace relative root_dir
Browse files Browse the repository at this point in the history
  • Loading branch information
jbedard committed Mar 1, 2023
1 parent d99e862 commit bb47c34
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 21 deletions.
1 change: 1 addition & 0 deletions examples/filegroup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ filegroup(
srcs = [
"a.ts",
"b.ts",
"sub/c.ts",
],
)

Expand Down
13 changes: 10 additions & 3 deletions examples/filegroup/check_outputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@ cd "$TEST_SRCDIR/$TEST_WORKSPACE/$(dirname $TEST_TARGET)"

grep "export var a" filegroup/a.js
grep "sourceMappingURL=a.js.map" filegroup/a.js
grep --fixed-strings '"sourceRoot":"examples/filegroup"' filegroup/a.js.map
grep --fixed-strings '"sourceRoot":"examples/filegroup/"' filegroup/a.js.map
grep --fixed-strings '"sources":["a.ts"]' filegroup/a.js.map

grep "export var b" filegroup/b.js
grep "sourceMappingURL=b.js.map" filegroup/b.js
grep --fixed-strings '"sourceRoot":"examples/filegroup"' filegroup/b.js.map
grep --fixed-strings '"sources":["b.ts"]' filegroup/b.js.map
grep --fixed-strings '"sourceRoot":"examples/filegroup/"' filegroup/b.js.map
grep --fixed-strings '"sources":["b.ts"]' filegroup/b.js.map

grep "export var c" filegroup/sub/c.js
grep "sourceMappingURL=c.js.map" filegroup/sub/c.js
grep --fixed-strings '"sourceRoot":"examples/filegroup/"' filegroup/sub/c.js.map
grep --fixed-strings '"sources":["sub/c.ts"]' filegroup/sub/c.js.map

echo "SUCCESS"
1 change: 1 addition & 0 deletions examples/filegroup/sub/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const c: string = "c";
2 changes: 1 addition & 1 deletion examples/rc/src/expected.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 13 additions & 15 deletions examples/root_dir/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
load("@aspect_rules_swc//swc:defs.bzl", "swc")
load("@aspect_bazel_lib//lib:testing.bzl", "assert_outputs")

swc(
name = "compile",
srcs = [
"src/a.ts",
"src/b.ts",
"src/sub/c.ts",
],
out_dir = "out",
root_dir = "src",
source_maps = True,
)

# Since the srcs were in a filegroup, the swc macro cannot pre-declare the outputs.
# So there is no label ":a.js" that we can reference from the build file.
# However, a.js is still produced as one of the default outputs of the compile rule.
# We can verify this in an action that depends on the ":compile" rule and reads the files.
assert_outputs(
name = "check_outputs",
actual = "compile",
expected = [
"examples/root_dir/out/a.js",
"examples/root_dir/out/a.js.map",
"examples/root_dir/out/b.js",
"examples/root_dir/out/b.js.map",
],
)
write_source_files(
name = "test",
files = {
"expected/a.js": ":out/a.js",
"expected/a.js.map": ":out/a.js.map",
"expected/b.js": ":out/b.js",
"expected/b.js.map": ":out/b.js.map",
"expected/sub/c.js": ":out/sub/c.js",
"expected/sub/c.js.map": ":out/sub/c.js.map",
},
)
3 changes: 3 additions & 0 deletions examples/root_dir/expected/a.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/root_dir/expected/a.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/root_dir/expected/b.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/root_dir/expected/b.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions examples/root_dir/expected/sub/c.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/root_dir/expected/sub/c.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/root_dir/src/sub/c.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const c: string = "c";
9 changes: 7 additions & 2 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"Internal implementation details"

load("@aspect_bazel_lib//lib:platform_utils.bzl", "platform_utils")
load("@aspect_bazel_lib//lib:paths.bzl", "relative_file")
load("@aspect_rules_js//js:libs.bzl", "js_lib_helpers")
load("@aspect_rules_js//js:providers.bzl", "js_info")
load("@bazel_skylib//lib:paths.bzl", "paths")
Expand Down Expand Up @@ -244,10 +245,14 @@ def _impl(ctx):
else:
output_sources = []

build_dir = ctx.label.package
root_dir = paths.join(build_dir, ctx.attr.root_dir)
root_dir_file = paths.join(root_dir, "x")

for src in ctx.files.srcs:
src_args = ctx.actions.args()
src_args.add("--source-file-name", src.basename)
src_args.add("--source-root", src.dirname)
src_args.add("--source-file-name", relative_file(src.path, root_dir_file))
src_args.add("--source-root", root_dir)

src_path = _relative_to_package(src.path, ctx)

Expand Down

0 comments on commit bb47c34

Please sign in to comment.