Skip to content

Commit 9dba78a

Browse files
committed
show a better error when running Miri with the wrong sysroot
1 parent 9edbf36 commit 9dba78a

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

cargo-miri/bin.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,12 +374,15 @@ fn setup(subcommand: MiriCommand) {
374374
}
375375
None => {
376376
// Check for `rust-src` rustup component.
377-
let sysroot = miri()
378-
.args(&["--print", "sysroot"])
379-
.output()
380-
.expect("failed to determine sysroot")
381-
.stdout;
382-
let sysroot = std::str::from_utf8(&sysroot).unwrap();
377+
let output =
378+
miri().args(&["--print", "sysroot"]).output().expect("failed to determine sysroot");
379+
if !output.status.success() {
380+
show_error(format!(
381+
"Failed to determine sysroot; Miri said:\n{}",
382+
String::from_utf8_lossy(&output.stderr).trim_end()
383+
));
384+
}
385+
let sysroot = std::str::from_utf8(&output.stdout).unwrap();
383386
let sysroot = Path::new(sysroot.trim_end_matches('\n'));
384387
// Check for `$SYSROOT/lib/rustlib/src/rust/library`; test if that contains `std/Cargo.toml`.
385388
let rustup_src =

src/bin/miri.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![feature(rustc_private, stmt_expr_attributes)]
2-
#![allow(clippy::manual_range_contains)]
2+
#![allow(clippy::manual_range_contains, clippy::useless_format)]
33

44
extern crate rustc_data_structures;
55
extern crate rustc_driver;
@@ -143,6 +143,11 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
143143
}
144144
}
145145

146+
fn show_error(msg: String) -> ! {
147+
eprintln!("fatal error: {}", msg);
148+
std::process::exit(1)
149+
}
150+
146151
fn init_early_loggers() {
147152
// Note that our `extern crate log` is *not* the same as rustc's; as a result, we have to
148153
// initialize them both, and we always initialize `miri`'s first.
@@ -214,13 +219,26 @@ fn compile_time_sysroot() -> Option<String> {
214219
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
215220
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
216221
Some(match (home, toolchain) {
217-
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
218-
_ =>
219-
option_env!("RUST_SYSROOT")
220-
.expect(
222+
(Some(home), Some(toolchain)) => {
223+
// Check that at runtime, we are still in this toolchain.
224+
let toolchain_runtime =
225+
env::var_os("RUSTUP_TOOLCHAIN").or_else(|| env::var_os("MULTIRUST_TOOLCHAIN"));
226+
if !matches!(toolchain_runtime, Some(r) if r == toolchain) {
227+
show_error(format!(
228+
"This Miri got built with local toolchain `{toolchain}`, but now is being run under a different toolchain. \n\
229+
Make sure to run Miri in the toolchain it got built with, e.g. via `cargo +{toolchain} miri`."
230+
));
231+
}
232+
233+
format!("{}/toolchains/{}", home, toolchain)
234+
}
235+
_ => option_env!("RUST_SYSROOT")
236+
.unwrap_or_else(|| {
237+
show_error(format!(
221238
"To build Miri without rustup, set the `RUST_SYSROOT` env var at build time",
222-
)
223-
.to_owned(),
239+
))
240+
})
241+
.to_owned(),
224242
})
225243
}
226244

0 commit comments

Comments
 (0)