JSON Output

This chapter documents the JSON structures emitted by rustc. JSON may beenabled with the —error-format=json flag. Additionaloptions may be specified with the —json flag which canchange 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, thecargo_metadata crate providessome support for parsing the messages.

When parsing, care should be taken to be forwards-compatible with future changesto the format. Optional values may be null. New fields may be added. Enumeratedfields like "level" or "suggestion_applicability" may add new values.

Diagnostics

Diagnostic messages provide errors or possible concerns generated duringcompilation. rustc provides detailed information about where the diagnosticoriginates, along with hints and suggestions.

Diagnostics are arranged in a parent/child relationship where the parentdiagnostic value is the core of the diagnostic, and the attached childrenprovide additional context, help, and information.

Diagnostics have the following format:

  1. {
  2. /* The primary message. */
  3. "message": "unused variable: `x`",
  4. /* The diagnostic code.
  5. Some messages may set this value to null.
  6. */
  7. "code": {
  8. /* A unique string identifying which diagnostic triggered. */
  9. "code": "unused_variables",
  10. /* An optional string explaining more detail about the diagnostic code. */
  11. "explanation": null
  12. },
  13. /* The severity of the diagnostic.
  14. Values may be:
  15. - "error": A fatal error that prevents compilation.
  16. - "warning": A possible error or concern.
  17. - "note": Additional information or context about the diagnostic.
  18. - "help": A suggestion on how to resolve the diagnostic.
  19. - "failure-note": A note attached to the message for further information.
  20. - "error: internal compiler error": Indicates a bug within the compiler.
  21. */
  22. "level": "warning",
  23. /* An array of source code locations to point out specific details about
  24. where the diagnostic originates from. This may be empty, for example
  25. for some global messages, or child messages attached to a parent.
  26. Character offsets are offsets of Unicode Scalar Values.
  27. */
  28. "spans": [
  29. {
  30. /* The file where the span is located.
  31. For spans located within a macro expansion, this will be the
  32. name of the expanded macro in the format "<MACRONAME macros>".
  33. */
  34. "file_name": "lib.rs",
  35. /* The byte offset where the span starts (0-based, inclusive). */
  36. "byte_start": 21,
  37. /* The byte offset where the span ends (0-based, exclusive). */
  38. "byte_end": 22,
  39. /* The first line number of the span (1-based, inclusive). */
  40. "line_start": 2,
  41. /* The last line number of the span (1-based, inclusive). */
  42. "line_end": 2,
  43. /* The first character offset of the line_start (1-based, inclusive). */
  44. "column_start": 9,
  45. /* The last character offset of the line_end (1-based, exclusive). */
  46. "column_end": 10,
  47. /* Whether or not this is the "primary" span.
  48. This indicates that this span is the focal point of the
  49. diagnostic.
  50. There are rare cases where multiple spans may be marked as
  51. primary. For example, "immutable borrow occurs here" and
  52. "mutable borrow ends here" can be two separate primary spans.
  53. The top (parent) message should always have at least one
  54. primary span, unless it has zero spans. Child messages may have
  55. zero or more primary spans.
  56. */
  57. "is_primary": true,
  58. /* An array of objects showing the original source code for this
  59. span. This shows the entire lines of text where the span is
  60. located. A span across multiple lines will have a separate
  61. value for each line.
  62. */
  63. "text": [
  64. {
  65. /* The entire line of the original source code. */
  66. "text": " let x = 123;",
  67. /* The first character offset of the line of
  68. where the span covers this line (1-based, inclusive). */
  69. "highlight_start": 9,
  70. /* The last character offset of the line of
  71. where the span covers this line (1-based, exclusive). */
  72. "highlight_end": 10
  73. }
  74. ],
  75. /* An optional message to display at this span location.
  76. This is typically null for primary spans.
  77. */
  78. "label": null,
  79. /* An optional string of a suggested replacement for this span to
  80. solve the issue. Tools may try to replace the contents of the
  81. span with this text.
  82. */
  83. "suggested_replacement": null,
  84. /* An optional string that indicates the confidence of the
  85. "suggested_replacement". Tools may use this value to determine
  86. whether or not suggestions should be automatically applied.
  87. Possible values may be:
  88. - "MachineApplicable": The suggestion is definitely what the
  89. user intended. This suggestion should be automatically
  90. applied.
  91. - "MaybeIncorrect": The suggestion may be what the user
  92. intended, but it is uncertain. The suggestion should result
  93. in valid Rust code if it is applied.
  94. - "HasPlaceholders": The suggestion contains placeholders like
  95. `(...)`. The suggestion cannot be applied automatically
  96. because it will not result in valid Rust code. The user will
  97. need to fill in the placeholders.
  98. - "Unspecified": The applicability of the suggestion is unknown.
  99. */
  100. "suggestion_applicability": null,
  101. /* An optional object indicating the expansion of a macro within
  102. this span.
  103. If a message occurs within a macro invocation, this object will
  104. provide details of where within the macro expansion the message
  105. is located.
  106. */
  107. "expansion": {
  108. /* The span of the macro invocation.
  109. Uses the same span definition as the "spans" array.
  110. */
  111. "span": {/*...*/}
  112. /* Name of the macro, such as "foo!" or "#[derive(Eq)]". */
  113. "macro_decl_name": "some_macro!",
  114. /* Optional span where the relevant part of the macro is
  115. defined. */
  116. "def_site_span": {/*...*/},
  117. }
  118. }
  119. ],
  120. /* Array of attached diagnostic messages.
  121. This is an array of objects using the same format as the parent
  122. message. Children are not nested (children do not themselves
  123. contain "children" definitions).
  124. */
  125. "children": [
  126. {
  127. "message": "`#[warn(unused_variables)]` on by default",
  128. "code": null,
  129. "level": "note",
  130. "spans": [],
  131. "children": [],
  132. "rendered": null
  133. },
  134. {
  135. "message": "consider prefixing with an underscore",
  136. "code": null,
  137. "level": "help",
  138. "spans": [
  139. {
  140. "file_name": "lib.rs",
  141. "byte_start": 21,
  142. "byte_end": 22,
  143. "line_start": 2,
  144. "line_end": 2,
  145. "column_start": 9,
  146. "column_end": 10,
  147. "is_primary": true,
  148. "text": [
  149. {
  150. "text": " let x = 123;",
  151. "highlight_start": 9,
  152. "highlight_end": 10
  153. }
  154. ],
  155. "label": null,
  156. "suggested_replacement": "_x",
  157. "suggestion_applicability": "MachineApplicable",
  158. "expansion": null
  159. }
  160. ],
  161. "children": [],
  162. "rendered": null
  163. }
  164. ],
  165. /* Optional string of the rendered version of the diagnostic as displayed
  166. by rustc. Note that this may be influenced by the `--json` flag.
  167. */
  168. "rendered": "warning: unused variable: `x`\n --> lib.rs:2:9\n |\n2 | let x = 123;\n | ^ help: consider prefixing with an underscore: `_x`\n |\n = note: `#[warn(unused_variables)]` on by default\n\n"
  169. }

Artifact notifications

Artifact notifications are emitted when the —json=artifactsflag is used. They indicate that a file artifact has been savedto disk. More information about emit kinds may be found in the —emitflag documentation.

  1. {
  2. /* The filename that was generated. */
  3. "artifact": "libfoo.rlib",
  4. /* The kind of artifact that was generated. Possible values:
  5. - "link": The generated crate as specified by the crate-type.
  6. - "dep-info": The `.d` file with dependency information in a Makefile-like syntax.
  7. - "metadata": The Rust `.rmeta` file containing metadata about the crate.
  8. - "save-analysis": A JSON file emitted by the `-Zsave-analysis` feature.
  9. */
  10. "emit": "link"
  11. }