Skip to content
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

source-map-support regressed in 1.2.0 #311

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"@types/node": "18.11.9",
"date-fns": "2.29.3",
"rxjs": "7.5.7",
"source-map-support": "^0.5.21",
"typescript": "4.8.4",
"zone.js": "0.12.0"
}
Expand Down
52 changes: 52 additions & 0 deletions examples/pnpm-lock.yaml

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

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_ts/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_ts/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')
}
33 changes: 33 additions & 0 deletions examples/source_map_support/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
load("//ts:defs.bzl", "ts_project")
load("//examples/source_map_support:defs.bzl", "js_test")

ts_project(
name = "ts",
srcs = [
"a.ts",
"b.ts",
],
source_map = True,
tsconfig = {
"compilerOptions": {
"types": ["node"],
"sourceMap": True,
},
},
deps = [
"//examples:node_modules/@types/node",
],
)

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

js_test(
name = "stack_trace_support_with_chdir_test",
chdir = "examples",
data = [":ts"],
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:17)`],
)
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')
}