Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
calebcartwright opened this issue Dec 4, 2019 · 1 comment · Fixed by #3953
Closed

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

calebcartwright opened this issue Dec 4, 2019 · 1 comment · Fixed by #3953
Labels
bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce

Comments

@calebcartwright
Copy link
Member

calebcartwright commented Dec 4, 2019

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)

@topecongiro topecongiro added bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce labels Dec 9, 2019
@bobbobbio
Copy link

In #3947 you mentioned that this issue was blocking stabilization of --emit json, with this issue closed is there now a tracking issue somewhere for the stabilization?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Panic, non-idempotency, invalid code, etc. only-with-option requires a non-default option value to reproduce
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants