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 2, 2023
1 parent d893b1e commit fac5edd
Show file tree
Hide file tree
Showing 21 changed files with 200 additions and 50 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":""' 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":""' 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":""' filegroup/sub/c.js.map
grep --fixed-strings '"sources":["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";
5 changes: 4 additions & 1 deletion examples/package.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"private": true
"private": true,
"dependencies": {
"source-map-support": "^0.5.21"
}
}
60 changes: 31 additions & 29 deletions examples/pnpm-lock.yaml

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

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";
11 changes: 11 additions & 0 deletions examples/source_map_support/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("@aspect_bazel_lib//lib:copy_to_bin.bzl", "copy_to_bin")

exports_files([
"defs.bzl",
])

copy_to_bin(
name = "stack-trace-support",
srcs = ["stack-trace-support.js"],
visibility = ["//visibility:public"],
)
28 changes: 28 additions & 0 deletions examples/source_map_support/defs.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""
Macro wrappers around rules_js's `js_binary` and `js_test` that improve the DX of stack traces by automatically
registering source-map-support and removing the runfiles directory prefix.
Use them wherever you would use rules_js's `js_binary` and `js_test`.
"""

load("@aspect_rules_js//js:defs.bzl", _js_binary = "js_binary", _js_test = "js_test")

def js_binary(data = [], node_options = [], **kwargs):
_js_binary(
data = [
"//examples:node_modules/source-map-support",
"//examples/source_map_support:stack-trace-support",
] + data,
node_options = ["--require", "$$RUNFILES/aspect_rules_swc/examples/source_map_support/stack-trace-support"] + node_options,
**kwargs
)

def js_test(data = [], node_options = [], **kwargs):
_js_test(
data = [
"//examples:node_modules/source-map-support",
"//examples/source_map_support:stack-trace-support",
] + data,
node_options = ["--require", "$$RUNFILES/aspect_rules_swc/examples/source_map_support/stack-trace-support"] + node_options,
**kwargs
)
42 changes: 42 additions & 0 deletions examples/source_map_support/stack-trace-support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// See defs.bzl for where this is used and what it does.

require('source-map-support/register')

let basePath = process.env.RUNFILES
? `${process.env.RUNFILES}/${process.env.JS_BINARY__WORKSPACE}`
: process.cwd()

if (!basePath.endsWith('/')) {
basePath = basePath + '/'
}

/*
Before:
Error: test
at foo (/private/var/tmp/_bazel_john/67beefda950d56283b98d96980e6e332/execroot/figma/bazel-out/darwin_arm64-fastbuild/bin/bazel/js/test/stack_trace_support.sh.runfiles/figma/bazel/js/test/b.js:2:11)
at Object.<anonymous> (/private/var/tmp/_bazel_john/67beefda950d56283b98d96980e6e332/execroot/figma/bazel-out/darwin_arm64-fastbuild/bin/bazel/js/test/stack_trace_support.sh.runfiles/figma/bazel/js/test/a.js:4:1)
...
After:
Error: test
at foo (bazel/js/test/b.ts:2:9)
at Object.<anonymous> (bazel/js/test/a.ts:5:1)
...
*/

const basePathRegex = new RegExp(
`(at | \\()${basePath
.replace(/\\/g, '/')
// Escape regex meta-characters.
.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&')
.replace(/-/g, '\\x2d')}`,
'g',
)

const prepareStackTrace = Error.prepareStackTrace
Error.prepareStackTrace = function (error, stack) {
return prepareStackTrace(error, stack)
.split('\n')
.map((line) => line.replace(basePathRegex, '$1'))
.join('\n')
}
24 changes: 24 additions & 0 deletions examples/source_map_support/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
load("@aspect_rules_swc//swc:defs.bzl", "swc")
load("//examples/source_map_support:defs.bzl", "js_test")

swc(
name = "compile",
srcs = [
"a.ts",
"b.ts",
],
source_maps = True,
)

js_test(
name = "stack_trace_support_test",
data = [":compile"],
entry_point = ":a.js",
)

js_test(
name = "stack_trace_support_with_chdir_test",
chdir = "examples",
data = [":compile"],
entry_point = ":a.js",
)
17 changes: 17 additions & 0 deletions examples/source_map_support/test/a.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
try {
require('./b')()
} catch (e) {
const assert = require('assert')
const frames = e.stack
.split('\n')
.slice(1)
.map((s) => s.trim())
assert.deepEqual(
frames.filter((f) => f.includes('source_map_support/test/a')),
[`at Object.<anonymous> (examples/source_map_support/test/a.ts:2:11)`],
)
assert.deepEqual(
frames.filter((f) => f.includes('source_map_support/test/b')),
[`at foo (examples/source_map_support/test/b.ts:2:9)`],
)
}
3 changes: 3 additions & 0 deletions examples/source_map_support/test/b.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function foo() {
throw new Error('test')
}
2 changes: 1 addition & 1 deletion swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ def _impl(ctx):
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 if ctx.attr.source_root == "" else ctx.attr.source_root)
src_args.add("--source-root", "" if ctx.attr.source_root == "" else ctx.attr.source_root)

src_path = _relative_to_package(src.path, ctx)

Expand Down

0 comments on commit fac5edd

Please sign in to comment.