Skip to content

Commit a94ee75

Browse files
authored
Better TypeScript debugging, support for alternate workflows other than MC's gulp template. (#13)
1 parent 5f3422b commit a94ee75

File tree

5 files changed

+187
-142
lines changed

5 files changed

+187
-142
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,7 @@
1111
## Version 0.2.0 (April 2022)
1212

1313
- Support for TypeScript debugging.
14+
15+
## Version 0.3.0 (April 2022)
16+
17+
- Better TypeScript support.

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "minecraft-debugger",
33
"displayName": "Minecraft Bedrock Edition Debugger",
44
"description": "Debug your JavaScript code running as part of the GameTest Framework experimental feature in Minecraft Bedrock Edition.",
5-
"version": "0.2.0",
5+
"version": "0.3.0",
66
"publisher": "mojang-studios",
77
"author": {
88
"name": "Mojang Studios"
@@ -12,7 +12,7 @@
1212
"type": "git",
1313
"url": "https://github.com/Mojang/minecraft-debugger.git"
1414
},
15-
"icon": "bedrock-icon.png",
15+
"icon": "bedrock-icon.png",
1616
"engines": {
1717
"vscode": "^1.55.0"
1818
},
@@ -64,14 +64,13 @@
6464
"description": "The local root of the Minecraft Add-On.",
6565
"default": "${workspaceFolder}/"
6666
},
67-
"remoteRoot": {
67+
"sourceMapRoot": {
6868
"type": "string",
69-
"description": "The remote root of the Minecraft Add-On."
69+
"description": "The location of the source maps."
7070
},
71-
"sourceMapRoot": {
71+
"generatedSourceRoot": {
7272
"type": "string",
73-
"description": "The local output folder for source maps.",
74-
"default": "{workspaceFolder}/"
73+
"description": "The location of the generated source files (js). Not required if same as source maps."
7574
},
7675
"host": {
7776
"type": "string",

src/MCConfigProvider.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,8 @@ export class MCConfigProvider implements vscode.DebugConfigurationProvider {
2323
if (!config.localRoot) {
2424
config.localRoot = "${workspaceFolder}/";
2525
}
26-
27-
// default to local root
28-
if (!config.remoteRoot) {
29-
config.remoteRoot = config.localRoot;
30-
}
31-
32-
// if no port, then set a command string that will trigger a user input dialog
3326
if (!config.port) {
34-
config.inputPort = "${command:PromptForPort}";
27+
config.inputPort = "${command:PromptForPort}"; // prompt user for port
3528
}
3629

3730
return config;

src/MCDebugSession.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { DebugProtocol } from 'vscode-debugprotocol';
77
import { LogOutputEvent, LogLevel } from 'vscode-debugadapter/lib/logger';
88
import { MCMessageStreamParser } from './MCMessageStreamParser';
99
import { MCSourceMaps } from './MCSourceMaps';
10-
import { window } from 'vscode';
10+
import { FileSystemWatcher, window, workspace } from 'vscode';
1111
import * as path from 'path';
1212

1313
interface PendingResponse {
@@ -20,7 +20,7 @@ interface PendingResponse {
2020
interface IAttachRequestArguments extends DebugProtocol.AttachRequestArguments {
2121
mode: string;
2222
localRoot: string;
23-
remoteRoot: string;
23+
generatedSourceRoot: string;
2424
sourceMapRoot: string;
2525
host: string;
2626
port: number;
@@ -39,8 +39,8 @@ export class MCDebugSession extends DebugSession {
3939
private _terminated: boolean = false;
4040
private _threads = new Set<number>();
4141
private _requests = new Map<number, PendingResponse>();
42-
private _sourceMaps: MCSourceMaps = new MCSourceMaps("", "");
43-
private _localRoot: string = "";
42+
private _sourceMaps: MCSourceMaps = new MCSourceMaps("");
43+
private _fileWatcher?: FileSystemWatcher;
4444
private _activeThreadId: number = 0; // the one being debugged
4545

4646
public constructor() {
@@ -72,11 +72,6 @@ export class MCDebugSession extends DebugSession {
7272

7373
// VSCode wants to attach to a debugee (MC), create socket connection on specified port
7474
protected async attachRequest(response: DebugProtocol.AttachResponse, args: IAttachRequestArguments, request?: DebugProtocol.Request) {
75-
// capture arguments from launch.json
76-
this._localRoot = path.normalize(args.localRoot);
77-
// init source maps
78-
this._sourceMaps = new MCSourceMaps(args.localRoot, args.remoteRoot, args.sourceMapRoot);
79-
8075
this.closeSession();
8176

8277
const host = args.host || 'localhost';
@@ -101,6 +96,12 @@ export class MCDebugSession extends DebugSession {
10196
return;
10297
}
10398

99+
// init source maps
100+
this._sourceMaps = new MCSourceMaps(args.localRoot, args.sourceMapRoot, args.generatedSourceRoot);
101+
102+
// watch for source map changes
103+
this.createSourceMapFileWatcher(args.sourceMapRoot);
104+
104105
// tell VSCode that attach is complete
105106
this.sendResponse(response);
106107
}
@@ -194,8 +195,7 @@ export class MCDebugSession extends DebugSession {
194195
line: line || 0,
195196
column: column || 0
196197
});
197-
let localOriginalAbsolutePath = path.join(this._localRoot, originalLocation.source);
198-
const source = new Source(path.basename(originalLocation.source), localOriginalAbsolutePath);
198+
const source = new Source(path.basename(originalLocation.source), originalLocation.source);
199199
stackFrames.push(new StackFrame(id, name, source, originalLocation.line, originalLocation.column));
200200
}
201201
catch (e) {
@@ -380,6 +380,11 @@ export class MCDebugSession extends DebugSession {
380380
this._connectionSocket.destroy();
381381
}
382382
this._connectionSocket = undefined;
383+
384+
if (this._fileWatcher) {
385+
this._fileWatcher.dispose();
386+
this._fileWatcher = undefined;
387+
}
383388
}
384389

385390
// close and terminate session (could be from debugee request)
@@ -439,7 +444,7 @@ export class MCDebugSession extends DebugSession {
439444
// json = 1 line json + new line
440445
let messageLength = jsonBuffer.byteLength + 1;
441446
let length = '00000000' + messageLength.toString(16) + '\n';
442-
length = length.substr(length.length - 9);
447+
length = length.substring(length.length - 9);
443448
let lengthBuffer = Buffer.from(length);
444449
let newline = Buffer.from('\n');
445450
let buffer = Buffer.concat([lengthBuffer, jsonBuffer, newline]);
@@ -517,6 +522,19 @@ export class MCDebugSession extends DebugSession {
517522
}
518523
}
519524

525+
private createSourceMapFileWatcher(sourceMapRoot?: string) {
526+
if (this._fileWatcher) {
527+
this._fileWatcher.dispose();
528+
this._fileWatcher = undefined
529+
}
530+
if (sourceMapRoot) {
531+
this._fileWatcher = workspace.createFileSystemWatcher('**/*.{map}', false, false, false);
532+
this._fileWatcher.onDidChange(uri => { this._sourceMaps.reset() });
533+
this._fileWatcher.onDidCreate(uri => { this._sourceMaps.reset() });
534+
this._fileWatcher.onDidDelete(uri => { this._sourceMaps.reset() });
535+
}
536+
}
537+
520538
// ------------------------------------------------------------------------
521539

522540
private log(message: string, logLevel: LogLevel) {

0 commit comments

Comments
 (0)