Skip to content

Conversation

@Elliot-Roberts
Copy link
Contributor

In project_model::toolchain_info::target_data_layout::get(), we are trying to run rustc --print target-spec-json.
We first try via cargo rustc, and fall back to invoking rustc directly in case of failure.

When trying cargo rustc, if we have a target string, we add the arguments ["--target", target] to the very end of the command line, after a --. So the target is passed directly to rustc and not interpreted by Cargo.

This becomes an issue when Cargo has also been given a specific target to use (e.g. in a .cargo/config.toml file), because then Cargo will also add --target ... onto its internal invocation of rustc.
Cargo (reasonably) does not inspect the arguments after -- to check for redundant --target, so rustc is invoked with the target option given twice; rustc does not attempt to interpret this and just errors.

If we instead add the target before the --, i.e. pass it to Cargo instead of directly to rustc, then Cargo can interpret it as an override to whatever it might see in config files or environment variables, and only pass the option to rustc once.

This is how we do it elsewhere in the codebase, too:

let mut cmd = sysroot.tool(Tool::Cargo, cargo_toml.parent(), extra_env);
cmd.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly");
cmd.args(["rustc", "-Z", "unstable-options"]).args(RUSTC_ARGS);
if let Some(target) = target {
cmd.args(["--target", target]);
}
cmd.args(["--", "-O"]);

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 30, 2025
@ChayimFriedman2
Copy link
Contributor

Thanks!

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Aug 30, 2025
Merged via the queue into rust-lang:master with commit 21614ed Aug 30, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants