Skip to content

Commit d12c716

Browse files
committed
fix: -Cmetadata includes whether extra rustflags is same as host
`-Ztarget-applies-to-host --config target-applies-to-host=false` make it possible to build two units for the same output directory with different rustflags (one with artifact/target rustflags, one with none/host rust flags). Those flags aren't included in the `-Cmetadata` hash which is used to disambiguate different versions of crates, resulting in a conflict. The actual error being produced appears to be the result of two invocations of `rustc` racing and clobbering each other. While we don't hash RUSTFLAGS because it may contain absolute paths that hurts reproducibility, we track whether a unit's RUSTFLAGS is from host config, so that we can generate a different metadata hash for runtime and compile-time units.
1 parent dba34d4 commit d12c716

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src/cargo/core/compiler/build_runner/compilation_files.rs

+20
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,26 @@ fn compute_metadata(
649649
// with user dependencies.
650650
unit.is_std.hash(&mut hasher);
651651

652+
// While we don't hash RUSTFLAGS because it may contain absolute paths that
653+
// hurts reproducibility, we track whether a unit's RUSTFLAGS is from host
654+
// config, so that we can generate a different metadata hash for runtime
655+
// and compile-time units.
656+
//
657+
// HACK: This is a temporary hack for fixing rust-lang/cargo#14253
658+
// Need to find a long-term solution to replace this fragile workaround.
659+
// See https://github.com/rust-lang/cargo/pull/14432#discussion_r1725065350
660+
if unit.kind.is_host() && !bcx.gctx.target_applies_to_host().unwrap_or_default() {
661+
let host_info = bcx.target_data.info(CompileKind::Host);
662+
let target_configs_are_different = unit.rustflags != host_info.rustflags
663+
|| unit.rustdocflags != host_info.rustdocflags
664+
|| bcx
665+
.target_data
666+
.target_config(CompileKind::Host)
667+
.links_overrides
668+
!= unit.links_overrides;
669+
target_configs_are_different.hash(&mut hasher);
670+
}
671+
652672
MetaInfo {
653673
meta_hash: Metadata(hasher.finish()),
654674
use_extra_filename: should_use_metadata(bcx, unit),

tests/testsuite/rustflags.rs

+11-19
Original file line numberDiff line numberDiff line change
@@ -1685,31 +1685,23 @@ fn host_config_shared_build_dep() {
16851685
.masquerade_as_nightly_cargo(&["target-applies-to-host"])
16861686
.arg("-Ztarget-applies-to-host")
16871687
.arg("-Zhost-config")
1688-
// Sometimes it compiles. Not deterministic...
1689-
.with_stderr_data(str![[r#"
1688+
.with_stderr_data(
1689+
str![[r#"
16901690
[UPDATING] `dummy-registry` index
16911691
[LOCKING] 2 packages to latest compatible versions
16921692
[DOWNLOADING] crates ...
16931693
[DOWNLOADED] cc v1.0.0 (registry `dummy-registry`)
1694-
[WARNING] output filename collision.
1695-
The lib target `cc` in package `cc v1.0.0` has the same output filename as the lib target `cc` in package `cc v1.0.0`.
1696-
Colliding filename is: [ROOT]/foo/target/debug/deps/libcc-[HASH].rlib
1697-
The targets should have unique names.
1698-
Consider changing their names to be unique or compiling them separately.
1699-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
1700-
[WARNING] output filename collision.
1701-
The lib target `cc` in package `cc v1.0.0` has the same output filename as the lib target `cc` in package `cc v1.0.0`.
1702-
Colliding filename is: [ROOT]/foo/target/debug/deps/libcc-[HASH].rmeta
1703-
The targets should have unique names.
1704-
Consider changing their names to be unique or compiling them separately.
1705-
This may become a hard error in the future; see <https://github.com/rust-lang/cargo/issues/6313>.
17061694
[COMPILING] cc v1.0.0
17071695
[RUNNING] `rustc --crate-name cc [..]--cfg from_host[..]`
17081696
[RUNNING] `rustc --crate-name cc [..]--cfg from_target[..]`
1709-
[ERROR] failed to build archive: No such file or directory
1710-
1711-
[ERROR] could not compile `cc` (lib) due to 1 previous error
1712-
1713-
"#]])
1697+
[COMPILING] bootstrap v0.0.0 ([ROOT]/foo)
1698+
[RUNNING] `rustc --crate-name build_script_build [..]--cfg from_host[..]`
1699+
[RUNNING] `[ROOT]/foo/target/debug/build/bootstrap-[HASH]/build-script-build`
1700+
[RUNNING] `rustc --crate-name bootstrap[..]--cfg from_target[..]`
1701+
[FINISHED] `dev` profile [unoptimized] target(s) in [ELAPSED]s
1702+
1703+
"#]]
1704+
.unordered(),
1705+
)
17141706
.run();
17151707
}

0 commit comments

Comments
 (0)