fix: Avoid --target option being given twice to rustc when invoked through cargo rustc while fetching target data layout
#20579
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In
project_model::toolchain_info::target_data_layout::get(), we are trying to runrustc --print target-spec-json.We first try via
cargo rustc, and fall back to invokingrustcdirectly 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.tomlfile), 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:
rust-analyzer/crates/project-model/src/toolchain_info/rustc_cfg.rs
Lines 67 to 73 in a271dac