Skip to content

Commit 78b1400

Browse files
committed
Show better error message for Windows abnormal termination.
1 parent 3ba5f27 commit 78b1400

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

src/bin/cargo/commands/test.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
use cargo::ops::{self, CompileFilter, FilterRule, LibRule};
2-
31
use crate::command_prelude::*;
2+
use cargo::ops::{self, CompileFilter, FilterRule, LibRule};
3+
use cargo::util::errors;
4+
use failure::Fail;
45

56
pub fn cli() -> App {
67
subcommand("test")
@@ -164,12 +165,15 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
164165
let err = ops::run_tests(&ws, &ops, &test_args)?;
165166
match err {
166167
None => Ok(()),
167-
Some(err) => Err(match err.exit.as_ref().and_then(|e| e.code()) {
168-
Some(i) => CliError::new(
169-
failure::format_err!("{}", err.hint(&ws, &ops.compile_opts)),
170-
i,
171-
),
172-
None => CliError::new(err.into(), 101),
173-
}),
168+
Some(err) => {
169+
let context = failure::format_err!("{}", err.hint(&ws, &ops.compile_opts));
170+
let e = match err.exit.as_ref().and_then(|e| e.code()) {
171+
// Don't show "process didn't exit successfully" for simple errors.
172+
Some(i) if errors::is_simple_exit_code(i) => CliError::new(context, i),
173+
Some(i) => CliError::new(err.context(context).into(), i),
174+
None => CliError::new(err.context(context).into(), 101),
175+
};
176+
Err(e)
177+
}
174178
}
175179
}

src/cargo/core/compiler/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use crate::core::manifest::TargetSourcePath;
4545
use crate::core::profiles::{Lto, PanicStrategy, Profile};
4646
use crate::core::Feature;
4747
use crate::core::{PackageId, Target};
48-
use crate::util::errors::{CargoResult, CargoResultExt, Internal, ProcessError};
48+
use crate::util::errors::{self, CargoResult, CargoResultExt, Internal, ProcessError};
4949
use crate::util::machine_message::Message;
5050
use crate::util::paths;
5151
use crate::util::{self, machine_message, ProcessBuilder};
@@ -271,7 +271,7 @@ fn rustc<'a, 'cfg>(
271271
.as_ref()
272272
.and_then(|perr| perr.exit.and_then(|e| e.code()))
273273
{
274-
Some(n) if n < 128 => Internal::new(err).into(),
274+
Some(n) if errors::is_simple_exit_code(n) => Internal::new(err).into(),
275275
_ => err,
276276
}
277277
}

src/cargo/util/errors.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,17 @@ pub fn process_error(
381381
}
382382
}
383383

384+
pub fn is_simple_exit_code(code: i32) -> bool {
385+
// Typical unix exit codes are 0 to 127.
386+
// Windows doesn't have anything "typical", and is a
387+
// 32-bit number (which appears signed here, but is really
388+
// unsigned). However, most of the interesting NTSTATUS
389+
// codes are very large. This is just a rough
390+
// approximation of which codes are "normal" and which
391+
// ones are abnormal termination.
392+
code >= 0 && code <= 127
393+
}
394+
384395
pub fn internal<S: fmt::Display>(error: S) -> failure::Error {
385396
_internal(&error)
386397
}

0 commit comments

Comments
 (0)