Skip to content

Commit 416ba38

Browse files
fix(devtool): normalize file URL source paths (#23028)
* fix(devtool): normalize file URL source paths Related: web-infra-dev/rspack#16313 * fix(devtool): keep a decoded ? in a file URL source path out of the query Make the path relative before attaching the query, so a checkout under a directory named with ? still yields a checkout-independent source name. --------- Co-authored-by: alexander-akait <[email protected]>
1 parent 61f9327 commit 416ba38

9 files changed

Lines changed: 156 additions & 1 deletion

File tree

‎.changeset/050-source-map-real-content-hash.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"webpack": patch
33
---
44

5-
Fix a stale hash in the source map `file` field with `realContentHash`.
5+
Fix source map hashes for `file` fields and local `file:///` sources.

‎lib/util/identifier.js‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,8 @@ const getUndoPath = (filename, outputPath, enforceRelative) => {
508508
};
509509

510510
const HASH_REGEXP = /(?<!\0)#/g;
511+
const QUESTION_MARK_REGEXP = /\?/g;
512+
const NUL_REGEXP = /\0/g;
511513

512514
/**
513515
* Escape `#` characters that appear inside a path request's directory portion
@@ -565,6 +567,32 @@ const makePathsRelative = makeCacheableWithContext(_makePathsRelative);
565567
*/
566568
const contextifySourceUrl = (context, source, associatedObjectForCache) => {
567569
if (source.startsWith("webpack://")) return source;
570+
if (source.startsWith("file:///")) {
571+
const {
572+
path: sourcePath,
573+
query,
574+
fragment
575+
} = _parseResource(source.slice(7));
576+
try {
577+
const decodedPath = decodeURIComponent(sourcePath);
578+
// Source maps can contain Windows paths even on a POSIX host.
579+
const withoutLeadingSlash = decodedPath.slice(1);
580+
const absolutePath = WINDOWS_ABS_PATH_REGEXP.test(withoutLeadingSlash)
581+
? withoutLeadingSlash
582+
: decodedPath;
583+
// A decoded `?` is a path character, so it is hidden from the query split as
584+
// a NUL, which no path or context can contain, while the path is made relative.
585+
source =
586+
absoluteToRequest(
587+
context.replace(QUESTION_MARK_REGEXP, "\0"),
588+
absolutePath.replace(QUESTION_MARK_REGEXP, "\0")
589+
).replace(NUL_REGEXP, "?") +
590+
query +
591+
fragment;
592+
} catch (_err) {
593+
// Source names need not be valid URLs.
594+
}
595+
}
568596
return `webpack://${makePathsRelative(
569597
context,
570598
source,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("../index");
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("../index");
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
it("should make file URL sources relative and decode their paths", () => {
5+
const mapFile = fs.readdirSync(__dirname).find(file => file.endsWith(".map"));
6+
const map = JSON.parse(fs.readFileSync(path.join(__dirname, mapFile), "utf8"));
7+
expect(map.sources).toContain("webpack:///./entry file.js");
8+
});
9+
10+
it("should emit identical hashes and assets from different directories", () => {
11+
const [first, second] = __STATS__.children;
12+
expect(first.hash).toBe(second.hash);
13+
const firstDirectory = path.join(__dirname, "..", "a");
14+
const secondDirectory = path.join(__dirname, "..", "b");
15+
const files = fs.readdirSync(firstDirectory).sort();
16+
expect(files).toEqual(fs.readdirSync(secondDirectory).sort());
17+
for (const file of files) {
18+
expect(fs.readFileSync(path.join(firstDirectory, file), "utf8")).toBe(
19+
fs.readFileSync(path.join(secondDirectory, file), "utf8")
20+
);
21+
}
22+
});
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"use strict";
2+
3+
const { pathToFileURL } = require("url");
4+
5+
/** @type {import("../../../../").LoaderDefinition} */
6+
module.exports = function (source) {
7+
this.callback(null, source, {
8+
version: 3,
9+
file: "entry file.js",
10+
sources: [pathToFileURL(this.resourcePath).href],
11+
sourcesContent: [source],
12+
names: [],
13+
mappings: "AAAA"
14+
});
15+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const findOutputFiles = require("../../../helpers/findOutputFiles");
5+
6+
module.exports = {
7+
findBundle(i, options) {
8+
const files = findOutputFiles(options, /^main\.[a-f0-9]+\.js$/);
9+
expect(files).toHaveLength(1);
10+
return path.join(options.output.path, files[0]);
11+
}
12+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"use strict";
2+
3+
const path = require("path");
4+
const { SourceMapDevToolPlugin } = require("../../../../");
5+
6+
/**
7+
* @param {undefined} env test environment
8+
* @param {{ testPath: string }} options test options
9+
* @returns {import("../../../../").Configuration[]} configurations
10+
*/
11+
module.exports = (env, { testPath }) =>
12+
["a", "b"].map((directory) => ({
13+
context: path.join(__dirname, directory),
14+
entry: "./entry file.js",
15+
target: "node",
16+
devtool: false,
17+
output: {
18+
path: path.join(testPath, directory),
19+
filename: "main.[contenthash].js"
20+
},
21+
node: { __dirname: false },
22+
module: {
23+
rules: [{ test: /entry file\.js$/, loader: require.resolve("./loader") }]
24+
},
25+
plugins: [
26+
new SourceMapDevToolPlugin({ filename: "[name].[contenthash].map" })
27+
]
28+
}));

‎test/unitCases/identifier.unittest.js‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,54 @@ const { pathToFileURL } = require("url");
55
const identifierUtil = require("../../lib/util/identifier");
66

77
describe("util/identifier", () => {
8+
describe("contextifySourceUrl", () => {
9+
/** @type {[string, string, string][]} */
10+
const cases = [
11+
["/project", "file:///project/source.js", "webpack://./source.js"],
12+
["/project", "file:///project/a%20file.js", "webpack://./a file.js"],
13+
["/project", "file:///shared/source.js", "webpack://../shared/source.js"],
14+
["/project", "file:///project/a%2520file.js", "webpack://./a%20file.js"],
15+
["/project", "file:///project/a%23file.js", "webpack://./a#file.js"],
16+
[
17+
"/work/a?b/project",
18+
"file:///work/a%3Fb/project/src/entry.js?query#fragment",
19+
"webpack://./src/entry.js?query#fragment"
20+
],
21+
[
22+
"C:\\work\\a?b",
23+
"file:///C:/work/a%3Fb/entry.js",
24+
"webpack://./entry.js"
25+
],
26+
[
27+
"/project",
28+
"file:///project/source.js?query=%20#fragment",
29+
"webpack://./source.js?query=%20#fragment"
30+
],
31+
[
32+
"C:\\project",
33+
"file:///C:/project/a%20file.js",
34+
"webpack://./a file.js"
35+
],
36+
[
37+
"/project",
38+
"file://server/share/source.js",
39+
"webpack://file://server/share/source.js"
40+
],
41+
["/project", "file:///project/100%.js", "webpack://file:///project/100%.js"],
42+
[
43+
"/project",
44+
"file:///project/%FF.js",
45+
"webpack://file:///project/%FF.js"
46+
],
47+
["/project", "webpack://custom/source.js", "webpack://custom/source.js"],
48+
["/project", "/project/source.js", "webpack://./source.js"]
49+
];
50+
51+
it.each(cases)("contextifies %s and %s as %s", (context, source, expected) => {
52+
expect(identifierUtil.contextifySourceUrl(context, source)).toBe(expected);
53+
});
54+
});
55+
856
describe("normalizeSourceMapUrl", () => {
957
it("should normalize a source map's source as its readers resolve it", () => {
1058
/** @type {[string, string][]} */

0 commit comments

Comments
 (0)