rust-lang / rustfmt

Format Rust code
https://rust-lang.github.io/rustfmt/
Apache License 2.0
6.06k stars 892 forks source link

Global config clashes with CI formatting checks that assume the default #6312

Closed kornelski closed 2 months ago

kornelski commented 2 months ago

It's pretty common to run cargo fmt --check in CI to firmly enforce a very specific code formatting. However, usually projects do not have, and may not want to add a rustfmt.toml file to their project to specify their required formatting style explicitly (by having an empty file that resets the settings to default).

fmt --check enforcement combined with the lack of a config file implicitly assumes that every contributor uses the default rustfmt config themselves. This makes existence of global rustfmt config (~/.rustfmt.toml) very contentious. If projects don't reset their rustfmt settings to default, then having any non-default rustfmt config actively ruins formatting in the projects that assume the default, and makes the global settings worse than useless. OTOH it's a big ask to require essentially all Rust projects to add a rustfmt config.

Do you have any ideas how to solve this dilemma?

Currently the ability to change rustfmt's defaults doesn't really work, because instead of having formatting automated and out of mind, it gets in the way and requires manually fixing CI failures due to formatting (some features like comment wrapping make the damage irreversible, so it's even more work than just reformatting a branch with the default config).

The alternative to a global config would be to copy the non-default config to each project using it, but that adds work to keep config copies in sync. An empty rustfmt.toml file is easier to "maintain".

Could there be some other option?

ytmimi commented 2 months ago

Thanks for reaching out. I'm pretty sure this is more or less a duplicate of #6264. I left two workaround suggestions in that issue, but they both involve the user needing to create config files on their system.