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

fix: Only touch include file if contents is changed #1058

Merged
merged 1 commit into from
Jun 10, 2024
Merged
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
44 changes: 28 additions & 16 deletions prost-build/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,30 +806,22 @@ impl Config {
.expect("every module should have a filename");
let output_path = target.join(file_name);

let previous_content = fs::read(&output_path);

if previous_content
.map(|previous_content| previous_content == content.as_bytes())
.unwrap_or(false)
{
trace!("unchanged: {:?}", file_name);
} else {
trace!("writing: {:?}", file_name);
fs::write(output_path, content)?;
}
write_file_if_changed(&output_path, content.as_bytes())?;
}

if let Some(ref include_file) = self.include_file {
trace!("Writing include file: {:?}", target.join(include_file));
let mut file = fs::File::create(target.join(include_file))?;
self.write_line(&mut file, 0, "// This file is @generated by prost-build.")?;
let path = target.join(include_file);
trace!("Writing include file: {:?}", path);
let mut buffer = Vec::new();
self.write_line(&mut buffer, 0, "// This file is @generated by prost-build.")?;
self.write_includes(
modules.keys().collect(),
&mut file,
&mut buffer,
if target_is_env { None } else { Some(&target) },
&file_names,
)?;
file.flush()?;

write_file_if_changed(&path, &buffer)?;
}

Ok(())
Expand Down Expand Up @@ -1072,6 +1064,26 @@ impl Config {
}
}

/// Write a slice as the entire contents of a file.
///
/// This function will create a file if it does not exist,
/// and will entirely replace its contents if it does. When
/// the contents is already correct, it doesn't touch to the file.
fn write_file_if_changed(path: &Path, content: &[u8]) -> std::io::Result<()> {
let previous_content = fs::read(path);

if previous_content
.map(|previous_content| previous_content == content)
.unwrap_or(false)
{
trace!("unchanged: {:?}", path);
Ok(())
} else {
trace!("writing: {:?}", path);
fs::write(path, content)
}
}

impl default::Default for Config {
fn default() -> Config {
Config {
Expand Down
Loading