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

Skip formatting generated files #4296

Merged
merged 1 commit into from
Jul 10, 2020

Conversation

topecongiro
Copy link
Contributor

This PR adds format_generated_files. When this value is false, rustfmt skips formatting generated files. If the value is true, rustfmt formats generated files. format_generated_files is set to false by default.

rustfmt considers files as generated if the file starts with a comment which includes @generated ( e.g., // @generated).

Close #3958.

assert!(is_comment_with_generated_notation("// @generated"));
assert!(is_comment_with_generated_notation("//@generated\n\n"));
assert!(is_comment_with_generated_notation("\n// @generated"));
assert!(is_comment_with_generated_notation("/* @generated"));
Copy link
Contributor

Choose a reason for hiding this comment

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

consider adding a test for string w/o @generated or not on the first line.

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

LGTM and should resolve the issue, though one inline question/thought on whether we could avoid the extra file read

Configurations.md Show resolved Hide resolved
.and_then(|s| s.lines().next())
.map(str::to_owned)
.unwrap_or("".to_owned()),
FileName::Real(ref path) => fs::File::open(path)
Copy link
Member

Choose a reason for hiding this comment

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

Not ideal to have another file system read (IIRC we're up to at least 3 due with this plus the parser and one for newline checks) 🤔

Would it be feasible to get the first line without the file system hit, maybe via the parse session data? Pperhaps it won't matter that much given the default is false, and/or could potentially be updated later

Copy link
Contributor

Choose a reason for hiding this comment

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

(forgive me for the intrusion, i was recently doing work in this area and learned this: unless you're very low on memory, recently-accessed files are cached by the kernel and effectively free to access; the slowest part will be parsing)

Copy link
Member

@calebcartwright calebcartwright Jul 1, 2020

Choose a reason for hiding this comment

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

forgive me for the intrusion

no worries at all!

the slowest part will be parsing

agreed, that's basically a given, and understand the main point about not strictly hitting the fs. though the intent of my original question wasn't really about a parsing/AST construction vs. additional file reads, it's whether the additional file read is necessary given that the file's already been read and loaded from the preceding parsing stage and available for use

unless you're very low on memory, recently-accessed files are cached by the kernel and effectively free to access;

do you know if that (especially the effectively free bit) holds true everywhere, for example Windows, CI environments, some of the tier 2/3 targets, etc?

I'm not terribly familiar in that space, so entirely possible this approach is better than an alternative like using the span from the associated mod to get the first line from the snippet for the file, either more performant/negligible difference due to the caching or other reasons, and/or simpler from a code readability and management perspective 🤷‍♂️

Also readily admit such a question may be unnecessary/premature optimization, but figured I'd ask anyway 😄

minor edits for clarity

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah, I agree that reusing data that is already and easily available is preferable. I just wanted to note that the performance implications of rereading a file may not be as high as it may appear, and that there are probably bigger bottlenecks. But I totally agree with you, no need to do extra work if you don't have to.

Re environments: most modern kernels (~last 20 years) have a page cache; this applies to windows as well

Copy link
Member

@calebcartwright calebcartwright Jul 2, 2020

Choose a reason for hiding this comment

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

Re environments: most modern kernels (~last 20 years) have a page cache; this applies to windows as well

Sure, but I suppose I should've phrased my question better.

Was wondering more so if you thought there'd always be sufficient available memory to guarantee/near-guaranatee cache hit in all scenarios. I know even on my laptops I can get really memory constrained (as I tend to use lower end machines and still go overboard with multi tasking 😆), but a lot of CI environments can be pretty low on resources and one of the things I learned during the conversations on the compiler frontend for rls/ra is that there's plenty of folks doing their inner dev loop on some pretty resource constrainted environments, and even some remote workflows.

I genuinely don't know, but wouldn't be surprised if running in some of the more resource constrained environments (or those that purged more aggressively) and/or larger workspace projects could result in some cache misses and i/o hits.

I'm quickly getting out my depth though and will happily defer to @topecongiro on whether it's worth considering an alternative 😄

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I first tried by using the span of ast::Mod, though it turned out that the span may not contain the comment at the beginning or the end of the file. I think that there are ways to get a correct span, though I opted for a simpler implementation this time.

Copy link
Member

Choose a reason for hiding this comment

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

Works for me!

@topecongiro topecongiro merged commit 2c39e80 into rust-lang:master Jul 10, 2020
@topecongiro topecongiro deleted the skip-generated-files branch July 10, 2020 02:23
stepancheg pushed a commit to stepancheg/rustfmt that referenced this pull request Jun 22, 2021
This is a copy of rust-lang#4296 with these changes:
* file is not reopened again to find if the file is generated
* first five lines are scanned for `@generated` marker instead of one
* no attempt is made to only search for marker in comments

`@generated` marker is used by certain tools to understand that the
file is generated, so it should be treated differently than a file
written by a human:
* linters should not be invoked on these files,
* diffs in these files are less important,
* and these files should not be reformatted.

This PR proposes builtin support for `@generated` marker.

I have not found a standard for a generated file marker, but:
* Facebook [uses `@generated` marker](https://tinyurl.com/fb-generated)
* Phabricator tool which was spawned from Facebook internal tool
  [also understands `@generated` marker](https://git.io/JnVHa)
* Cargo inserts `@generated` marker into [generated Cargo.lock files](https://git.io/JnVHP)

My personal story is that rust-protobuf project which I maintain
was broken twice because of incompatibilities/bugs in rustfmt marker
handling: [one](stepancheg/rust-protobuf#493),
[two](stepancheg/rust-protobuf#551).
(Also, rust-protobuf started generating `@generated` marker
[6 years ago](https://git.io/JnV5h)).

While rustfmt AST markers are useful to apply to a certain AST
elements, disable whole-file-at-once all-tools-at-once text level
marker might be easier to use and more reliable for generated code.
stepancheg pushed a commit to stepancheg/rustfmt that referenced this pull request Jun 22, 2021
This is a copy of rust-lang#4296 with these changes:
* file is not reopened again to find if the file is generated
* first five lines are scanned for `@generated` marker instead of one
* no attempt is made to only search for marker in comments

`@generated` marker is used by certain tools to understand that the
file is generated, so it should be treated differently than a file
written by a human:
* linters should not be invoked on these files,
* diffs in these files are less important,
* and these files should not be reformatted.

This PR proposes builtin support for `@generated` marker.

I have not found a standard for a generated file marker, but:
* Facebook [uses `@generated` marker](https://tinyurl.com/fb-generated)
* Phabricator tool which was spawned from Facebook internal tool
  [also understands `@generated` marker](https://git.io/JnVHa)
* Cargo inserts `@generated` marker into [generated Cargo.lock files](https://git.io/JnVHP)

My personal story is that rust-protobuf project which I maintain
was broken twice because of incompatibilities/bugs in rustfmt marker
handling: [one](stepancheg/rust-protobuf#493),
[two](stepancheg/rust-protobuf#551).
(Also, rust-protobuf started generating `@generated` marker
[6 years ago](https://git.io/JnV5h)).

While rustfmt AST markers are useful to apply to a certain AST
elements, disable whole-file-at-once all-tools-at-once text level
marker might be easier to use and more reliable for generated code.
calebcartwright pushed a commit that referenced this pull request Sep 8, 2021
This is a copy of #4296 with these changes:
* file is not reopened again to find if the file is generated
* first five lines are scanned for `@generated` marker instead of one
* no attempt is made to only search for marker in comments

`@generated` marker is used by certain tools to understand that the
file is generated, so it should be treated differently than a file
written by a human:
* linters should not be invoked on these files,
* diffs in these files are less important,
* and these files should not be reformatted.

This PR proposes builtin support for `@generated` marker.

I have not found a standard for a generated file marker, but:
* Facebook [uses `@generated` marker](https://tinyurl.com/fb-generated)
* Phabricator tool which was spawned from Facebook internal tool
  [also understands `@generated` marker](https://git.io/JnVHa)
* Cargo inserts `@generated` marker into [generated Cargo.lock files](https://git.io/JnVHP)

My personal story is that rust-protobuf project which I maintain
was broken twice because of incompatibilities/bugs in rustfmt marker
handling: [one](stepancheg/rust-protobuf#493),
[two](stepancheg/rust-protobuf#551).
(Also, rust-protobuf started generating `@generated` marker
[6 years ago](https://git.io/JnV5h)).

While rustfmt AST markers are useful to apply to a certain AST
elements, disable whole-file-at-once all-tools-at-once text level
marker might be easier to use and more reliable for generated code.
calebcartwright pushed a commit to calebcartwright/rustfmt that referenced this pull request Sep 15, 2021
This is a copy of rust-lang#4296 with these changes:
* file is not reopened again to find if the file is generated
* first five lines are scanned for `@generated` marker instead of one
* no attempt is made to only search for marker in comments

`@generated` marker is used by certain tools to understand that the
file is generated, so it should be treated differently than a file
written by a human:
* linters should not be invoked on these files,
* diffs in these files are less important,
* and these files should not be reformatted.

This PR proposes builtin support for `@generated` marker.

I have not found a standard for a generated file marker, but:
* Facebook [uses `@generated` marker](https://tinyurl.com/fb-generated)
* Phabricator tool which was spawned from Facebook internal tool
  [also understands `@generated` marker](https://git.io/JnVHa)
* Cargo inserts `@generated` marker into [generated Cargo.lock files](https://git.io/JnVHP)

My personal story is that rust-protobuf project which I maintain
was broken twice because of incompatibilities/bugs in rustfmt marker
handling: [one](stepancheg/rust-protobuf#493),
[two](stepancheg/rust-protobuf#551).
(Also, rust-protobuf started generating `@generated` marker
[6 years ago](https://git.io/JnV5h)).

While rustfmt AST markers are useful to apply to a certain AST
elements, disable whole-file-at-once all-tools-at-once text level
marker might be easier to use and more reliable for generated code.
calebcartwright pushed a commit that referenced this pull request Sep 15, 2021
This is a copy of #4296 with these changes:
* file is not reopened again to find if the file is generated
* first five lines are scanned for `@generated` marker instead of one
* no attempt is made to only search for marker in comments

`@generated` marker is used by certain tools to understand that the
file is generated, so it should be treated differently than a file
written by a human:
* linters should not be invoked on these files,
* diffs in these files are less important,
* and these files should not be reformatted.

This PR proposes builtin support for `@generated` marker.

I have not found a standard for a generated file marker, but:
* Facebook [uses `@generated` marker](https://tinyurl.com/fb-generated)
* Phabricator tool which was spawned from Facebook internal tool
  [also understands `@generated` marker](https://git.io/JnVHa)
* Cargo inserts `@generated` marker into [generated Cargo.lock files](https://git.io/JnVHP)

My personal story is that rust-protobuf project which I maintain
was broken twice because of incompatibilities/bugs in rustfmt marker
handling: [one](stepancheg/rust-protobuf#493),
[two](stepancheg/rust-protobuf#551).
(Also, rust-protobuf started generating `@generated` marker
[6 years ago](https://git.io/JnV5h)).

While rustfmt AST markers are useful to apply to a certain AST
elements, disable whole-file-at-once all-tools-at-once text level
marker might be easier to use and more reliable for generated code.
@ytmimi
Copy link
Contributor

ytmimi commented Apr 1, 2022

Seems like this was already backported in #4990

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.

Add option to skip generated files
5 participants