Skip to content

Commit ea5a092

Browse files
authored
fix recompiling every time in uv-python (#16214)
Cargo is currently recompiling uv-python on every invocation because the minified JSON output file is getting a mod time newer than the last run. Instead, set the mod time of the output file to the same as the input file.
1 parent 9887ef5 commit ea5a092

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

crates/uv-python/build.rs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#[allow(clippy::disallowed_types)]
2+
use std::fs::{File, FileTimes};
3+
use std::io::Write;
14
use std::path::PathBuf;
25
use std::{env, fs};
36

@@ -36,16 +39,37 @@ fn main() {
3639

3740
let json_data: serde_json::Value = serde_json::from_str(
3841
#[allow(clippy::disallowed_methods)]
39-
&fs::read_to_string(version_metadata).expect("Failed to read download-metadata.json"),
42+
&fs::read_to_string(&version_metadata).expect("Failed to read download-metadata.json"),
4043
)
4144
.expect("Failed to parse JSON");
4245

4346
let filtered_data = process_json(&json_data);
4447

48+
#[allow(clippy::disallowed_types)]
49+
let mut out_file = File::create(version_metadata_minified)
50+
.expect("failed to open download-metadata-minified.json");
51+
4552
#[allow(clippy::disallowed_methods)]
46-
fs::write(
47-
version_metadata_minified,
48-
serde_json::to_string(&filtered_data).expect("Failed to serialize JSON"),
49-
)
50-
.expect("Failed to write minified JSON");
53+
out_file
54+
.write_all(
55+
serde_json::to_string(&filtered_data)
56+
.expect("Failed to serialize JSON")
57+
.as_bytes(),
58+
)
59+
.expect("Failed to write minified JSON");
60+
61+
// Cargo uses the modified times of the paths specified in
62+
// `rerun-if-changed`, so fetch the current file times and set them the same
63+
// on the output file.
64+
#[allow(clippy::disallowed_methods)]
65+
let meta =
66+
fs::metadata(version_metadata).expect("failed to read metadata for download-metadata.json");
67+
68+
out_file
69+
.set_times(
70+
FileTimes::new()
71+
.set_accessed(meta.accessed().unwrap())
72+
.set_modified(meta.modified().unwrap()),
73+
)
74+
.expect("failed to write file times to download-metadata-minified.json");
5175
}

0 commit comments

Comments
 (0)