Skip to content

Commit 86050da

Browse files
authored
fix: Wrong source locations for source maps (#228)
1 parent 604d040 commit 86050da

File tree

4 files changed

+14
-10
lines changed

4 files changed

+14
-10
lines changed

src/runner.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,9 +707,12 @@ async function deriveSourceLocation(
707707

708708
const maintainer = store.maintain(fileUri);
709709
const mapping = await (maintainer.value || maintainer.refresh());
710+
// in parsed stack traces line and column numbers are 1-based, in the VS Code APIs lines are 0-based
711+
const zeroBasedLine = Number(line) - 1;
712+
const zeroBasedCol = Number(col) - 1;
710713
const value =
711-
mapping?.originalPositionFor(Number(line), Number(col)) ||
712-
new vscode.Location(fileUri, new vscode.Position(Number(line), Number(col)));
714+
mapping?.originalPositionFor(zeroBasedLine, zeroBasedCol) ||
715+
new vscode.Location(fileUri, new vscode.Position(zeroBasedLine, zeroBasedCol));
713716

714717
// timeout the maintainer async so that it stays alive for any other immediate teset usage in the file:
715718
setTimeout(() => maintainer.dispose(), 5000);

src/source-map.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const identityMapping = (file: vscode.Uri): IMappingAccessor => ({
3030
});
3131

3232
const smMappingAccessor = (file: vscode.Uri, sm: TraceMap): IMappingAccessor => ({
33+
// @jridgewell/trace-mapping: Lines start at line 1, columns at column 0.
3334
originalPositionFor(line, column) {
3435
const {
3536
source,

src/test/integration/simple.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,8 @@ describe('simple', () => {
235235
expect(failed.message?.location).to.not.be.undefined;
236236
expect(failed.message?.location?.uri.toString()).to.include('hello.test.js');
237237
expect(path.isAbsolute(failed.message!.location!.uri.fsPath)).to.be.true;
238-
expect(failed.message?.location?.range.start.line).to.equal(12);
239-
expect(failed.message?.location?.range.start.character).to.equal(5);
238+
expect(failed.message?.location?.range.start.line).to.equal(11);
239+
expect(failed.message?.location?.range.start.character).to.equal(4);
240240
});
241241

242242
it('handles file and directory excludes', async () => {
@@ -318,10 +318,10 @@ describe('simple', () => {
318318
});
319319

320320
const failed = run.states.find((s) => s.state === 'failed');
321-
expect(failed!.message!.location!.range.start.line).to.equal(4);
322-
expect(failed!.message!.location!.range.start.character).to.equal(11);
323-
expect(failed!.message!.location!.range.end.line).to.equal(4);
324-
expect(failed!.message!.location!.range.end.character).to.equal(11);
321+
expect(failed!.message!.location!.range.start.line).to.equal(3);
322+
expect(failed!.message!.location!.range.start.character).to.equal(10);
323+
expect(failed!.message!.location!.range.end.line).to.equal(3);
324+
expect(failed!.message!.location!.range.end.character).to.equal(10);
325325
expect(failed!.message!.location!.uri.fsPath).to.equal(item.uri!.fsPath);
326326
});
327327
});

src/test/integration/typescript.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ describe('typescript', () => {
8484
expect(failed.message?.location).to.not.be.undefined;
8585
expect(failed.message?.location?.uri.toString()).to.include('hello.test.ts');
8686
expect(path.isAbsolute(failed.message!.location!.uri.fsPath)).to.be.true;
87-
expect(failed.message?.location?.range.start.line).to.equal(29);
88-
expect(failed.message?.location?.range.start.character).to.equal(5);
87+
expect(failed.message?.location?.range.start.line).to.equal(28);
88+
expect(failed.message?.location?.range.start.character).to.equal(4);
8989
});
9090
});

0 commit comments

Comments
 (0)