Skip to content
This repository was archived by the owner on Apr 3, 2024. It is now read-only.

Commit 108225c

Browse files
Louis-Yemctavish
andauthored
fix: source mapping original path instead of user-provided input (#978)
* fix: Add debugging information for sourcemapper * Fix lint issue * fix: source mapping original path instead of user-provided input * Fix lint issue * Add test case for more coverage in sourcemapper * Move comments in test-sourcemmapper to the correct place * Update comments about the entry of getMapInfoOutput * Fix typo Co-authored-by: James McTavish <[email protected]>
1 parent b647106 commit 108225c

File tree

4 files changed

+98
-82
lines changed

4 files changed

+98
-82
lines changed

src/agent/io/sourcemapper.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ export interface MapInfoInput {
3535
// process's working directory).
3636
outputFile: string;
3737

38+
// The path of the original source file (relative to the process's working
39+
// directory). In the example above, this will be either "src/index1.ts" or
40+
// "src/index2.ts".
41+
inputFile: string;
42+
3843
// The source map's path (relative to the process's working directory). For
3944
// the same example above, this field is "dist/index.js.map".
4045
mapFile: string;
@@ -158,8 +163,10 @@ async function processSourcemap(
158163
}
159164

160165
for (const src of normalizedSourcesRelToProc) {
161-
infoMap.set(path.normalize(src), {
166+
const inputFile = path.normalize(src);
167+
infoMap.set(inputFile, {
162168
outputFile: outputPath,
169+
inputFile,
163170
mapFile: mapPath,
164171
mapConsumer: consumer,
165172
sources: sourcesRelToSrcmap,
@@ -218,13 +225,13 @@ export class SourceMapper {
218225
}
219226

220227
/**
221-
* @param {string} inputPath The path to an input file that could possibly
222-
* be the input to a transpilation process. The path should be relative to
223-
* the process's current working directory
224228
* @param {number} The line number in the input file where the line number is
225229
* zero-based.
226230
* @param {number} (Optional) The column number in the line of the file
227231
* specified where the column number is zero-based.
232+
* @param {string} The entry of the source map info in the sourceMapper. Such
233+
* an entry is supposed to be got by the getMapInfoInput method.
234+
*
228235
* @return {Object} The object returned has a "file" attribute for the
229236
* path of the output file associated with the given input file (where the
230237
* path is relative to the process's current working directory),
@@ -237,17 +244,14 @@ export class SourceMapper {
237244
* with it then null is returned.
238245
*/
239246
getMapInfoOutput(
240-
inputPath: string,
241247
lineNumber: number,
242248
colNumber: number,
243249
entry: MapInfoInput
244250
): MapInfoOutput | null {
245-
this.logger.debug(`sourcemapper inputPath: ${inputPath}`);
246-
247-
inputPath = path.normalize(inputPath);
251+
this.logger.debug(`sourcemapper entry.inputFile: ${entry.inputFile}`);
248252

249253
const relPath = path
250-
.relative(path.dirname(entry.mapFile), inputPath)
254+
.relative(path.dirname(entry.mapFile), entry.inputFile)
251255
.replace(/\\/g, '/');
252256

253257
/**

src/agent/v8/inspector-debugapi.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ export class InspectorDebugApi implements debugapi.DebugApi {
198198
const line = breakpoint.location.line;
199199
const column = 0;
200200
const mapInfo = this.sourcemapper.getMapInfoOutput(
201-
baseScriptPath,
202201
line,
203202
column,
204203
mapInfoInput

src/agent/v8/legacy-debugapi.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ export class V8DebugApi implements debugapi.DebugApi {
145145
const line = breakpoint.location.line;
146146
const column = 0;
147147
const mapInfo = this.sourcemapper.getMapInfoOutput(
148-
baseScriptPath,
149148
line,
150149
column,
151150
mapInfoInput

test/test-sourcemapper.ts

Lines changed: 85 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,6 @@ import {MockLogger} from './mock-logger';
2525
const BASE_PATH = path.join(__dirname, 'fixtures', 'sourcemapper');
2626
const QUICK_MILLISECONDS = 300;
2727

28-
/**
29-
* @param {string} tool The name of the tool that was used to generate the
30-
* given sourcemap data
31-
* @param {string} relativeMapFilePath The path to the sourcemap file of a
32-
* transpilation to test
33-
* @param {string} relativeInputFilePath The path to the input file that was
34-
* transpiled to generate the specified sourcemap file
35-
* @param {string} relativeOutputFilePath The path to the output file that was
36-
* generated during the transpilation process that constructed the
37-
* specified sourcemap file
38-
* @param {Array.<Array.<number, number>>} inToOutLineNums An array of arrays
39-
* where each element in the array is a pair of numbers. The first number
40-
* in the pair is the line number from the input file and the second number
41-
* in the pair is the expected line number in the corresponding output file
42-
*
43-
* Note: The line numbers are zero-based
44-
*/
45-
4628
describe('sourcemapper debug info', () => {
4729
const logger = new MockLogger();
4830
const mapFilePath = path.join(
@@ -84,45 +66,56 @@ describe('sourcemapper debug info', () => {
8466

8567
const mapInfoInput = sourcemapper.getMapInfoInput(inputFilePath);
8668
assert.notEqual(mapInfoInput, null);
87-
sourcemapper.getMapInfoOutput(inputFilePath, 1, 0, mapInfoInput!);
69+
sourcemapper.getMapInfoOutput(1, 0, mapInfoInput!);
8870

8971
// Verify if the debugging information is correctly printed.
9072
// We use 'indexOf' here instead of 'match' to avoid parsing regular
9173
// expression, which will confuse the result when having '\' in the path
9274
// name on platform like Windows.
75+
const expectedDebugMessages = [
76+
`sourcemapper entry.inputFile: ${inputFilePath}`,
77+
'sourcePos: {"source":"in.ts","line":2,"column":0}',
78+
'mappedPos: {"line":6,"column":0,"lastColumn":null}',
79+
].reverse();
9380
const debugsLength = logger.debugs.length;
94-
assert.notStrictEqual(
95-
logger.debugs[debugsLength - 3].args[0].indexOf(
96-
`sourcemapper inputPath: ${inputFilePath}`
97-
),
98-
-1
99-
);
100-
assert.notStrictEqual(
101-
logger.debugs[debugsLength - 2].args[0].indexOf(
102-
'sourcePos: {"source":"in.ts","line":2,"column":0}'
103-
),
104-
-1
105-
);
106-
assert.notStrictEqual(
107-
logger.debugs[debugsLength - 1].args[0].indexOf(
108-
'mappedPos: {"line":6,"column":0,"lastColumn":null}'
109-
),
110-
-1
111-
);
81+
for (let i = 0; i < expectedDebugMessages.length; i++) {
82+
assert.notStrictEqual(
83+
logger.debugs[debugsLength - i - 1].args[0].indexOf(
84+
expectedDebugMessages[i]
85+
),
86+
-1,
87+
`'${logger.debugs[debugsLength - i - 1].args[0]}' does not match '${
88+
expectedDebugMessages[i]
89+
}'`
90+
);
91+
}
11292
});
11393
});
11494

95+
/**
96+
* @param {string} tool The name of the tool that was used to generate the
97+
* given sourcemap data
98+
* @param {string} mapFilePath The path to the sourcemap file of a
99+
* transpilation to test
100+
* @param {string} inputFilePath The path to the input file that was
101+
* transpiled to generate the specified sourcemap file
102+
* @param {string} outputFilePath The path to the output file that was
103+
* generated during the transpilation process that constructed the
104+
* specified sourcemap file
105+
* @param {Array.<Array.<number, number>>} inToOutLineNums An array of arrays
106+
* where each element in the array is a pair of numbers. The first number
107+
* in the pair is the line number from the input file and the second number
108+
* in the pair is the expected line number in the corresponding output file
109+
*
110+
* Note: The line numbers are zero-based
111+
*/
115112
function testTool(
116113
tool: string,
117-
relativeMapFilePath: string,
118-
relativeInputFilePath: string,
119-
relativeOutputFilePath: string,
114+
mapFilePath: string,
115+
inputFilePath: string,
116+
outputFilePath: string,
120117
inToOutLineNums: number[][]
121118
) {
122-
const mapFilePath = path.join(BASE_PATH, relativeMapFilePath);
123-
const inputFilePath = path.join(BASE_PATH, relativeInputFilePath);
124-
const outputFilePath = path.join(BASE_PATH, relativeOutputFilePath);
125-
126119
describe('sourcemapper for tool ' + tool, () => {
127120
const logger = new MockLogger();
128121
let sourcemapper: sm.SourceMapper;
@@ -160,10 +153,7 @@ function testTool(
160153
sourcemapper.getMapInfoInput(inputFilePath),
161154
null
162155
);
163-
const movedPath = path.join(
164-
'/some/other/base/dir/',
165-
relativeInputFilePath
166-
);
156+
const movedPath = path.join('/some/other/base/dir/', inputFilePath);
167157
assert.notStrictEqual(
168158
sourcemapper.getMapInfoInput(inputFilePath),
169159
null,
@@ -189,20 +179,34 @@ function testTool(
189179
}
190180
);
191181

182+
it(
183+
'for tool ' +
184+
tool +
185+
' it can get mapping info output when input file does not exist in the source map',
186+
done => {
187+
const mapInfoInput = sourcemapper.getMapInfoInput(inputFilePath);
188+
assert.notEqual(mapInfoInput, null);
189+
190+
const mapInfoOutput = sourcemapper.getMapInfoOutput(10, 0, {
191+
outputFile: mapInfoInput!.outputFile,
192+
inputFile: 'some/file/not/in/sourcemap',
193+
mapFile: mapInfoInput!.mapFile,
194+
mapConsumer: mapInfoInput!.mapConsumer,
195+
sources: mapInfoInput!.sources,
196+
});
197+
198+
assert.notEqual(mapInfoOutput, null);
199+
assert.strictEqual(mapInfoOutput!.file, mapInfoInput!.outputFile);
200+
assert.strictEqual(mapInfoOutput!.line, -1);
201+
done();
202+
}
203+
);
204+
192205
const testLineMapping = (inputLine: number, expectedOutputLine: number) => {
193206
const mapInfoInput = sourcemapper.getMapInfoInput(inputFilePath);
194207
assert.notEqual(mapInfoInput, null);
195-
const info = sourcemapper.getMapInfoOutput(
196-
inputFilePath,
197-
inputLine,
198-
0,
199-
mapInfoInput!
200-
);
201-
assert.notStrictEqual(
202-
info,
203-
null,
204-
'The mapping info for file ' + inputFilePath + ' must be non-null'
205-
);
208+
const info = sourcemapper.getMapInfoOutput(inputLine, 0, mapInfoInput!);
209+
206210
assert.strictEqual(info!.file, outputFilePath);
207211
assert.strictEqual(
208212
info!.line,
@@ -228,9 +232,9 @@ function testTool(
228232

229233
testTool(
230234
'Babel',
231-
path.join('babel', 'out.js.map'),
232-
path.join('babel', 'in.js'),
233-
path.join('babel', 'out.js'),
235+
path.join(BASE_PATH, path.join('babel', 'out.js.map')),
236+
path.join(BASE_PATH, path.join('babel', 'in.js')),
237+
path.join(BASE_PATH, path.join('babel', 'out.js')),
234238
[
235239
[1, 14],
236240
[2, 15],
@@ -287,9 +291,9 @@ testTool(
287291

288292
testTool(
289293
'Typescript',
290-
path.join('typescript', 'out.js.map'),
291-
path.join('typescript', 'in.ts'),
292-
path.join('typescript', 'out.js'),
294+
path.join(BASE_PATH, path.join('typescript', 'out.js.map')),
295+
path.join(BASE_PATH, path.join('typescript', 'in.ts')),
296+
path.join(BASE_PATH, path.join('typescript', 'out.js')),
293297
[
294298
[1, 5],
295299
[2, 6],
@@ -307,11 +311,21 @@ testTool(
307311
]
308312
);
309313

314+
// This test is for testing that by providing a partial partial match of the
315+
// source file, the breakpoint can still be set correctly.
316+
testTool(
317+
'Typescript with sub path',
318+
path.join(BASE_PATH, path.join('typescript', 'out.js.map')),
319+
'in.ts',
320+
path.join(BASE_PATH, path.join('typescript', 'out.js')),
321+
[[1, 5]]
322+
);
323+
310324
testTool(
311325
'Coffeescript',
312-
path.join('coffeescript', 'in.js.map'),
313-
path.join('coffeescript', 'in.coffee'),
314-
path.join('coffeescript', 'in.js'),
326+
path.join(BASE_PATH, path.join('coffeescript', 'in.js.map')),
327+
path.join(BASE_PATH, path.join('coffeescript', 'in.coffee')),
328+
path.join(BASE_PATH, path.join('coffeescript', 'in.js')),
315329
[
316330
[1, 1],
317331
[2, 7],
@@ -333,9 +347,9 @@ testTool(
333347

334348
testTool(
335349
'Webpack with Typescript',
336-
path.join('webpack-ts', 'out.js.map'),
337-
path.join('webpack-ts', 'in.ts_'),
338-
path.join('webpack-ts', 'out.js'),
350+
path.join(BASE_PATH, path.join('webpack-ts', 'out.js.map')),
351+
path.join(BASE_PATH, path.join('webpack-ts', 'in.ts_')),
352+
path.join(BASE_PATH, path.join('webpack-ts', 'out.js')),
339353
[
340354
[3, 93],
341355
[4, 94],

0 commit comments

Comments
 (0)