Use write-if-changed pattern in connectrpc-build#22
Merged
Conversation
Skip writing output files when content is already identical, preserving mtime so Cargo doesn't spuriously recompile downstream crates. Cargo's rebuild decision for include!-ed files is mtime-based (rustc dep-info lists the file, Cargo compares mtime vs fingerprint). Before this change, touching any single .proto file re-ran the build script and unconditionally rewrote every output .rs, bumping all their mtimes and cascading into a full recompile even when N-1 of N files were byte-identical. Mirrors anthropics/buffa#17 and prost-build's write_file_if_changed.
asacamano
approved these changes
Apr 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Skip writing output files when their content is already identical to what's on disk, preserving mtime so Cargo doesn't spuriously recompile downstream crates.
Why
Cargo's rebuild decision for
include!-ed files is mtime-based — rustc's dep-info (.d) file lists every included path, and Cargo compares each one's mtime against the stored fingerprint timestamp. Verified empirically:touch $OUT_DIR/gen.rs(content unchanged) →cargo build -vreportsDirty ...: the file 'gen.rs' has changed (mtime 1774896316s, 1m 14s after last build)and recompiles.Before this change, touching any single
.protofile re-ran the build script (correct, percargo:rerun-if-changed), which then unconditionally rewrote every output.rsfile — bumping all their mtimes and cascading into a full downstream recompile even when N-1 of N generated files were byte-identical.Change
Two call sites in
Config::compile()now go throughwrite_if_changed(read-compare-conditionally-write). The now-unuseduse std::io::Writeis removed. Three tests cover create/skip-identical/overwrite. Uses edition-2024 let-chain syntax for the nested condition.Prior art
buffa-buildprost-buildconfig.rs:1179—write_file_if_changedwith the identical implementation