From 5bcc730c2aa3edca16848189902bed04d07fc66d Mon Sep 17 00:00:00 2001 From: Frans Skarman Date: Tue, 21 Jan 2025 00:43:19 +0000 Subject: [PATCH] Fix unnecessary rebuilds due to pyo3 config file modified time change (#2446) On some of my computers, maturin build always rebuilds pyo3 and friends, and looking at the cargo fingerprinting, this is caused by the py03-config file being written regardless of changes. This only writes the file if it has changed since last rebuild, which seems to prevent the problem. --------- Co-authored-by: messense --- src/compile.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/compile.rs b/src/compile.rs index e91bc191c..909a7ed7f 100644 --- a/src/compile.rs +++ b/src/compile.rs @@ -441,12 +441,17 @@ fn cargo_build_command( target_triple, interpreter.major, interpreter.minor )); fs::create_dir_all(&maturin_target_dir)?; - fs::write(&config_file, pyo3_config).with_context(|| { - format!( - "Failed to create pyo3 config file at '{}'", - config_file.display() - ) - })?; + // We don't want to rewrite the file every time as that will make cargo + // trigger a rebuild of the project every time + let existing_pyo3_config = fs::read_to_string(&config_file).unwrap_or_default(); + if pyo3_config != existing_pyo3_config { + fs::write(&config_file, pyo3_config).with_context(|| { + format!( + "Failed to create pyo3 config file at '{}'", + config_file.display() + ) + })?; + } let abs_config_file = config_file.normalize()?.into_path_buf(); build_command.env("PYO3_CONFIG_FILE", abs_config_file); }