Skip to content

Commit 5c7a9c8

Browse files
PaperStrikerichardlau
authored andcommitted
lib: don't parse windows drive letters as schemes
We were incorrectly parsing windows drive letters as schemes. This was polluting the source map cache with malformed file paths. Fixes: #50523 PR-URL: #50580 Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 00dfabf commit 5c7a9c8

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

lib/internal/source_map/source_map_cache.js

+5
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const kLeadingProtocol = /^\w+:\/\//;
3939
const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/g;
4040
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;
4141

42+
const { isAbsolute } = require('path');
4243
const { fileURLToPath, pathToFileURL, URL } = require('internal/url');
4344

4445
let SourceMap;
@@ -263,9 +264,13 @@ function sourceMapFromDataUrl(sourceURL, url) {
263264
// If the sources are not absolute URLs after prepending of the "sourceRoot",
264265
// the sources are resolved relative to the SourceMap (like resolving script
265266
// src in a html document).
267+
// If the sources are absolute paths, the sources are converted to absolute file URLs.
266268
function sourcesToAbsolute(baseURL, data) {
267269
data.sources = data.sources.map((source) => {
268270
source = (data.sourceRoot || '') + source;
271+
if (isAbsolute(source)) {
272+
return pathToFileURL(source).href;
273+
}
269274
return new URL(source, baseURL).href;
270275
});
271276
// The sources array is now resolved to absolute URLs, sourceRoot should

test/fixtures/source-map/ts-node-win32.js

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixtures/source-map/ts-node.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function foo(str: string) {
2+
return str;
3+
}
4+
5+
foo('noop');
6+
7+
// To recreate (Windows only):
8+
//
9+
// const filePath = require.resolve('./test/fixtures/source-map/ts-node.ts');
10+
// const compiled = require('ts-node').create({ transpileOnly: true }).compile(fs.readFileSync(filePath, 'utf8'), filePath);
11+
// fs.writeFileSync('test/fixtures/source-map/ts-node-win32.js', compiled);

test/parallel/test-source-map-enable.js

+24
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,30 @@ function nextdir() {
289289
assert.match(output.stderr.toString(), /at functionC.*10:3/);
290290
}
291291

292+
// Properly converts Windows absolute paths to absolute URLs.
293+
// Refs: https://github.com/nodejs/node/issues/50523
294+
// Refs: https://github.com/TypeStrong/ts-node/issues/1769
295+
{
296+
const coverageDirectory = nextdir();
297+
const output = spawnSync(process.execPath, [
298+
require.resolve('../fixtures/source-map/ts-node-win32.js'),
299+
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
300+
assert.strictEqual(output.status, 0);
301+
assert.strictEqual(output.stderr.toString(), '');
302+
const sourceMap = getSourceMapFromCache(
303+
'ts-node-win32.js',
304+
coverageDirectory
305+
);
306+
// base64 JSON should have been decoded, the D: in the sources field should
307+
// have been taken as the drive letter on Windows, the scheme on POSIX.
308+
assert.strictEqual(
309+
sourceMap.data.sources[0],
310+
common.isWindows ?
311+
'file:///D:/workspaces/node/test/fixtures/source-map/ts-node.ts' :
312+
'd:/workspaces/node/test/fixtures/source-map/ts-node.ts'
313+
);
314+
}
315+
292316
// Stores and applies source map associated with file that throws while
293317
// being required.
294318
{

0 commit comments

Comments
 (0)