Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize write_to_file in windows-bindgen to avoid unnecessary file writes #3079

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion crates/libs/bindgen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,14 @@ fn write_to_file<C: AsRef<[u8]>>(path: &str, contents: C) -> Result<()> {
.map_err(|_| Error::new("failed to create directory").with_path(path))?;
}

std::fs::write(path, contents).map_err(|_| Error::new("failed to write file").with_path(path))
let existing = std::fs::read(path).unwrap_or_default();

if contents.as_ref() != existing {
kennykerr marked this conversation as resolved.
Show resolved Hide resolved
std::fs::write(path, contents)
.map_err(|_| Error::new("failed to write file").with_path(path))?;
}

Ok(())
}

fn canonicalize(value: &str) -> Result<String> {
Expand Down
9 changes: 0 additions & 9 deletions crates/tests/standalone/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,15 +202,6 @@ fn write_vtbl(output: &str, filter: &[&str]) {
}

fn riddle(output: &str, filter: &[&str], config: &[&str]) {
// Rust-analyzer may re-run build scripts whenever a source file is deleted
// which causes an endless loop if the file is deleted from a build script.
// To workaround this, we truncate the file instead of deleting it.
// See https://github.com/microsoft/windows-rs/issues/2777
_ = std::fs::File::options()
.truncate(true)
.write(true)
.open(output);

let mut command = std::process::Command::new("cargo");

command.args([
Expand Down
Loading