Skip to content

Commit 7f1abd2

Browse files
committed
docs(onnxruntime): add detailed comments explaining patch and cache handling
Add comprehensive comments explaining: - Problem: Modern Emscripten compatibility issue - Solution: Two-part patch approach - Cache handling: Why we delete stale build copies - Flow: Patch source → delete cache → CMake recopy Makes the build script more maintainable and easier to understand for future developers encountering this code.
1 parent d102291 commit 7f1abd2

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

packages/onnxruntime/scripts/build.mjs

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -129,33 +129,57 @@ async function cloneOnnxSource() {
129129
printSuccess('BUILD_MLAS_NO_ONNXRUNTIME commented out')
130130

131131
// Patch 3: Modern Emscripten compatibility (see docs/patches.md).
132+
//
133+
// PROBLEM: ONNX Runtime's wasm_post_build.js expects specific Worker URL pattern
134+
// from older Emscripten versions. Modern Emscripten (3.1.50+) doesn't generate
135+
// this pattern, causing build to fail with "Unexpected number of matches" error.
136+
//
137+
// SOLUTION: Patch the script to handle modern Emscripten gracefully:
138+
// 1. Allow zero matches (modern Emscripten generates correct code already)
139+
// 2. Improve error message to show actual match count
140+
//
141+
// CACHE HANDLING: CMake copies wasm_post_build.js from source to build directory
142+
// during configuration. GitHub Actions may restore cached builds with old unpatched
143+
// copies, so we must:
144+
// 1. Patch source file (single source of truth)
145+
// 2. Delete cached build copy if present (forces CMake recopy from patched source)
146+
// 3. Clear CMake cache (ensures full reconfiguration)
132147
printStep('Patching wasm_post_build.js to handle modern Emscripten...')
133148
const postBuildSourcePath = path.join(ONNX_SOURCE_DIR, 'js', 'web', 'script', 'wasm_post_build.js')
134149
if (existsSync(postBuildSourcePath)) {
135150
let postBuildContent = await fs.readFile(postBuildSourcePath, 'utf-8')
151+
152+
// Patch 1: Allow zero matches (modern Emscripten case).
153+
// Insert early return when no Worker URL pattern found.
136154
postBuildContent = postBuildContent.replace(
137155
/if \(matches\.length !== 1\) \{/,
138156
`if (matches.length === 0) {\n console.log('No Worker URL pattern found - skipping post-build transformation (modern Emscripten)');\n return;\n }\n if (matches.length !== 1) {`
139157
)
158+
159+
// Patch 2: Improve error message to show actual match count.
160+
// Helps debug if we get unexpected pattern variations.
140161
postBuildContent = postBuildContent.replace(
141162
/Unexpected number of matches for "" in "": \./,
142163
`Unexpected number of Worker URL matches: found \${matches.length}, expected 1. Pattern: \${regex}`
143164
)
165+
144166
await fs.writeFile(postBuildSourcePath, postBuildContent, 'utf-8')
145-
printSuccess('wasm_post_build.js patched')
167+
printSuccess('wasm_post_build.js (source) patched')
146168
}
147169

148-
// Delete stale cached copies in build directory to force CMake recopy from patched source.
149-
// CMake copies from source during configure, but cached builds may have old unpatched version.
170+
// Delete stale cached copies in build directory.
171+
// This ensures CMake will recopy the patched version from source during configure.
172+
// Without this, GitHub Actions cached builds would use old unpatched version.
150173
printStep('Removing stale wasm_post_build.js from build cache...')
151174
const platform = process.platform === 'darwin' ? 'Darwin' : 'Linux'
152175
const postBuildBuildPath = path.join(ONNX_SOURCE_DIR, 'build', platform, 'Release', 'wasm_post_build.js')
153176
if (existsSync(postBuildBuildPath)) {
154177
await safeDelete(postBuildBuildPath)
155-
printSuccess('Stale wasm_post_build.js removed (will be recopied from source)')
178+
printSuccess('Stale wasm_post_build.js removed (CMake will recopy from patched source)')
156179
}
157180

158-
// Clear CMake cache to ensure fresh reconfiguration.
181+
// Clear CMake cache to force full reconfiguration.
182+
// This ensures CMake recopies all files from source, picking up our patch.
159183
printStep('Clearing CMake cache to force reconfiguration...')
160184
const cmakeCachePath = path.join(ONNX_SOURCE_DIR, 'build', platform, 'Release', 'CMakeCache.txt')
161185
if (existsSync(cmakeCachePath)) {

0 commit comments

Comments
 (0)