Skip to content

Commit cf2ab2c

Browse files
authored
[SourceMaps] Avoid emitting multiple sourceMappingURL sections (#8191)
When the user asks to emit one, and one already exists, replace the existing one. It is meaningless to have two (and could be interpreted differently by tools - does the first or the second matter, etc.). Fixes #8190
1 parent c3de795 commit cf2ab2c

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/wasm/wasm-binary.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,9 +1373,19 @@ void WasmBinaryWriter::writeSourceMapEpilog() {
13731373

13741374
void WasmBinaryWriter::writeLateCustomSections() {
13751375
for (auto& section : wasm->customSections) {
1376-
if (section.name != BinaryConsts::CustomSections::Dylink) {
1377-
writeCustomSection(section);
1376+
if (section.name == BinaryConsts::CustomSections::Dylink) {
1377+
// This is an early custom section.
1378+
continue;
13781379
}
1380+
1381+
if (section.name == BinaryConsts::CustomSections::SourceMapUrl &&
1382+
sourceMap && !sourceMapUrl.empty()) {
1383+
// We are writing a SourceMapURL manually, following the user's request.
1384+
// Do not emit the existing custom section as a second one.
1385+
continue;
1386+
}
1387+
1388+
writeCustomSection(section);
13791389
}
13801390
}
13811391

test/lit/debug/source-map-url.wast

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
(module
2+
(func $test (param i32) (result i32)
3+
;;@ waka:100:1
4+
(i32.const 42)
5+
)
6+
)
7+
8+
;; 1. Generate a binary with a source map and url.
9+
;;
10+
;; RUN: wasm-opt %s -g -o %t.wasm -osm %t.wasm.map -osu=one
11+
;; RUN: wasm-dis %t.wasm | filecheck %s --check-prefix=ONE
12+
13+
;; ONE: ;; custom section "sourceMappingURL", size 4
14+
15+
16+
;; 2. Round-trip that binary, again using -osu. This should not end up with two
17+
;; sourceMappingURL sections (one could arrive from the flag, and one from the
18+
;; existing custom section, if we copy it). Note the length increases, from
19+
;; "one\0" (4) to "swap\0" (5), as we swap the URL.
20+
;;
21+
;; RUN: wasm-opt %t.wasm -o %t.wasm2 -ism %t.wasm.map -osm %t.wasm.map2 -osu=swap
22+
;; RUN: wasm-dis %t.wasm2 | filecheck %s --check-prefix=SWAP
23+
24+
;; Look for a wrong section both before and after the correct one.
25+
26+
;; SWAP-NOT: ;; custom section "sourceMappingURL", size 4
27+
;; SWAP: ;; custom section "sourceMappingURL", size 5
28+
;; SWAP-NOT: ;; custom section "sourceMappingURL"
29+
30+
31+
;; 3. Round-trip without -osu. Now we just copy the old URL.
32+
;;
33+
;; RUN: wasm-opt %t.wasm2 -o %t.wasm3 -ism %t.wasm.map2 -osm %t.wasm.map3
34+
;; RUN: wasm-dis %t.wasm3 | filecheck %s --check-prefix=ROUND
35+
36+
;; ROUND: ;; custom section "sourceMappingURL", size 5
37+

0 commit comments

Comments
 (0)