Skip to content

json emit mode produces invalid json in terminal if rustfmt errs  #3952

Closed
@calebcartwright

Description

@calebcartwright

Whenever rustfmt encounters a condition that results in an eprint/eprintln invocation while formatting multiple files, when using the json emit mode the output in the terminal can be invalid json due to the error text splitting apart the json object.

Technically stdout will contain the valid json result, and stderr the warnings and errors. However, I believe the emitter can be tweaked so that the full/valid json representation of the formatting results can be displayed to users in the terminal without being broken and invalidated.

This is very similar to #1635, but opening as a separate issue for tracking/visbility and because I believe I have a solution for this problem that is emitter-specific.

To reproduce, use any input file that will err on formatting and that also includes a mod defined in another file. For example:

src/lib.rs

mod bar;

fn foo() {
// This line is extra long, and doesn't fit within 100 max_width. 1234567890123456789 aBcDeFgHiffffffffffffffffffffffffffffffffffffffffffffffffffffffffJ
let x = 0;
}

src/bar.rs

pub(crate) fn do_baz() -> bool {
false
}

with rustfmt.toml:

error_on_line_overflow = true
error_on_unformatted = true

Then run cargo fmt or rustfmt in recursive/non-skip_children mode:

$ rustfmt ./src/lib.rs --emit json

and then notice the error displayed with the json object for the two respective files that makes the larger json output invalid
error[internal]: line formatted, but exceeded maximum width (maximum: 100 (see max_width option), found: 253)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.only-with-optionrequires a non-default option value to reproduce

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions