Skip to content

Commit

Permalink
Show better error message for Windows abnormal termination.
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Oct 23, 2019
1 parent 3ba5f27 commit 78b1400
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
22 changes: 13 additions & 9 deletions src/bin/cargo/commands/test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use cargo::ops::{self, CompileFilter, FilterRule, LibRule};

use crate::command_prelude::*;
use cargo::ops::{self, CompileFilter, FilterRule, LibRule};
use cargo::util::errors;
use failure::Fail;

pub fn cli() -> App {
subcommand("test")
Expand Down Expand Up @@ -164,12 +165,15 @@ pub fn exec(config: &mut Config, args: &ArgMatches<'_>) -> CliResult {
let err = ops::run_tests(&ws, &ops, &test_args)?;
match err {
None => Ok(()),
Some(err) => Err(match err.exit.as_ref().and_then(|e| e.code()) {
Some(i) => CliError::new(
failure::format_err!("{}", err.hint(&ws, &ops.compile_opts)),
i,
),
None => CliError::new(err.into(), 101),
}),
Some(err) => {
let context = failure::format_err!("{}", err.hint(&ws, &ops.compile_opts));
let e = match err.exit.as_ref().and_then(|e| e.code()) {
// Don't show "process didn't exit successfully" for simple errors.
Some(i) if errors::is_simple_exit_code(i) => CliError::new(context, i),
Some(i) => CliError::new(err.context(context).into(), i),
None => CliError::new(err.context(context).into(), 101),
};
Err(e)
}
}
}
4 changes: 2 additions & 2 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ use crate::core::manifest::TargetSourcePath;
use crate::core::profiles::{Lto, PanicStrategy, Profile};
use crate::core::Feature;
use crate::core::{PackageId, Target};
use crate::util::errors::{CargoResult, CargoResultExt, Internal, ProcessError};
use crate::util::errors::{self, CargoResult, CargoResultExt, Internal, ProcessError};
use crate::util::machine_message::Message;
use crate::util::paths;
use crate::util::{self, machine_message, ProcessBuilder};
Expand Down Expand Up @@ -271,7 +271,7 @@ fn rustc<'a, 'cfg>(
.as_ref()
.and_then(|perr| perr.exit.and_then(|e| e.code()))
{
Some(n) if n < 128 => Internal::new(err).into(),
Some(n) if errors::is_simple_exit_code(n) => Internal::new(err).into(),
_ => err,
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/cargo/util/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,17 @@ pub fn process_error(
}
}

pub fn is_simple_exit_code(code: i32) -> bool {
// Typical unix exit codes are 0 to 127.
// Windows doesn't have anything "typical", and is a
// 32-bit number (which appears signed here, but is really
// unsigned). However, most of the interesting NTSTATUS
// codes are very large. This is just a rough
// approximation of which codes are "normal" and which
// ones are abnormal termination.
code >= 0 && code <= 127
}

pub fn internal<S: fmt::Display>(error: S) -> failure::Error {
_internal(&error)
}
Expand Down

0 comments on commit 78b1400

Please sign in to comment.