Skip to content

BridgeJS: Skip writing output files when content is unchanged#639

Merged
krodak merged 1 commit intoswiftwasm:mainfrom
PassiveLogic:kr/bridgejs-skip-unchanged-writes
Feb 16, 2026
Merged

BridgeJS: Skip writing output files when content is unchanged#639
krodak merged 1 commit intoswiftwasm:mainfrom
PassiveLogic:kr/bridgejs-skip-unchanged-writes

Conversation

@krodak
Copy link
Member

@krodak krodak commented Feb 16, 2026

Overview

BridgeJSTool writes BridgeJS.swift and BridgeJS.json on every invocation, even when the content hasn't changed. The atomic write creates a new temp file each time, which updates the mtime and makes SPM think the output changed. This triggers a recompile of BridgeJS.swift and a relink on every build, even with zero source changes.

Reproducing

While testing cross-plugin builds after #638, we noticed Compiling Khasm BridgeJS.swift and Linking KhasmWasm.wasm in every no-op build. Traced it to the unconditional .write(to:atomically:) calls in BridgeJSTool - the content was identical but the file got a new timestamp every time.

To reproduce with any BridgeJS project:

swift package js --product MyProduct    # first build
swift package js --product MyProduct    # immediate rebuild, no changes
# BridgeJS.swift recompiles, binary relinks

Fix

Compare content before writing. If the existing file has identical content, skip the write.

private func writeIfChanged(_ content: String, to url: URL) throws {
    let existing = try? String(contentsOf: url, encoding: .utf8)
    guard existing != content else { return }
    try content.write(to: url, atomically: true, encoding: .utf8)
}

Applied to both BridgeJS.swift and BridgeJS.json. The --always-write flag still works - it controls whether the file gets created at all (even when empty), just doesn't force a rewrite when content matches.

Question

Was the unconditional write intentional? Wondering if there's an edge case I'm not seeing, if so, lets just close this one 🙏🏻

@krodak krodak self-assigned this Feb 16, 2026
Copy link
Member

@kateinoigakukun kateinoigakukun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes sense to me, thanks!

@krodak krodak merged commit 79bda2e into swiftwasm:main Feb 16, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants