Skip to content

Commit 097b34b

Browse files
kevmoosuryaKommana2662
authored andcommitted
fix(web): hash source maps before rewriting sourceMappingURL with --web-content-hash (flutter#193614)
## Description Fixes a content-hash mismatch when combining `--web-content-hash` and `--source-maps` in `flutter build web`: - **Hash and rename `sourceMapFile` before rewriting `sourceMappingURL` (`_hashAndRenameWebOutput`)**: - Previously, `_hashAndRenameWebOutput` (`packages/flutter_tools/lib/src/build_system/targets/web.dart`) hashed `main.dart.js` *before* rewriting its trailing `//# sourceMappingURL=main.dart.js.map` comment in-place (and stripped the trailing newline because `\s*$` matched `\n` before EOF). - When `updatePrecacheManifest` (`packages/flutter_tools/lib/src/web/content_hash.dart`) later hashed the final `build/web/main.dart.<hash>.js` bytes on disk, the rewritten `sourceMappingURL` comment produced a different SHA-256 prefix, causing `precache_manifest.json` to emit `"urlHashed": false` and a mismatched `"hash"` for `main.dart.<hash>.js`. - `_hashAndRenameWebOutput` now hashes `sourceMapFile` first (`mapHash`), renames it via `computeHashedBasename(oldMapBasename, mapHash, file.fileSystem)` (`main.dart.<mapHash>.js.map`, `main.dart.<mapHash>.mjs.map`, `worker.<mapHash>.js.map`), rewrites `//# sourceMappingURL=<newMapBasename>` in the code file using a positive lookahead `(?=\s*$)` so trailing newlines are preserved, and then hashes and renames the code file (`contentHash`) so its filename hash matches its final on-disk bytes. - **Resolve hashed source map basenames in `Dart2JSTarget.buildFiles` and `Dart2WasmTarget.buildFiles`**: - Uses `_resolveHashedBasename(environment.buildDir, _mainJsMapRegex, 'main.dart.js.map')` and `_resolveHashedBasename(environment.buildDir, _mainMjsMapRegex, 'main.dart.mjs.map')` when `compilerConfig.webContentHash` is enabled so `WebReleaseBundle` copies the independently content-hashed `.map` files to `build/web/`. ## Related Issues - Fixes flutter#193601 - Part of flutter#149031 ## Tests - Updated source map hashing unit tests in `packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart` to verify independent map and code content hashes and trailing newline preservation. - Added a regression test (`hashAndRenameWebOutput with sourceMapFile produces matching hash and urlHashed: true in updatePrecacheManifest`) verifying end-to-end parity between `_hashAndRenameWebOutput` and `updatePrecacheManifest`.
1 parent 969bccf commit 097b34b

3 files changed

Lines changed: 131 additions & 50 deletions

File tree

‎packages/flutter_tools/lib/src/build_system/targets/web.dart‎

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,28 +129,22 @@ String _hashAndRenameWebOutput({required File file, File? sourceMapFile}) {
129129
return file.basename;
130130
}
131131

132-
// The hash is computed before the sourceMappingURL comment is rewritten
133-
// below; deriving the map name from the hashed binary name would otherwise
134-
// be circular. The compiler emits the binary and its map from the same
135-
// compilation, so identical binaries imply identical maps.
136-
final String contentHash = crypto.sha256
137-
.convert(file.readAsBytesSync())
138-
.toString()
139-
.substring(0, 8);
140-
final String newBasename = computeHashedBasename(file.basename, contentHash, file.fileSystem);
141-
142-
// The source map shares the binary's hash so the pair stays discoverable as
143-
// '<binary>.map'. A `.wasm` binary embeds its map name in a binary custom
144-
// section that cannot be rewritten here, so its map keeps the unhashed name.
132+
// Hash and rename the source map first so its hashed filename can be
133+
// written into the JS/MJS `sourceMappingURL` comment before hashing the
134+
// code file itself. This ensures the code file's final on-disk bytes match
135+
// the content hash in its filename. A `.wasm` binary embeds its map name in
136+
// a binary custom section that cannot be rewritten here, so its map keeps
137+
// the unhashed name.
145138
final isWasm = file.fileSystem.path.extension(file.path) == '.wasm';
146139
if (sourceMapFile != null && sourceMapFile.existsSync() && !isWasm) {
147140
final String oldMapBasename = sourceMapFile.basename;
148-
final newMapBasename = '$newBasename.map';
141+
final String mapHash = computeShortContentHash(sourceMapFile.readAsBytesSync());
142+
final String newMapBasename = computeHashedBasename(oldMapBasename, mapHash, file.fileSystem);
149143
sourceMapFile.renameSync(sourceMapFile.parent.childFile(newMapBasename).path);
150144

151145
final String content = file.readAsStringSync();
152146
final mapDirectiveRegex = RegExp(
153-
r'//[#@]\s*sourceMappingURL=' + RegExp.escape(oldMapBasename) + r'\s*$',
147+
r'//[#@]\s*sourceMappingURL=' + RegExp.escape(oldMapBasename) + r'(?=\s*$)',
154148
multiLine: true,
155149
);
156150
if (mapDirectiveRegex.hasMatch(content)) {
@@ -160,6 +154,8 @@ String _hashAndRenameWebOutput({required File file, File? sourceMapFile}) {
160154
}
161155
}
162156

157+
final String contentHash = computeShortContentHash(file.readAsBytesSync());
158+
final String newBasename = computeHashedBasename(file.basename, contentHash, file.fileSystem);
163159
file.renameSync(file.parent.childFile(newBasename).path);
164160
return newBasename;
165161
}
@@ -406,12 +402,16 @@ class Dart2JSTarget extends Dart2WebTarget {
406402
Iterable<File> buildFiles(Environment environment) {
407403
final String mainJsName =
408404
(getBuildConfig(environment)['mainJsPath'] as String?) ?? 'main.dart.js';
409-
final mainJsMapName = '$mainJsName.map';
405+
final String? mainJsMapName = !compilerConfig.sourceMaps
406+
? null
407+
: compilerConfig.webContentHash
408+
? _resolveHashedBasename(environment.buildDir, _mainJsMapRegex, 'main.dart.js.map')
409+
: '$mainJsName.map';
410410
return environment.buildDir.listSync(recursive: true).whereType<File>().where((File file) {
411411
if (file.basename == mainJsName) {
412412
return true;
413413
}
414-
if (compilerConfig.sourceMaps && file.basename == mainJsMapName) {
414+
if (mainJsMapName != null && file.basename == mainJsMapName) {
415415
return true;
416416
}
417417
if (_partFileRegex.hasMatch(file.basename)) {
@@ -667,7 +667,11 @@ class Dart2WasmTarget extends Dart2WebTarget {
667667
final String mainWasmName = (config['mainWasmPath'] as String?) ?? 'main.dart.wasm';
668668
final String jsSupportName = (config['jsSupportRuntimePath'] as String?) ?? 'main.dart.mjs';
669669
const mainWasmMapName = 'main.dart.wasm.map';
670-
final jsSupportMapName = '$jsSupportName.map';
670+
final String? jsSupportMapName = !compilerConfig.sourceMaps
671+
? null
672+
: compilerConfig.webContentHash
673+
? _resolveHashedBasename(environment.buildDir, _mainMjsMapRegex, 'main.dart.mjs.map')
674+
: '$jsSupportName.map';
671675

672676
return environment.buildDir.listSync(recursive: true).whereType<File>().where((File file) {
673677
if (file.basename == mainWasmName || file.basename == jsSupportName) {

‎packages/flutter_tools/lib/src/web/content_hash.dart‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ class WebAssetHashResult {
5656
final Map<String, String> extraAssets;
5757
}
5858

59+
/// The short content hash embedded in hashed web output filenames.
60+
String computeShortContentHash(List<int> bytes) =>
61+
crypto.sha256.convert(bytes).toString().substring(0, 8);
62+
5963
String computeHashedBasename(String oldBasename, String contentHash, FileSystem fileSystem) {
6064
final String doubleExt = fileSystem.path.extension(oldBasename, 2);
6165
final String ext = _kKnownHashedExtensions.contains(doubleExt)
@@ -93,10 +97,7 @@ WebAssetHashResult hashWebAssets(Directory assetsDir) {
9397
final String relativePath = fileSystem.path.relative(file.path, from: assetsDir.path);
9498
final List<String> segments = fileSystem.path.split(relativePath);
9599
final String basename = fileSystem.path.basename(file.path);
96-
final String contentHash = crypto.sha256
97-
.convert(file.readAsBytesSync())
98-
.toString()
99-
.substring(0, 8);
100+
final String contentHash = computeShortContentHash(file.readAsBytesSync());
100101
final String newBasename = computeHashedBasename(basename, contentHash, fileSystem);
101102

102103
final newSegments = <String>[...segments.sublist(0, segments.length - 1), newBasename];
@@ -532,7 +533,7 @@ File? updatePrecacheManifest(
532533
}
533534

534535
final Uint8List bytes = file.readAsBytesSync();
535-
final String shortHash = crypto.sha256.convert(bytes).toString().substring(0, 8);
536+
final String shortHash = computeShortContentHash(bytes);
536537
final bool urlHashed = basename.contains('.$shortHash.') || basename.endsWith('.$shortHash');
537538

538539
entries.add(<String, Object>{

‎packages/flutter_tools/test/general.shard/build_system/targets/web_test.dart‎

Lines changed: 103 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,17 +1835,25 @@ _flutter.loader.load();
18351835
'hashAndRenameWebOutput renames the binary with its content hash and pairs the source map',
18361836
() => testbed.run(() {
18371837
const jsContent = 'console.log("hello");\n//# sourceMappingURL=main.dart.js.map\n';
1838+
const mapContent = '{"version":3,"sources":[]}';
18381839
final File jsFile = environment.buildDir.childFile('main.dart.js')
18391840
..createSync(recursive: true)
18401841
..writeAsStringSync(jsContent);
18411842
final File mapFile = environment.buildDir.childFile('main.dart.js.map')
18421843
..createSync(recursive: true)
1843-
..writeAsStringSync('{"version":3,"sources":[]}');
1844+
..writeAsStringSync(mapContent);
18441845

1845-
// The hash is computed from the compiler's output, before the
1846-
// sourceMappingURL comment is rewritten to the hashed map name.
1846+
final String expectedMapHash = crypto.sha256
1847+
.convert(utf8.encode(mapContent))
1848+
.toString()
1849+
.substring(0, 8);
1850+
final expectedMapBasename = 'main.dart.$expectedMapHash.js.map';
1851+
final rewrittenJsContent =
1852+
'console.log("hello");\n//# sourceMappingURL=$expectedMapBasename\n';
1853+
// The binary hash is computed after rewriting the sourceMappingURL
1854+
// comment so the filename hash matches the final on-disk file bytes.
18471855
final String expectedHash = crypto.sha256
1848-
.convert(utf8.encode(jsContent))
1856+
.convert(utf8.encode(rewrittenJsContent))
18491857
.toString()
18501858
.substring(0, 8);
18511859

@@ -1855,33 +1863,38 @@ _flutter.loader.load();
18551863
final File renamedFile = environment.buildDir.childFile(newBasename);
18561864
expect(renamedFile, exists);
18571865

1858-
// The map shares the binary's hash so that '<binary>.map' resolves, and
1859-
// the binary's sourceMappingURL comment points at the renamed map.
1866+
// The map is hashed with its own content hash, and the binary's
1867+
// sourceMappingURL comment points at the renamed map.
18601868
expect(mapFile, isNot(exists));
1861-
expect(environment.buildDir.childFile('$newBasename.map'), exists);
1862-
expect(renamedFile.readAsStringSync(), contains('sourceMappingURL=$newBasename.map'));
1869+
expect(environment.buildDir.childFile(expectedMapBasename), exists);
1870+
expect(renamedFile.readAsStringSync(), rewrittenJsContent);
18631871
}),
18641872
);
18651873

18661874
test(
18671875
'Dart2JSTarget buildFiles includes the renamed source map',
18681876
() => testbed.run(() {
1869-
// Binary and map contents differ, as in a real build; the map name must
1870-
// still be discoverable from the binary name.
1877+
// Binary and map contents differ, as in a real build; the map name has
1878+
// its own content hash and must still be discoverable by buildFiles.
1879+
const mapContent = '{"version":3,"sources":["main.dart"]}';
18711880
final File jsFile = environment.buildDir.childFile('main.dart.js')
18721881
..createSync(recursive: true)
18731882
..writeAsStringSync('console.log("hello");\n//# sourceMappingURL=main.dart.js.map\n');
18741883
final File mapFile = environment.buildDir.childFile('main.dart.js.map')
18751884
..createSync(recursive: true)
1876-
..writeAsStringSync('{"version":3,"sources":["main.dart"]}');
1885+
..writeAsStringSync(mapContent);
1886+
final String expectedMapHash = crypto.sha256
1887+
.convert(utf8.encode(mapContent))
1888+
.toString()
1889+
.substring(0, 8);
18771890
final String newBasename = hashAndRenameWebOutput(file: jsFile, sourceMapFile: mapFile);
18781891

18791892
final target = Dart2JSTarget(const JsCompilerConfig(webContentHash: true));
18801893
final List<String> files = target
18811894
.buildFiles(environment)
18821895
.map((File f) => f.basename)
18831896
.toList();
1884-
expect(files, containsAll(<String>[newBasename, '$newBasename.map']));
1897+
expect(files, containsAll(<String>[newBasename, 'main.dart.$expectedMapHash.js.map']));
18851898
}),
18861899
);
18871900

@@ -1969,16 +1982,18 @@ _flutter.loader.load();
19691982
// Produce the mjs and its map with the real rename logic instead of
19701983
// hand-picked names, so the discovery logic is tested against what the
19711984
// build actually writes.
1985+
const mjsMapContent = '{"version":3,"sources":["main.dart"]}';
19721986
final File mjsFile = environment.buildDir.childFile('main.dart.mjs')
19731987
..createSync(recursive: true)
19741988
..writeAsStringSync('export function main() {}\n//# sourceMappingURL=main.dart.mjs.map\n');
19751989
final File mjsMapFile = environment.buildDir.childFile('main.dart.mjs.map')
19761990
..createSync(recursive: true)
1977-
..writeAsStringSync('{"version":3,"sources":["main.dart"]}');
1978-
final String newMjsBasename = hashAndRenameWebOutput(
1979-
file: mjsFile,
1980-
sourceMapFile: mjsMapFile,
1981-
);
1991+
..writeAsStringSync(mjsMapContent);
1992+
final String expectedMjsMapHash = crypto.sha256
1993+
.convert(utf8.encode(mjsMapContent))
1994+
.toString()
1995+
.substring(0, 8);
1996+
hashAndRenameWebOutput(file: mjsFile, sourceMapFile: mjsMapFile);
19821997

19831998
final target = Dart2WasmTarget(
19841999
const WasmCompilerConfig(webContentHash: true),
@@ -1990,7 +2005,7 @@ _flutter.loader.load();
19902005
.map((File f) => f.basename)
19912006
.toList();
19922007
expect(files, contains(mapFile.basename));
1993-
expect(files, contains('$newMjsBasename.map'));
2008+
expect(files, contains('main.dart.$expectedMjsMapHash.mjs.map'));
19942009
}),
19952010
);
19962011

@@ -2043,6 +2058,7 @@ _flutter.loader.load();
20432058
final File stalePartMap = environment.buildDir.childFile('main.dart.js_1.part.js.map')
20442059
..createSync(recursive: true);
20452060
const jsContent = 'console.log("hello");\n//# sourceMappingURL=main.dart.js.map\n';
2061+
const mapContent = '{"version":3,"sources":["main.dart"]}';
20462062
final common = <String>[
20472063
..._kDart2jsLinuxArgs,
20482064
'-Ddart.vm.product=true',
@@ -2074,17 +2090,22 @@ _flutter.loader.load();
20742090
],
20752091
onRun: (_) {
20762092
environment.buildDir.childFile('main.dart.js').writeAsStringSync(jsContent);
2077-
environment.buildDir
2078-
.childFile('main.dart.js.map')
2079-
.writeAsStringSync('{"version":3,"sources":["main.dart"]}');
2093+
environment.buildDir.childFile('main.dart.js.map').writeAsStringSync(mapContent);
20802094
},
20812095
),
20822096
);
20832097

20842098
await Dart2JSTarget(const JsCompilerConfig(webContentHash: true)).build(environment);
20852099

2100+
final String expectedMapHash = crypto.sha256
2101+
.convert(utf8.encode(mapContent))
2102+
.toString()
2103+
.substring(0, 8);
2104+
final expectedMapBasename = 'main.dart.$expectedMapHash.js.map';
2105+
final rewrittenJsContent =
2106+
'console.log("hello");\n//# sourceMappingURL=$expectedMapBasename\n';
20862107
final String expectedHash = crypto.sha256
2087-
.convert(utf8.encode(jsContent))
2108+
.convert(utf8.encode(rewrittenJsContent))
20882109
.toString()
20892110
.substring(0, 8);
20902111
final expectedBasename = 'main.dart.$expectedHash.js';
@@ -2093,7 +2114,7 @@ _flutter.loader.load();
20932114
expect(stalePartJs, isNot(exists));
20942115
expect(stalePartMap, isNot(exists));
20952116
expect(environment.buildDir.childFile(expectedBasename), exists);
2096-
expect(environment.buildDir.childFile('$expectedBasename.map'), exists);
2117+
expect(environment.buildDir.childFile(expectedMapBasename), exists);
20972118

20982119
final Depfile depfile = environment.depFileService.parse(
20992120
environment.buildDir.childFile('dart2js.d'),
@@ -2361,34 +2382,89 @@ const mapName = "main.dart.js.map";
23612382
console.log(mapName);
23622383
//# sourceMappingURL=main.dart.js.map
23632384
''';
2385+
const mapContent = '{"version":3}';
23642386
final File jsFile = environment.buildDir.childFile('main.dart.js')
23652387
..createSync(recursive: true)
23662388
..writeAsStringSync(jsContent);
23672389
final File mapFile = environment.buildDir.childFile('main.dart.js.map')
23682390
..createSync(recursive: true)
2369-
..writeAsStringSync('{"version":3}');
2391+
..writeAsStringSync(mapContent);
23702392

2393+
final String expectedMapHash = crypto.sha256
2394+
.convert(utf8.encode(mapContent))
2395+
.toString()
2396+
.substring(0, 8);
23712397
final String newBasename = hashAndRenameWebOutput(file: jsFile, sourceMapFile: mapFile);
23722398
final File renamedJs = environment.buildDir.childFile(newBasename);
23732399
expect(renamedJs.readAsStringSync(), contains('const mapName = "main.dart.js.map";'));
2374-
expect(renamedJs.readAsStringSync(), contains('//# sourceMappingURL=$newBasename.map'));
2400+
expect(
2401+
renamedJs.readAsStringSync(),
2402+
contains('//# sourceMappingURL=main.dart.$expectedMapHash.js.map'),
2403+
);
23752404
}),
23762405
);
23772406

23782407
test(
23792408
'hashAndRenameWebOutput supports non-dart compound extensions like worker.js.map',
23802409
() => testbed.run(() {
2410+
const mapContent = '{"version":3}';
23812411
final File workerJs = environment.buildDir.childFile('worker.js')
23822412
..createSync(recursive: true)
23832413
..writeAsStringSync('console.log("worker");\n//# sourceMappingURL=worker.js.map\n');
23842414
final File workerMap = environment.buildDir.childFile('worker.js.map')
23852415
..createSync(recursive: true)
2386-
..writeAsStringSync('{"version":3}');
2416+
..writeAsStringSync(mapContent);
23872417

2418+
final String expectedMapHash = crypto.sha256
2419+
.convert(utf8.encode(mapContent))
2420+
.toString()
2421+
.substring(0, 8);
23882422
final String newBasename = hashAndRenameWebOutput(file: workerJs, sourceMapFile: workerMap);
23892423
expect(newBasename, matches(r'^worker\.[a-f0-9]{8}\.js$'));
23902424
expect(workerMap.existsSync(), isFalse);
2391-
expect(environment.buildDir.childFile('$newBasename.map').existsSync(), isTrue);
2425+
expect(environment.buildDir.childFile('worker.$expectedMapHash.js.map').existsSync(), isTrue);
2426+
}),
2427+
);
2428+
2429+
test(
2430+
'hashAndRenameWebOutput with sourceMapFile produces matching hash and urlHashed: true in updatePrecacheManifest',
2431+
() => testbed.run(() {
2432+
final File jsFile = environment.outputDir.childFile('main.dart.js')
2433+
..createSync(recursive: true)
2434+
..writeAsStringSync('console.log("hello");\n//# sourceMappingURL=main.dart.js.map\n');
2435+
final File mapFile = environment.outputDir.childFile('main.dart.js.map')
2436+
..createSync(recursive: true)
2437+
..writeAsStringSync('{"version":3,"sources":["main.dart"]}');
2438+
final File mjsFile = environment.outputDir.childFile('main.dart.mjs')
2439+
..createSync(recursive: true)
2440+
..writeAsStringSync('export function main() {}\n//# sourceMappingURL=main.dart.mjs.map\n');
2441+
final File mjsMapFile = environment.outputDir.childFile('main.dart.mjs.map')
2442+
..createSync(recursive: true)
2443+
..writeAsStringSync('{"version":3,"sources":["main.dart"]}');
2444+
2445+
final String newJsBasename = hashAndRenameWebOutput(file: jsFile, sourceMapFile: mapFile);
2446+
final String newMjsBasename = hashAndRenameWebOutput(
2447+
file: mjsFile,
2448+
sourceMapFile: mjsMapFile,
2449+
);
2450+
2451+
final File manifestFile = updatePrecacheManifest(
2452+
environment.outputDir,
2453+
enabled: true,
2454+
useLocalCanvasKit: false,
2455+
)!;
2456+
final decoded = jsonDecode(manifestFile.readAsStringSync()) as Map<String, Object?>;
2457+
final List<Map<String, Object?>> entries = (decoded['entries']! as List<Object?>)
2458+
.cast<Map<String, Object?>>();
2459+
2460+
expect(entries, hasLength(2));
2461+
for (final entry in entries) {
2462+
final url = entry['url']! as String;
2463+
final hash = entry['hash']! as String;
2464+
expect(url, anyOf(newJsBasename, newMjsBasename));
2465+
expect(url, contains('.$hash.'));
2466+
expect(entry['urlHashed'], isTrue);
2467+
}
23922468
}),
23932469
);
23942470

0 commit comments

Comments
 (0)