Skip to content

Commit

Permalink
Add support for module name mapping and unknown source files in stack…
Browse files Browse the repository at this point in the history
… trace (#21)

Allows debugger to function when some modules can't be mapped
  • Loading branch information
rlandav authored Oct 4, 2022
1 parent 8f6db95 commit 074030d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@
## Version 0.5.0 (September 2022)

- Changes to the source path location in Minecraft require that localRoot points to a directory with sources, not the pack root. Update your launch.json 'localRoot'.

## Version 0.6.0 (September 2022)

- Add support for external modules while debugging. For JS modules built into Minecraft, stack will be visible but listed as "unknown". Add configuration support for module name mapping.
4 changes: 2 additions & 2 deletions package-lock.json

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

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "minecraft-debugger",
"displayName": "Minecraft Bedrock Edition Debugger",
"description": "Debug your JavaScript code running as part of the GameTest Framework experimental feature in Minecraft Bedrock Edition.",
"version": "0.5.0",
"version": "0.6.0",
"publisher": "mojang-studios",
"author": {
"name": "Mojang Studios"
Expand Down Expand Up @@ -84,6 +84,10 @@
"inputPort": {
"type": "string",
"description": "Prompts for a port at launch."
},
"moduleMapping": {
"type": "object",
"description": "Module mapping for imports. Each key is an import name that will be mapped to the provided value. Used if modules are external (i.e. included as part of minecraft). Defaults to an empty object."
}
}
}
Expand Down Expand Up @@ -149,4 +153,4 @@
"vsce": "^1.93.0",
"vscode-test": "^1.5.0"
}
}
}
15 changes: 11 additions & 4 deletions src/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ interface PendingResponse {
reject: Function;
}

// Module mapping for getting line numbers for a given module
interface ModuleMapping {
[moduleName: string]: string;
}

// Interface for specific launch arguments.
// See package.json for schema.
interface IAttachRequestArguments extends DebugProtocol.AttachRequestArguments {
Expand All @@ -26,6 +31,7 @@ interface IAttachRequestArguments extends DebugProtocol.AttachRequestArguments {
host: string;
port: number;
inputPort: string;
moduleMapping: ModuleMapping;
}

// The Debug Adapter for 'minecraft-js'
Expand All @@ -46,6 +52,7 @@ export class Session extends DebugSession {
private _localRoot: string = "";
private _sourceMapRoot?: string;
private _generatedSourceRoot?: string;
private _moduleMapping?: ModuleMapping;

public constructor() {
super();
Expand Down Expand Up @@ -88,6 +95,7 @@ export class Session extends DebugSession {
this._localRoot = args.localRoot ? path.normalize(args.localRoot) : "";
this._sourceMapRoot = args.sourceMapRoot ? path.normalize(args.sourceMapRoot) : undefined;
this._generatedSourceRoot = args.generatedSourceRoot ? path.normalize(args.generatedSourceRoot) : undefined;
this._moduleMapping = args.moduleMapping;

// Listen or connect (default), depending on mode.
// Attach makes more sense to use connect, but some MC platforms require using listen.
Expand Down Expand Up @@ -191,19 +199,18 @@ export class Session extends DebugSession {

const stackFrames: StackFrame[] = [];
for (const { id, name, filename, line, column } of stacksBody) {
const mappedFilename = this._moduleMapping?.[filename] ?? filename;
try {
const originalLocation = await this._sourceMaps.getOriginalPositionFor({
source: filename,
source: mappedFilename,
line: line || 0,
column: column || 0
});
const source = new Source(path.basename(originalLocation.source), originalLocation.source);
stackFrames.push(new StackFrame(id, name, source, originalLocation.line, originalLocation.column));
}
catch (e) {
this.log((e as Error).message, LogLevel.Error);
this.sendErrorResponse(response, 1003, `Failed to get stack trace for ${filename} at line ${line}.`);
return;
stackFrames.push(new StackFrame(id, name));
}
}

Expand Down

0 comments on commit 074030d

Please sign in to comment.