-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add troubleshooting note and example of using node_options for …
…nicer stack traces (#2072)
- Loading branch information
Showing
12 changed files
with
133 additions
and
6 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
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
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
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("@aspect_rules_js//js:defs.bzl", "js_library", "js_test") | ||
load("@npm//:defs.bzl", "npm_link_all_packages") | ||
|
||
npm_link_all_packages(name = "node_modules") | ||
|
||
js_test( | ||
name = "stack_traces", | ||
data = [ | ||
":library", | ||
|
||
# Include the stack-traces library - this could be wrapped in a `js_test` macro | ||
":stack-traces", | ||
], | ||
entry_point = "main.js", | ||
node_options = [ | ||
# Load the stack-traces library at node startup time - this could be wrapped in a `js_test` macro | ||
"--require", | ||
"$$JS_BINARY__RUNFILES/$$JS_BINARY__WORKSPACE/examples/stack_traces/stacks.cjs", | ||
], | ||
) | ||
|
||
js_library( | ||
name = "library", | ||
srcs = glob(["lib/**/*.js"]), | ||
) | ||
|
||
# Put the stacks.cjs in a `js_library` that can also declare any dependencies that | ||
# the --require script may have such as the source-map-support package. | ||
js_library( | ||
name = "stack-traces", | ||
srcs = ["stacks.cjs"], | ||
deps = [":node_modules/source-map-support"], | ||
) |
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,7 @@ | ||
module.exports.a = function a() { | ||
return b() | ||
} | ||
|
||
function b() { | ||
throw new Error('the error') | ||
} |
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,12 @@ | ||
const { a } = require('./lib/a') | ||
|
||
try { | ||
a() | ||
throw new Error('a() should throw for this test') | ||
} catch (e) { | ||
if (e.stack.indexOf('.runfiles/') !== -1) { | ||
throw new Error( | ||
'stacks.cjs should have stripped runfiles directories from the stack trace' | ||
) | ||
} | ||
} |
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,5 @@ | ||
{ | ||
"devDependencies": { | ||
"source-map-support": "^0.5.21" | ||
} | ||
} |
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,41 @@ | ||
// Register source-map-support for source maps to be applied on stack traces. | ||
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_username/67beefda950d56283b98d96980e6e332/execroot/aspect_rules_js/bazel-out/darwin_arm64-fastbuild/bin/examples/stack_traces/stack_trace_support.sh.runfiles/aspect_rules_js/examples/stack_traces/b.js:2:11) | ||
at Object.<anonymous> (/private/var/tmp/_bazel_username/67beefda950d56283b98d96980e6e332/execroot/aspect_rules_js/bazel-out/darwin_arm64-fastbuild/bin/examples/stack_traces/stack_trace_support.sh.runfiles/aspect_rules_js/examples/stack_traces/a.js:4:1) | ||
... | ||
After: | ||
Error: test | ||
at foo (examples/stack_traces/b.ts:2:9) | ||
at Object.<anonymous> (examples/stack_traces/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') | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.