Add support for `cargo --explain`
authorAlex Crichton <alex@alexcrichton.com>
Fri, 8 Apr 2016 00:27:49 +0000 (17:27 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Thu, 21 Apr 2016 16:46:12 +0000 (09:46 -0700)
The error messages in the compiler are being tweaked and will likely drop the
`rustc --explain` part of the error message in favor of `--explain`. In that
case you're expected to basically take whatever tool you're using and pass
`--explain` to it with an error code, so let's add it to Cargo as well!

src/bin/cargo.rs
tests/test_cargo.rs

index be0f9b3f22483177c1475a33193bb6ac9ddcb682..2d0a15d6bf2f3f7e85af5868d73464fe75cffbc6 100644 (file)
@@ -15,6 +15,7 @@ use cargo::core::shell::Verbosity;
 use cargo::execute_main_without_stdin;
 use cargo::util::ChainError;
 use cargo::util::{self, CliResult, lev_distance, Config, human, CargoResult};
+use cargo::util::process_builder::process;
 
 #[derive(RustcDecodable)]
 pub struct Flags {
@@ -23,6 +24,7 @@ pub struct Flags {
     flag_verbose: Option<bool>,
     flag_quiet: Option<bool>,
     flag_color: Option<String>,
+    flag_explain: Option<String>,
     arg_command: String,
     arg_args: Vec<String>,
 }
@@ -38,6 +40,7 @@ Options:
     -h, --help          Display this message
     -V, --version       Print version info and exit
     --list              List installed commands
+    --explain CODE      Run `rustc --explain CODE`
     -v, --verbose       Use verbose output
     -q, --quiet         No output printed to stdout
     --color WHEN        Coloring: auto, always, never
@@ -129,6 +132,12 @@ fn execute(flags: Flags, config: &Config) -> CliResult<Option<()>> {
         return Ok(None)
     }
 
+    if let Some(ref code) = flags.flag_explain {
+        try!(process(config.rustc()).arg("--explain").arg(code).exec()
+                                    .map_err(human));
+        return Ok(None)
+    }
+
     let args = match &flags.arg_command[..] {
         // For the commands `cargo` and `cargo help`, re-execute ourselves as
         // `cargo -h` so we can go through the normal process of printing the
index ebd69024cc2c449692354fc8210c5fff1673a6d2..1dd5b3ed92dd799817a34b892e329525dc0bfe0b 100644 (file)
@@ -162,3 +162,8 @@ test!(cargo_help {
     assert_that(cargo_process().arg("help").arg("help"),
                 execs().with_status(0));
 });
+
+test!(explain {
+    assert_that(cargo_process().arg("--explain").arg("E0001"),
+                execs().with_status(0));
+});