Skip to content

Using rustfmt with input on stdin and disable_all_formatting=true leads to empty output #1972

Closed
@nossralf

Description

@nossralf

Sending code to be formatted by rustfmt on stdin (e.g. rustfmt < src/lib.rs), and having disabled all formatting in the corresponding project using a rustfmt.toml file, causes the output from rustfmt to be empty.

The contents of rustfmt.toml was simply:

disable_all_formatting = true

The root cause is format_input which checks for config.disable_all_formatting() and just does an early return if that is true. The exit code from rustfmt is still 0, but there is no output whatsoever. (Because it's actually the formatting visitor that does the outputting, from what I can tell.)

(Edit: This isn't a problem when running rustfmt like rustfmt src/lib.rs because it simply skips the file completely in that case. With input on stdin, "doing nothing" implies echoing the input verbatim, rather than actually doing nothing.)

This is a bug, in my opinion, because it's very useful to be able to have one's editor set to always use rustfmt, but be able to disable it using a rustfmt.toml file for projects that do not use rustfmt at all.

I tried to write a patch myself by simply removing the early return, but that led to source code being modified even though the configuration says to disable all formatting. (I would have expected a byte-for-byte identical output.) If that in itself is a bug or not, I don't know.

(I found this issue when using rust-mode in Emacs, where I have a save-hook set to run rustfmt. Rust-mode sends the source to be formatted on stdin to rustfmt. I was hacking on Cargo -- which doesn't use rustfmt -- and when I tried to avoid unnecessary modifications due to rustfmt by disabling formatting with a rustfmt.toml file, saving a buffer just made the buffer become empty. It turns out rust-mode was using the empty output from rustfmt as the newly formatted source.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugPanic, non-idempotency, invalid code, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions