generated from bazel-contrib/rules-template
-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Co-authored-by: Sahin Yort <[email protected]>
- Loading branch information
1 parent
18dc100
commit e6b7fa2
Showing
8 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 @@ | ||
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"], | ||
) |
This file contains 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,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 | ||
) |
This file contains 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,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') | ||
} |
This file contains 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,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", | ||
) |
This file contains 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,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)`], | ||
) | ||
} |
This file contains 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,3 @@ | ||
module.exports = function foo() { | ||
throw new Error('test') | ||
} |