Skip to content

Commit ebda506

Browse files
committed
Auto merge of #8119 - ehuss:new-err-formatting, r=alexcrichton
Don't use debug display for error object. When `cargo new` displays a workspace validation warning, it was using the debug display which we probably shouldn't rely on. This also consolidates a similar usage when `cargo doc --open` fails. Closes #8118
2 parents 2cb9243 + b64d0f3 commit ebda506

File tree

4 files changed

+26
-15
lines changed

4 files changed

+26
-15
lines changed

src/cargo/lib.rs

+15-3
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub fn exit_with_error(err: CliError, shell: &mut Shell) -> ! {
117117
/// Displays an error, and all its causes, to stderr.
118118
pub fn display_error(err: &Error, shell: &mut Shell) {
119119
debug!("display_error; err={:?}", err);
120-
let has_verbose = _display_error(err, shell);
120+
let has_verbose = _display_error(err, shell, true);
121121
if has_verbose {
122122
drop(writeln!(
123123
shell.err(),
@@ -140,7 +140,15 @@ pub fn display_error(err: &Error, shell: &mut Shell) {
140140
}
141141
}
142142

143-
fn _display_error(err: &Error, shell: &mut Shell) -> bool {
143+
/// Displays a warning, with an error object providing detailed information
144+
/// and context.
145+
pub fn display_warning_with_error(warning: &str, err: &Error, shell: &mut Shell) {
146+
drop(shell.warn(warning));
147+
drop(writeln!(shell.err(), ""));
148+
_display_error(err, shell, false);
149+
}
150+
151+
fn _display_error(err: &Error, shell: &mut Shell, as_err: bool) -> bool {
144152
let verbosity = shell.verbosity();
145153
let is_verbose = |e: &(dyn std::error::Error + 'static)| -> bool {
146154
verbosity != Verbose && e.downcast_ref::<VerboseError>().is_some()
@@ -149,7 +157,11 @@ fn _display_error(err: &Error, shell: &mut Shell) -> bool {
149157
if is_verbose(err.as_ref()) {
150158
return true;
151159
}
152-
drop(shell.error(&err));
160+
if as_err {
161+
drop(shell.error(&err));
162+
} else {
163+
drop(writeln!(shell.err(), "{}", err));
164+
}
153165
for cause in err.chain().skip(1) {
154166
// If we're not in verbose mode then print remaining errors until one
155167
// marked as `VerboseError` appears.

src/cargo/ops/cargo_doc.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,8 @@ fn open_docs(path: &Path, shell: &mut Shell) -> CargoResult<()> {
104104
}
105105
None => {
106106
if let Err(e) = opener::open(&path) {
107-
shell.warn(format!("Couldn't open docs: {}", e))?;
108-
for cause in anyhow::Error::new(e).chain().skip(1) {
109-
shell.warn(format!("Caused by:\n {}", cause))?;
110-
}
107+
let e = e.into();
108+
crate::display_warning_with_error("couldn't open docs", &e, shell);
111109
}
112110
}
113111
};

src/cargo/ops/cargo_new.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,12 @@ mod tests {
762762
}
763763

764764
if let Err(e) = Workspace::new(&path.join("Cargo.toml"), config) {
765-
let msg = format!(
765+
crate::display_warning_with_error(
766766
"compiling this new crate may not work due to invalid \
767-
workspace configuration\n\n{:?}",
768-
e,
767+
workspace configuration",
768+
&e,
769+
&mut config.shell(),
769770
);
770-
config.shell().warn(msg)?;
771771
}
772772

773773
Ok(())

tests/testsuite/workspaces.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1037,8 +1037,7 @@ fn new_warns_you_this_will_not_work() {
10371037
.env("USER", "foo")
10381038
.with_stderr(
10391039
"\
1040-
warning: compiling this new crate may not work due to invalid workspace \
1041-
configuration
1040+
warning: compiling this new crate may not work due to invalid workspace configuration
10421041
10431042
current package believes it's in a workspace when it's not:
10441043
current: [..]
@@ -1065,8 +1064,10 @@ fn new_warning_with_corrupt_ws() {
10651064
failed to parse manifest at `[..]foo/Cargo.toml`
10661065
10671066
Caused by:
1068-
0: could not parse input as TOML
1069-
1: expected an equals, found eof at line 1 column 5
1067+
could not parse input as TOML
1068+
1069+
Caused by:
1070+
expected an equals, found eof at line 1 column 5
10701071
Created binary (application) `bar` package
10711072
",
10721073
)

0 commit comments

Comments
 (0)