Skip to content

Commit 3ad481e

Browse files
authored
fix(build-rs)!: Updates from an audit (#14817)
### What does this PR try to resolve? Did a quick audit for parts of the API and implementation that stood out to me. So far, I've mostly focused on "input". ### How should we test and review this PR? ### Additional information CC @CAD97
2 parents 8ced46a + c02c9f2 commit 3ad481e

File tree

4 files changed

+226
-195
lines changed

4 files changed

+226
-195
lines changed

crates/build-rs-test-lib/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ fn smoke_test_inputs() {
4747
dbg!(cargo_encoded_rustflags());
4848
dbg!(cargo_feature("unstable"));
4949
dbg!(cargo_manifest_dir());
50+
dbg!(cargo_manifest_path());
5051
dbg!(cargo_manifest_links());
5152
dbg!(cargo_pkg_authors());
5253
dbg!(cargo_pkg_description());

crates/build-rs/src/allow_use.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ use std::{process::Command, sync::OnceLock};
33
fn rust_version_minor() -> u32 {
44
static VERSION_MINOR: OnceLock<u32> = OnceLock::new();
55
*VERSION_MINOR.get_or_init(|| {
6-
crate::input::cargo_pkg_rust_version()
7-
.split('.')
8-
.nth(1)
6+
version_minor(&crate::input::cargo_pkg_rust_version().unwrap_or_default())
97
// assume build-rs's MSRV if none specified for the current package
10-
.unwrap_or(env!("CARGO_PKG_RUST_VERSION").split('.').nth(1).unwrap())
11-
.parse()
12-
.unwrap()
8+
.unwrap_or_else(|| version_minor(env!("CARGO_PKG_RUST_VERSION")).unwrap())
139
})
1410
}
1511

@@ -25,16 +21,18 @@ fn cargo_version_minor() -> u32 {
2521
// > cargo -V # example output
2622
// cargo 1.82.0 (8f40fc59f 2024-08-21)
2723

28-
String::from_utf8(out.stdout).expect("`cargo -V` should output valid UTF-8")
29-
["cargo 1.".len()..]
30-
.split('.')
31-
.next()
32-
.expect("`cargo -V` format should be stable")
33-
.parse()
34-
.unwrap()
24+
let out = std::str::from_utf8(&out.stdout).expect("`cargo -V` should output valid UTF-8");
25+
let version = out.split(' ').nth(1).unwrap();
26+
version_minor(version).unwrap()
3527
})
3628
}
3729

30+
fn version_minor(version: &str) -> Option<u32> {
31+
let minor = version.split('.').nth(1)?;
32+
let minor = minor.parse().unwrap();
33+
Some(minor)
34+
}
35+
3836
pub(crate) fn double_colon_directives() -> bool {
3937
// cargo errors on `cargo::` directives with insufficient package.rust-version
4038
rust_version_minor() >= 77

0 commit comments

Comments
 (0)