Skip to content

cargo rustc --message-format=json fails to parse stderr of rustc #3390

@fmdkdd

Description

@fmdkdd

Trying the following on a small test crate that contains one error:

$ cargo rustc --message-format=json --lib -- -Z no-trans
   Compiling test-crate v0.1.0 (file://<snip>/test-crate)
error: Could not compile `test-crate`.

To learn more, run the command again with --verbose.

I get no error message. Without -Z no-trans, I do get an error message:

$ cargo rustc --message-format=json --lib
   Compiling test-crate v0.1.0 (file://<snip>/test-crate)
{"reason":"compiler-message",<snip>,"message":{"children":[],"code":{"code":"E0499","explanation":"\nA variable was borrowed as mutable more than once. [...]

This is the expected output.

If I pass --verbose, we can see the error message is output by rustc:

$ cargo rustc --verbose --message-format=json --lib -- -Z no-trans
   Compiling test-crate v0.1.0 (file:<snip>/test-crate)
     Running `rustc src/lib.rs --error-format json --crate-name test_crate --crate-type lib -g -Z no-trans -C metadata=f713ecd4594c2f7f <snip>`
error: Could not compile `test-crate`.

Caused by:
  process didn't exit successfully: `rustc src/lib.rs --error-format json --crate-name test_crate --crate-type lib -g -Z no-trans -C metadata=f713ecd4594c2f7f <snip>` (exit code: 101)
--- stderr
warning: the option `Z` is unstable and should only be used on the nightly compiler, but it is currently accepted for backwards compatibility; this will soon change, see issue #31847 for more details

{"message":"cannot borrow `a` as mutable more than once at a time",<snip>}

But it's not picked up and reported by cargo.

Testing on a crate with no build errors, the causes are reported slightly differently:

$ cargo rustc --verbose --message-format=json -- -Z no-trans
   Compiling good-crate v0.1.0 (file://<snip>/good-crate)
     Running `rustc src/main.rs --error-format json [...]`
error: Could not compile `good-crate`.

Caused by:
  failed to parse process output: `rustc src/main.rs --error-format json [...]` (exit code: 0)
--- stderr
warning: the option `Z` is unstable and should only be used on the nightly compiler, but it is currently accepted for backwards compatibility; this will soon change, see issue #31847 for more details

Caused by:
  compiler produced invalid json: `warning: the option `Z` is unstable and should only be used on the nightly compiler, but it is currently accepted for backwards compatibility; this will soon change, see issue #31847 for more details`

So it seems cargo --message-format parses the JSON of rustc, but does not expect rustc to output anything else than JSON.

$ cargo --version
cargo 0.14.0 (eca9e15 2016-11-01)
$ rustc --version
rustc 1.13.0

I know I'm using an unstable flag on stable rust, so I should heed the warning and all would be fine. But I believe the behavior of --message-format may still be viewed as a bug, since it's not choking on non-JSON output from rustc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions