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 3, 2023
1 parent 020f85b commit c52fdc0
Show file tree
Hide file tree
Showing 26 changed files with 212 additions and 41 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
11 changes: 8 additions & 3 deletions examples/filegroup/check_outputs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ 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
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";
3 changes: 3 additions & 0 deletions examples/out_dir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ swc(
srcs = [
"a.ts",
"b.ts",
"sub/c.ts",
],
out_dir = "out",
source_maps = True,
Expand All @@ -19,5 +20,7 @@ write_source_files(
"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",
},
)
2 changes: 1 addition & 1 deletion examples/out_dir/expected/a.js.map

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

2 changes: 1 addition & 1 deletion examples/out_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/out_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/out_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/out_dir/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.

1 change: 1 addition & 0 deletions examples/root_dir/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ swc(
srcs = [
"src/a.ts",
"src/b.ts",
"src/sub/c.ts",
],
out_dir = "out",
root_dir = "src",
Expand Down
2 changes: 1 addition & 1 deletion examples/root_dir/expected/a.js.map

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

2 changes: 1 addition & 1 deletion 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 examples/source_root/expected_subdir.js.map

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

24 changes: 22 additions & 2 deletions swc/private/swc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,25 @@ def _calculate_map_out(src, source_maps, out_dir = None, root_dir = None):
def _calculate_map_outs(srcs, source_maps, out_dir = None, root_dir = None):
return [f2 for f2 in [_calculate_map_out(f, source_maps, out_dir, root_dir) for f in srcs] if f2]

def _calculate_source_file(ctx, src):
if not (ctx.attr.out_dir or ctx.attr.root_dir):
return src.basename

src_pkg = src.dirname[len(ctx.label.package)+1:] if ctx.label.package else ""
s = ""

# out of src subdir
if src_pkg:
s = paths.join(s, "/".join([".." for _ in src_pkg.split("/")]))

# out of the out dir
if ctx.attr.out_dir:
s = paths.join(s, "/".join([".." for _ in ctx.attr.out_dir.split("/")]))

# back into the src dir, including into the root_dir that was stripped if it exists
return paths.join(s, src_pkg, src.basename)


def _swc_action(ctx, swc_binary, **kwargs):
# Workaround Rust SDK issue on Windows, see https://github.com/aspect-build/rules_swc/issues/141
if platform_utils.host_platform_is_windows():
Expand Down Expand Up @@ -255,8 +274,9 @@ 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", ctx.attr.source_root or src.dirname)
src_args.add("--source-file-name", _calculate_source_file(ctx, src))
src_args.add("--source-root", ctx.attr.source_root)

src_path = _relative_to_package(src.path, ctx)

js_out_path = _calculate_js_out(src_path, ctx.attr.out_dir, ctx.attr.root_dir, [_relative_to_package(f.path, ctx) for f in ctx.outputs.js_outs])
Expand Down

0 comments on commit c52fdc0

Please sign in to comment.