JSON Output
This chapter documents the JSON structures emitted by rustc. JSON may be
enabled with the --error-format=json flag. Additional
options may be specified with the --json flag which can
change which messages are generated, and the format of the messages.
JSON messages are emitted one per line to stderr.
If parsing the output with Rust, the
cargo_metadata crate provides
some support for parsing the messages.
Each type of message has a $message_type field which can be used to
distinguish the different formats. When parsing, care should be taken
to be forwards-compatible with future changes to the format. Optional
values may be null. New fields may be added. Enumerated fields like
"level" or "suggestion_applicability" may add new values.
Diagnostics
Diagnostic messages provide errors or possible concerns generated during
compilation. rustc provides detailed information about where the diagnostic
originates, along with hints and suggestions.
Diagnostics are arranged in a parent/child relationship where the parent diagnostic value is the core of the diagnostic, and the attached children provide additional context, help, and information.
Diagnostics have the following format:
Artifact notifications
Artifact notifications are emitted when the --json=artifacts
flag is used. They indicate that a file artifact has been saved
to disk. More information about emit kinds may be found in the --emit
flag documentation. Notifications can contain more than one file
for each type, for example when using multiple codegen units.
Future-incompatible reports
If the --json=future-incompat flag is used, then a separate
JSON structure will be emitted if the crate may stop compiling in the future.
This contains diagnostic information about the particular warnings that may be
turned into a hard error in the future. This will include the diagnostic
information, even if the diagnostics have been suppressed (such as with an
#[allow] attribute or the --cap-lints option).
Unused Dependency Notifications
The options --json=unused-externs and --json=unused-externs-silent in
conjunction with the unused-crate-dependencies lint will emit JSON structures
reporting any crate dependencies (specified with --extern) which never had any
symbols referenced. These are intended to be consumed by the build system which
can then emit diagnostics telling the user to remove the unused dependencies
from Cargo.toml (or whatever build-system file defines dependencies).
The JSON structure is:
The warn/deny/forbid lint level (as defined either on the command line or in the
source) dictates the lint_level in the JSON. With unused-externs, a
deny or forbid level diagnostic will also cause rustc to exit with a
failure exit code.
unused-externs-silent will report the diagnostic the same way, but will not
cause rustc to exit with failure - it's up to the consumer to flag failure
appropriately. (This is needed by Cargo which shares the same dependencies
across multiple build targets, so it should only report an unused dependency if
its not used by any of the targets.)
Timings
This setting is currently unstable and requires usage of -Zunstable-options.
The --timings option will tell rustc to emit messages when a certain compilation
section (such as code generation or linking) begins or ends. The messages currently have
the following format:
Note that the JSON format of the timings messages is unstable and subject to change.
Compilation sections can be nested; for example, if you encounter the start of "foo", then the start of "bar", then the end of "bar" and then the end of "bar", it means that the "bar" section happened as a part of the "foo" section.
The timestamp should only be used for computing the duration of each section.
We currently do not guarantee any specific section names to be emitted.