Skip to content

Commit 3c15681

Browse files
committed
Auto merge of #3619 - RalfJung:print-sysroot, r=RalfJung
properly print error in 'cargo miri setup --print-sysroot' Based on rustc-build-sysroot now putting the stderr into the error message.
2 parents 0e41a80 + 2b9c1ca commit 3c15681

File tree

4 files changed

+36
-58
lines changed

4 files changed

+36
-58
lines changed

src/tools/miri/cargo-miri/Cargo.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,9 @@ dependencies = [
178178

179179
[[package]]
180180
name = "rustc-build-sysroot"
181-
version = "0.5.0"
181+
version = "0.5.2"
182182
source = "registry+https://github.com/rust-lang/crates.io-index"
183-
checksum = "de6077473f0c46779b49e4587a81f1b8919e0ec26630409ecfda0ba3259efb43"
183+
checksum = "fa3ca63cc537c1cb69e4c2c0afc5fda2ccd36ac84c97d5a4ae05e69b1c834afb"
184184
dependencies = [
185185
"anyhow",
186186
"rustc_version",

src/tools/miri/cargo-miri/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ directories = "5"
1818
rustc_version = "0.4"
1919
serde_json = "1.0.40"
2020
cargo_metadata = "0.18.0"
21-
rustc-build-sysroot = "0.5.0"
21+
rustc-build-sysroot = "0.5.2"
2222

2323
# Enable some feature flags that dev-dependencies need but dependencies
2424
# do not. This makes `./miri install` after `./miri build` faster.

src/tools/miri/cargo-miri/src/setup.rs

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
33
use std::env;
44
use std::ffi::OsStr;
5-
use std::fmt::Write;
65
use std::path::PathBuf;
76
use std::process::{self, Command};
87

@@ -24,6 +23,7 @@ pub fn setup(
2423
let only_setup = matches!(subcommand, MiriCommand::Setup);
2524
let ask_user = !only_setup;
2625
let print_sysroot = only_setup && has_arg_flag("--print-sysroot"); // whether we just print the sysroot path
26+
let show_setup = only_setup && !print_sysroot;
2727
if !only_setup {
2828
if let Some(sysroot) = std::env::var_os("MIRI_SYSROOT") {
2929
// Skip setup step if MIRI_SYSROOT is explicitly set, *unless* we are `cargo miri setup`.
@@ -115,18 +115,16 @@ pub fn setup(
115115
// `config.toml`.
116116
command.env("RUSTC_WRAPPER", "");
117117

118-
if only_setup && !print_sysroot {
118+
if show_setup {
119119
// Forward output. Even make it verbose, if requested.
120+
command.stdout(process::Stdio::inherit());
121+
command.stderr(process::Stdio::inherit());
120122
for _ in 0..verbose {
121123
command.arg("-v");
122124
}
123125
if quiet {
124126
command.arg("--quiet");
125127
}
126-
} else {
127-
// Suppress output.
128-
command.stdout(process::Stdio::null());
129-
command.stderr(process::Stdio::null());
130128
}
131129

132130
command
@@ -137,22 +135,25 @@ pub fn setup(
137135
// not apply `RUSTFLAGS` to the sysroot either.
138136
let rustflags = &["-Cdebug-assertions=off", "-Coverflow-checks=on"];
139137

138+
let mut after_build_output = String::new(); // what should be printed when the build is done.
140139
let notify = || {
141-
let mut msg = String::new();
142-
write!(msg, "Preparing a sysroot for Miri (target: {target})").unwrap();
143-
if verbose > 0 {
144-
write!(msg, " in {}", sysroot_dir.display()).unwrap();
145-
}
146-
write!(msg, "...").unwrap();
147-
148-
if print_sysroot || quiet {
149-
// Be silent.
150-
} else if only_setup {
151-
// We want to be explicit.
152-
eprintln!("{msg}");
153-
} else {
154-
// We want to be quiet, but still let the user know that something is happening.
155-
eprint!("{msg} ");
140+
if !quiet {
141+
eprint!("Preparing a sysroot for Miri (target: {target})");
142+
if verbose > 0 {
143+
eprint!(" in {}", sysroot_dir.display());
144+
}
145+
if show_setup {
146+
// Cargo will print things, so we need to finish this line.
147+
eprintln!("...");
148+
after_build_output = format!(
149+
"A sysroot for Miri is now available in `{}`.\n",
150+
sysroot_dir.display()
151+
);
152+
} else {
153+
// Keep all output on a single line.
154+
eprint!("... ");
155+
after_build_output = format!("done\n");
156+
}
156157
}
157158
};
158159

@@ -167,31 +168,17 @@ pub fn setup(
167168
.build_from_source(&rust_src);
168169
match status {
169170
Ok(SysrootStatus::AlreadyCached) =>
170-
if only_setup && !(print_sysroot || quiet) {
171+
if !quiet && show_setup {
171172
eprintln!(
172173
"A sysroot for Miri is already available in `{}`.",
173174
sysroot_dir.display()
174175
);
175176
},
176177
Ok(SysrootStatus::SysrootBuilt) => {
177-
if print_sysroot || quiet {
178-
// Be silent.
179-
} else if only_setup {
180-
eprintln!("A sysroot for Miri is now available in `{}`.", sysroot_dir.display());
181-
} else {
182-
eprintln!("done");
183-
}
178+
// Print what `notify` prepared.
179+
eprint!("{after_build_output}");
184180
}
185-
Err(err) =>
186-
if print_sysroot {
187-
show_error!("failed to build sysroot")
188-
} else if only_setup {
189-
show_error!("failed to build sysroot: {err:?}")
190-
} else {
191-
show_error!(
192-
"failed to build sysroot; run `cargo miri setup` to see the error details"
193-
)
194-
},
181+
Err(err) => show_error!("failed to build sysroot: {err:?}"),
195182
}
196183

197184
if print_sysroot {

src/tools/miri/miri-script/src/commands.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,19 @@ impl MiriEnv {
4242
let target_flag = &target_flag;
4343

4444
if !quiet {
45+
eprint!("$ cargo miri setup");
4546
if let Some(target) = target {
46-
eprintln!("$ (building Miri sysroot for {})", target.to_string_lossy());
47-
} else {
48-
eprintln!("$ (building Miri sysroot)");
47+
eprint!(" --target {target}", target = target.to_string_lossy());
4948
}
49+
eprintln!();
5050
}
5151

52-
let output = cmd!(self.sh,
52+
let mut cmd = cmd!(self.sh,
5353
"cargo +{toolchain} --quiet run {cargo_extra_flags...} --manifest-path {manifest_path} --
5454
miri setup --print-sysroot {target_flag...}"
55-
).read();
56-
let Ok(output) = output else {
57-
// Run it again (without `--print-sysroot` or `--quiet`) so the user can see the error.
58-
cmd!(
59-
self.sh,
60-
"cargo +{toolchain} run {cargo_extra_flags...} --manifest-path {manifest_path} --
61-
miri setup {target_flag...}"
62-
)
63-
.run()
64-
.with_context(|| "`cargo miri setup` failed")?;
65-
panic!("`cargo miri setup` didn't fail again the 2nd time?");
66-
};
55+
);
56+
cmd.set_quiet(quiet);
57+
let output = cmd.read()?;
6758
self.sh.set_var("MIRI_SYSROOT", &output);
6859
Ok(output.into())
6960
}

0 commit comments

Comments
 (0)