Skip to content

Commit 83615cf

Browse files
authored
Setup cargo environment for cargo rustc --print (#15026)
It turns out, running `cargo rustc --print cfg -Zunstable-options` (and the like, #9357) fail with `.cargo/config.toml` setups like ```toml [build] # custom target json that lives in `./targets/my-super-cool-target.json` target = "my-super-cool-target" [env] RUST_TARGET_PATH = { value = "./targets", relative = true } ``` resulting in ``` ❯ cargo rustc --print cfg -Zunstable-options error: Error loading target specification: Could not find specification for target "my-super-cool-target". Run `rustc --print target-list` for a list of built-in targets error: process didn't exit successfully: `C:\Users\lukas\.rustup\toolchains\nightly-x86_64-pc-windows-msvc\bin\rustc.exe --target my-super-cool-target --print cfg` (exit code: 1) ``` The reason for that is that cargo recognizes the target from the `.cargo/config` and then implicitly passes that along to the spawned rustc process, but it does so without passing along the important environment that is required for the target tuple to make sense. (can add a test if desired, just tell me where)
2 parents 9438fe3 + b7a0c9d commit 83615cf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ use std::sync::Arc;
4141

4242
use crate::core::compiler::unit_dependencies::build_unit_dependencies;
4343
use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph};
44-
use crate::core::compiler::{standard_lib, CrateType, TargetInfo};
44+
use crate::core::compiler::{apply_env_config, standard_lib, CrateType, TargetInfo};
4545
use crate::core::compiler::{BuildConfig, BuildContext, BuildRunner, Compilation};
4646
use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit};
4747
use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner};
@@ -188,6 +188,7 @@ pub fn print<'a>(
188188
}
189189
let target_info = TargetInfo::new(gctx, &build_config.requested_kinds, &rustc, *kind)?;
190190
let mut process = rustc.process();
191+
apply_env_config(gctx, &mut process)?;
191192
process.args(&target_info.rustflags);
192193
if let Some(args) = target_rustc_args {
193194
process.args(args);

tests/testsuite/rustc.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,37 @@ windows
793793
.run();
794794
}
795795

796+
#[cargo_test]
797+
fn rustc_with_print_cfg_config_toml_env() {
798+
let p = project()
799+
.file("Cargo.toml", &basic_bin_manifest("foo"))
800+
.file(
801+
"targets/best-target.json",
802+
r#"{
803+
"llvm-target": "x86_64-unknown-none",
804+
"target-pointer-width": "64",
805+
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
806+
"arch": "x86_64"
807+
}"#,
808+
)
809+
.file(
810+
".cargo/config.toml",
811+
r#"
812+
[build]
813+
target = "best-target"
814+
[env]
815+
RUST_TARGET_PATH = { value = "./targets", relative = true }
816+
"#,
817+
)
818+
.file("src/main.rs", r#"fn main() {} "#)
819+
.build();
820+
821+
p.cargo("rustc -Z unstable-options --print cfg")
822+
.masquerade_as_nightly_cargo(&["print"])
823+
.with_stdout_data(str!["..."].unordered())
824+
.run();
825+
}
826+
796827
#[cargo_test]
797828
fn precedence() {
798829
// Ensure that the precedence of cargo-rustc is only lower than RUSTFLAGS,

0 commit comments

Comments
 (0)