From ac4ffa1a8c89f87f790cce0e68c5792546a04474 Mon Sep 17 00:00:00 2001 From: Cedric Barreteau Date: Mon, 19 Jan 2026 11:15:57 -0500 Subject: [PATCH] Fix remote builds by making build-config contents deterministic Because of the usage of `env!`, we were baking paths to generated files in `lib.rs`. In setups where the build can be executed remotely (e.g. when building with Buck2), those paths would be different depending on whether the build ran locally or remotely. This difference propagates to `pyo3-macros`. Because Buck2 runs the metadata and codegen builds separately (for increased parallelism), they can have different contents due to the above, causing rustc to find two versions of the crate, and erroring out. This is not observable with Cargo because it runs both builds with the same rustc invocation. See https://github.com/facebook/buck2/issues/1206 and https://github.com/rwf2/Rocket/pull/2797 for more context. This commit fixes the issue by simply reading `OUT_DIR` at run-time, rather than compile-time, so that the content of `lib.rs` is fixed. Closes https://github.com/facebook/buck2/issues/1206 Many thanks to https://github.com/cormacrelf for pointing out the root of the issue. --- newsfragments/5745.fixed.md | 1 + pyo3-build-config/src/lib.rs | 10 ++++------ 2 files changed, 5 insertions(+), 6 deletions(-) create mode 100644 newsfragments/5745.fixed.md diff --git a/newsfragments/5745.fixed.md b/newsfragments/5745.fixed.md new file mode 100644 index 00000000000..9a67400d6de --- /dev/null +++ b/newsfragments/5745.fixed.md @@ -0,0 +1 @@ +Fix remote builds by not baking paths in the `pyo3-build-config` build scripts. diff --git a/pyo3-build-config/src/lib.rs b/pyo3-build-config/src/lib.rs index 5d8002d8c91..20f685cecdf 100644 --- a/pyo3-build-config/src/lib.rs +++ b/pyo3-build-config/src/lib.rs @@ -208,12 +208,10 @@ const HOST_CONFIG: &str = include_str!(concat!(env!("OUT_DIR"), "/pyo3-build-con #[doc(hidden)] #[cfg(feature = "resolve-config")] fn resolve_cross_compile_config_path() -> Option { - env::var_os("TARGET").map(|target| { - let mut path = PathBuf::from(env!("OUT_DIR")); - path.push(Path::new(&target)); - path.push("pyo3-build-config.txt"); - path - }) + let mut path = PathBuf::from(env::var_os("OUT_DIR")?); + path.push(Path::new(&env::var_os("TARGET")?)); + path.push("pyo3-build-config.txt"); + Some(path) } /// Helper to print a feature cfg with a minimum rust version required.