-
Notifications
You must be signed in to change notification settings - Fork 302
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
MISC: Improve stack trace of errors in transformed scripts #1951
Draft
catloversg
wants to merge
3
commits into
bitburner-official:dev
Choose a base branch
from
catloversg:pull-request/misc/improve-stack-trace-of-errors-in-transformed-scripts
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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,111 @@ | ||
import ErrorStackParser from "error-stack-parser"; | ||
import { type RawSourceMap, SourceMapConsumer } from "source-map-js"; | ||
|
||
/** | ||
* parseStackTrace uses some properties of workerScript, but the dependency chain of WorkerScript is long. In order to | ||
* avoid worsening the dependency chain of parseStackTrace's callers, we create this minimal version of WorkerScript's | ||
* type. It violates the DRY principle, but it's worth the trouble: | ||
* - We rarely change WorkerScript. | ||
* - The entire codebase is riddled with massive dependency chains. We should avoid worsening the situation. | ||
*/ | ||
export type LiteWorkerScript = { | ||
hostname: string; | ||
scriptRef: { | ||
dependencies: Map< | ||
unknown, | ||
{ | ||
filename: string; | ||
mod: { | ||
sourceMap?: string; | ||
} | null; | ||
} | ||
>; | ||
}; | ||
}; | ||
|
||
/** | ||
* This function parses the stack trace of the error and returns only stack lines in the player's scripts. With | ||
* transformed scripts, it also parses the source map to show the original lines/columns of the original scripts. | ||
* | ||
* For example: This stack: | ||
* | ||
* at errorMessage (webpack://bitburner/./src/Netscript/ErrorMessages.ts?:35:97) | ||
* at Object.getServer (webpack://bitburner/./src/Netscript/NetscriptHelpers.tsx?:420:72) | ||
* at eval (webpack://bitburner/./src/NetscriptFunctions.ts?:889:86) | ||
* at Proxy.wrappedFunction (webpack://bitburner/./src/Netscript/APIWrapper.ts?:67:16) | ||
* at test1 (home/a.ts:10:8) | ||
* at main (home/a.ts:23:5) | ||
* at startNetscript2Script (webpack://bitburner/./src/NetscriptWorker.ts?:91:9) | ||
* | ||
* Becomes: | ||
* | ||
* at test1 (home/a.ts:11:5) | ||
* at main (home/a.ts:26:2) | ||
* | ||
* There are 2 changes: | ||
* - All stack lines pointing to our codebase are stripped. | ||
* - Stack lines show original lines/columns (e.g., a.ts:23:5 -> home/a.ts:26:2). | ||
*/ | ||
export function parseStackTrace(error: Error, workerScript: LiteWorkerScript): string { | ||
const stackFrames = ErrorStackParser.parse(error); | ||
const stackLines = [error.message]; | ||
// Cache of found source maps. | ||
const sourceMaps = new Map<string, string>(); | ||
for (const stackFrame of stackFrames) { | ||
if (!stackFrame.fileName) { | ||
continue; | ||
} | ||
/** | ||
* Filename in the stack line is actually `${hostname}/${filename}`. Check sourceURL in generateLoadedModule | ||
* (src\NetscriptJSEvaluator.ts). | ||
*/ | ||
if (!stackFrame.fileName.startsWith(workerScript.hostname)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't these mismatch due to module sharing across hosts? |
||
continue; | ||
} | ||
const fileName = stackFrame.fileName.replace(`${workerScript.hostname}/`, ""); | ||
if (!fileName) { | ||
continue; | ||
} | ||
let line = stackFrame.lineNumber; | ||
let column = stackFrame.columnNumber; | ||
if (line !== undefined && column !== undefined) { | ||
let sourceMap = sourceMaps.get(fileName); | ||
// If the source map is not in the cache, we try to find it. | ||
if (!sourceMap) { | ||
/** | ||
* workerScript.scriptRef.dependencies contains directly or indirectly import, including the script itself. | ||
* Check Script in src\Script\Script.ts. | ||
*/ | ||
for (const script of workerScript.scriptRef.dependencies.values()) { | ||
// Find the current script in workerScript.scriptRef.dependencies. | ||
if (script.filename !== fileName) { | ||
continue; | ||
} | ||
// Check if sourceMap exists. | ||
if (script.mod?.sourceMap) { | ||
sourceMap = script.mod.sourceMap; | ||
// Put it in the cache. | ||
sourceMaps.set(fileName, sourceMap); | ||
} | ||
break; | ||
} | ||
} | ||
// Parse the source map if it exists. | ||
if (sourceMap) { | ||
/** | ||
* SourceMap is generated by SWC, so we assume that it's valid. Validating it with ajv is unnecessary. If there | ||
* are bugs in SWC or source-map-js, the try-catch block will ensure that the game won't crash. | ||
*/ | ||
try { | ||
const sourceMapConsumer = new SourceMapConsumer(JSON.parse(sourceMap) as RawSourceMap); | ||
({ line, column } = sourceMapConsumer.originalPositionFor({ line, column })); | ||
} catch (errorParsingSourceMap) { | ||
console.error(errorParsingSourceMap); | ||
console.error(`Cannot parse map of ${fileName} in ${workerScript.hostname}. Source map: ${sourceMap}`); | ||
} | ||
} | ||
} | ||
stackLines.push(` at ${stackFrame.functionName} (${workerScript.hostname}/${fileName}:${line}:${column})`); | ||
} | ||
return stackLines.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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Most of this comment is no longer relevant. However, in general we shouldn't be logging things to console, so I think some explanation is still warranted - specifically the bit about how the links are still helpful with the built-in developer tools. (Without that, I don't think it would be worth doing this here.)