Skip to content

Commit

Permalink
lib: don't parse windows drive letters as schemes
Browse files Browse the repository at this point in the history
We were incorrectly parsing windows drive letters as schemes. This was
polluting the source map cache with malformed file paths.

Fixes: nodejs#50523
  • Loading branch information
PaperStrike committed Nov 6, 2023
1 parent 2238532 commit 64365ad
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/internal/source_map/source_map_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const kLeadingProtocol = /^\w+:\/\//;
const kSourceMappingURLMagicComment = /\/[*/]#\s+sourceMappingURL=(?<sourceMappingURL>[^\s]+)/g;
const kSourceURLMagicComment = /\/[*/]#\s+sourceURL=(?<sourceURL>[^\s]+)/g;

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

let SourceMap;
Expand Down Expand Up @@ -254,6 +255,9 @@ function sourceMapFromDataUrl(sourceURL, url) {
function sourcesToAbsolute(baseURL, data) {
data.sources = data.sources.map((source) => {
source = (data.sourceRoot || '') + source;
if (isAbsolute(source)) {
return pathToFileURL(source).href;
}
return new URL(source, baseURL).href;
});
// The sources array is now resolved to absolute URLs, sourceRoot should
Expand Down
10 changes: 10 additions & 0 deletions test/fixtures/source-map/ts-node-win32.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions test/fixtures/source-map/ts-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function foo(str: string) {
return str;
}

foo('noop');

// To recreate (only on windows):
//
// const filePath = require.resolve('./test/fixtures/source-map/ts-node.ts');
// const compiled = require('ts-node').create({ transpileOnly: true }).compile(fs.readFileSync(filePath, 'utf8'), filePath);
// fs.writeFileSync('test/fixtures/source-map/ts-node-win32.js', compiled);
24 changes: 24 additions & 0 deletions test/parallel/test-source-map-enable.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,30 @@ function nextdir() {
assert.match(output.stderr.toString(), /at functionC.*10:3/);
}

// Properly converts windows absolute paths to absolute URLs
// Refs: https://github.com/nodejs/node/issues/50523
// Refs: https://github.com/TypeStrong/ts-node/issues/1769
{
const coverageDirectory = nextdir();
const output = spawnSync(process.execPath, [
require.resolve('../fixtures/source-map/ts-node-win32.js'),
], { env: { ...process.env, NODE_V8_COVERAGE: coverageDirectory } });
assert.strictEqual(output.status, 0);
assert.strictEqual(output.stderr.toString(), '');
const sourceMap = getSourceMapFromCache(
'ts-node-win32.js',
coverageDirectory
);
// base64 JSON should have been decoded, the D: in the sources field should
// have been taken as the drive letter on Windows, the scheme on POSIX.
assert.strictEqual(
sourceMap.data.sources[0],
common.isWindows ?
'file:///D:/workspaces/node/test/fixtures/source-map/ts-node.ts' :
'd:/workspaces/node/test/fixtures/source-map/ts-node.ts'
);
}

// Stores and applies source map associated with file that throws while
// being required.
{
Expand Down

0 comments on commit 64365ad

Please sign in to comment.