Skip to content
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

Add config option generated_file_header_size #5659

Closed

Conversation

captbaritone
Copy link
Contributor

@captbaritone captbaritone commented Jan 14, 2023

Fixes #5658

FYI @ytmimi

@captbaritone
Copy link
Contributor Author

I wonder if it would be preferable to make format_generated_files a richer config option which the line number being a sub-option that can only be configured when the rule is enabled. As it stands, generated_file_header_size does not really make sense if format_generated_files is not enabled.

@ytmimi
Copy link
Contributor

ytmimi commented Jan 15, 2023

@captbaritone thanks for your first contribution to rustfmt 🎉

The tests are failing because we're missing an entry in Configurations.md for the new config option. additionally it would be nice to include some tests to validate the behavior of the new option.

I wonder if it would be preferable to make format_generated_files a richer config option which the line number being a sub-option that can only be configured when the rule is enabled.

Interesting idea! Are you suggesting the configuration should be something like this:

// Default. The file is always formatted
format_generated_files = true

// Disable formatting if the `@generated` marker is found within the first 5 lines of the file.
format_generated_files = false

// Disable formatting if the `@generated` marker is found within the first n lines of the file.
format_generated_files = <usize>

@ytmimi
Copy link
Contributor

ytmimi commented Jan 18, 2023

Upon further consideration the format_generated_files = <usize> idea I brought up in my previous comment seems a little clunky and counter intuitive since the config is named format_generated_files and what we're trying to do here is configure the number of lines to search for the @generated marker to prevent formatting.

Let me know if you can come up with anything better than the current approach of generated_file_header_size. Personally I think it would suffice to document that the option has no impact if format_generated_files is true (the default).


- **Default value**: `5`
- **Possible values**: any positive integer
- **Stable**: No (tracking issue: [#5080](https://github.com/rust-lang/rustfmt/issues/5080))
Copy link
Contributor Author

@captbaritone captbaritone Jan 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these two config options be promoted to stable together? Or should I create a new issue to track promoting this option?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's would be fine to stabilize these at the same time.

@captbaritone
Copy link
Contributor Author

@ytmimi I've added documentation and tests.

@captbaritone
Copy link
Contributor Author

Hey, @ytmimi just following up to see if there's any feedback on this.

By default, generated files are reformatted, i. e. `@generated` marker is ignored.
This option is currently ignored for stdin (`@generated` in stdin is ignored.)

- **Default value**: `true`
- **Possible values**: `true`, `false`
- **Stable**: No (tracking issue: [#5080](https://github.com/rust-lang/rustfmt/issues/5080))

## `generated_file_header_size`

Number of lines to check for a `@generated` pragma header when `format_generated_files` is enabled. When `format_generated_files` is disabled, this option has no effect.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's reword this to be clearer about when this option has an effect:

Number of lines to check for a @generated pragma header when format_generated_files is false. When format_generated_files is true, this option has no effect.

- **Default value**: `5`
- **Possible values**: any positive integer
- **Stable**: No (tracking issue: [#5080](https://github.com/rust-lang/rustfmt/issues/5080))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add a See also [format_generated_files](#format_generated_files) link here.

Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this!

The implementation looks good, I'd just want us to make some minor tweaks to the documentation. Additionally can we add two more test cases:

  1. Add a test case where format_generated_files=true and generated_file_header_size is set to some value where the @generated marker could be found. This would better test that when format_generated_files=true is set generated_file_header_size has no effect.
  2. Add a test cast where format_generated_files=false and generated_file_header_size is set to some value where the @generated marker can't be found. This would show that even when format_generated_files=false we'll still format the file if we can't find the @generaged marker.

@calebcartwright
Copy link
Member

Just want to add that I'm open to allowing the user to configure this value, but I haven't had a chance to consider this particular approach and I don't know if this is a significantly high enough priority for us to consider for release in the immediate future

@captbaritone
Copy link
Contributor Author

@calebcartwright Thanks for the honesty. I'll wait for you to consider the approach more closely before I invest more time in this PR.

As it stands we are blocked from running rustftm from the CLI (needed to enforce formatting in CI) and must be careful to avoid saving generated files in our editor. Is there a workaround that you would recommend in the mean time?

@calebcartwright
Copy link
Member

I'll wait for you to consider the approach more closely before I invest more time in this PR.

To be clear, we'll absolutely support adding an option that allows for this to be controlled, just need a chance to catch up on the back and forth within this thread as well as consider the specifics (as one initial reaction, something bothers me about the currently proposed option name as I don't think it's as self-explanatory as we'd like to see)

As it stands we are blocked from running rustftm from the CLI (needed to enforce formatting in CI) and must be careful to avoid saving generated files in our editor. Is there a workaround that you would recommend in the mean time?
Merge state

Sorry not sure what you're asking. If you want to (temporarily) block folks from running rustfmt, intentionally or accidentally, the best route would be to enable disable_all_formatting. If you want to permit formatting in general but only skip generated files, then ignore would likely be a viable option, assuming you're on a nightly toolchain

@ytmimi
Copy link
Contributor

ytmimi commented Jan 25, 2024

closing since we merged #5993

@ytmimi ytmimi closed this Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Option to configure how many lines down the @generated tag can be for format_generated_files?
3 participants