rustfmt icon indicating copy to clipboard operation
rustfmt copied to clipboard

[unstable option] `format_generated_files`

Open karyon opened this issue 3 years ago • 10 comments

Tracking issue for unstable option: format_generated_files.

See Processes.md, "Stabilising an Option":

  • [ ] Is the default value correct?
  • [ ] The design and implementation of the option are sound and clean.
  • [ ] The option is well tested, both in unit tests and, optimally, in real usage.
  • [ ] There is no open bug about the option that prevents its use.

karyon avatar Nov 14 '21 09:11 karyon

The latest rust version made false the default, but it's still unstable. This broke software that relied on being able to format generated file (e.g. ours, applying .patch to rustfmt-d generated code), and we can't even re-enable formatting of generated files because this option is marked as unstable.

I think it's inconsistent to have this as an unstable option if the extra code that this feature represents is now enabled by default. Please make this stable asap, or leave true as default as it was before.

Ten0 avatar Jan 14 '22 16:01 Ten0

Thanks for reaching out @Ten0, could you expand a bit on your use case?

IIUC, you've been inserting @generated comments within the first few lines at the top of generated files historically, predating the recent changes to rustfmt that actually recognize this/utilize this new option. And your desire is to be able to use stable rustfmt to format those generated files?

calebcartwright avatar Jan 14 '22 17:01 calebcartwright

We've been using Diesel CLI to generate schema.rs files automatically. This flags files as @generated (legitimately, because they are). We then need to make a few changes to these files. So the process to generate them is:

  • Diesel CLI call to generate the file
  • rustfmt schema.rs
  • patch -i schema.patch ./src/schema.rs

Because rustfmt now doesn't format these files anymore, all our patch files don't work anymore (they are based on formatted file and not un-formatted file, which is desired), hence our CI/CD broke.

Our current workaround will obviously be to patch the outputs of the diesel CLI to remove the @generated flag, but that is obviously incorrect semantically and pretty disgusting. (We also have other generated files related to e.g. gRPC which we occasionally need to read and that don't format anymore...)

Ten0 avatar Jan 14 '22 17:01 Ten0

Gotcha, thanks for confirming.

We do need to get the default updated because we inverted the option itself to avoid double negatives but failed to make the corresponding update to the default. However, I'm not sure whether that'll warrant a 1.58.1 patch release (will check with the release team though)

Here's some tactical alternatives that should be available to you already:

  • Use the command line override e.g. rustfmt schema.rs --config format_generated_files=true (yes, it'll work on stable)
  • Use nightly rustfmt for that specific step, rustfmt +nightly schema.rs
  • Use the 1.57 stable toolchain for whatever step/job is formatting the generated file
  • Adjust the generated file header such that the @generated marker appears below line 5 of the file (realize that may be problematic for other reasons, but mentioning because it would resolve the issue you are experiencing)

calebcartwright avatar Jan 14 '22 17:01 calebcartwright

  • Use the command line override e.g. rustfmt schema.rs --config format_generated_files=true (yes, it'll work on stable)

This will work great as a workaround for now. Thanks! :)

Ten0 avatar Jan 14 '22 17:01 Ten0

Perhaps another thing to consider in terms of promoting this to stable is if we want to bundle it with #5658, the ability to configure the number of header lines that rustfmt will look for for @generated in.

captbaritone avatar Jan 19 '23 05:01 captbaritone

Is there a technical reason this is ignored for stdin?

jhpratt avatar Apr 07 '23 05:04 jhpratt

Is there a technical reason this is ignored for stdin?

Yes, at least in so far as current state behavior. See #5172 and https://github.com/rust-lang/rustfmt/blob/a3b2bfc2db33ec3bed266404e6391c145e000df0/src/formatting.rs#L79-L81

calebcartwright avatar Apr 07 '23 12:04 calebcartwright

@calebcartwright is there an issue or something somewhere else that tracks the stdin support that I can upvote? It would be nice if format-on-save in VS Code would ignore files tagged with // @generated if I happen to hit save while inside them (a rare situation, but jarring nonetheless!). IIUC format-on-save there is run through stdin.

DavisVaughan avatar Feb 14 '24 19:02 DavisVaughan

@calebcartwright is there an issue or something somewhere else that tracks the stdin support that I can upvote? It would be nice if format-on-save in VS Code would ignore files tagged with // @generated if I happen to hit save while inside them (a rare situation, but jarring nonetheless!). IIUC format-on-save there is run through stdin.

Not that I'm aware of, I believe it's just a known issue that would need to be resolved in order for this option to potentially be stabilized

calebcartwright avatar Feb 14 '24 19:02 calebcartwright