Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7a8a2d5

Browse filesBrowse files
PaperStrikeRafaelGSS
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 <legendecas@gmail.com>
1 parent c984158 commit 7a8a2d5
Copy full SHA for 7a8a2d5

File tree

Expand file treeCollapse file tree

4 files changed

+50
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

4 files changed

+50
-0
lines changed
Open diff view settings
Collapse file

‎lib/internal/source_map/source_map_cache.js‎

Copy file name to clipboardExpand all lines: lib/internal/source_map/source_map_cache.js
+5Lines changed: 5 additions & 0 deletions
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
Collapse file

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

Copy file name to clipboardExpand all lines: test/fixtures/source-map/ts-node-win32.js
+10Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Collapse file
+11Lines changed: 11 additions & 0 deletions
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);
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-source-map-enable.js
+24Lines changed: 24 additions & 0 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.