-
Notifications
You must be signed in to change notification settings - Fork 900
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
Support @generated marker to skip code formatting #4877
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hi 👋 thanks for opening the PR and the detailed data in the description!
This is actually a duplicate of #3958 with a different resolution. #3958 was resolved via #4296 but this was done against a different, experimental branch in the project for a potential v2 of rustfmt, and the support for @generated
hasn't yet made into the rustfmt mainline and release train.
If you'd be interested in trying to grab those commits from #4296 (can be found on the rustfmt-2.0.0-rc2 branch https://github.com/rust-lang/rustfmt/tree/rustfmt-2.0.0-rc.2) then I'd be happy to get those changes incorporated into rustfmt. However, I don't want to move forward with the alternative implementation you've proposed here, as always running an up front full text scan of every file is too open ended and involved considering that generators can easily stick the marker at the top of file as #4296 supports
I did not get it, that branch is called RC, does it means it will be released soon? There are two issues with that implementation:
So I did a little benchmark. On my mac laptop the results are:
Of course, it is not life and death issue either. What I would do:
|
No, and don't really want to get off topic here. The only point is that an implementation was already added, but on a different branch.
sure, but keep in mind that the skip with
I'm not sure I follow. In both cases, including the existing implementation as well as your initial proposal, the file is still parsed to produce an AST prior to checking any contents in the associated span.
Can you elaborate on why this matters? The whole point of generated code in this context is that it's not human written and rustfmt shouldn't have to be concerned with the arbitrary nature of human-written code like we do everywhere else, since any generators leveraging this pattern should be able to easily conform to a standard pattern. There's no valid use case I can see for generators needing to stick That's not a risk I'm willing to accept. If a generator wants to use this alternative instead of the other, more commonly used options, then it seems entirely reasonable to stick something at (or near) the top of the file. I'm open to improvements/alternatives to what's already been incorporated, but not with what's originally been proposed here. Additionally, the original implementation should be improvable now, as we already have the corresponding span for the module which will cover the entire file (subsequent upstream AST changes), and we can peek inside the corresponding snippet without have to (potentially) open the file again. The only question really is: how much of that snippet do we really need to search? If you have some examples/benefits of expanding that beyond the first line that'd be helpful and will certainly consider, but the answer can't be "the entire snippet" 😄 |
Was just trying to understand how things work. In particular, against which branch I need to submit a PR. Now I got it, against master, and that 2-branch is not going anywhere soon. Just that -rc name confused me.
Let's skip this part of the discussion, because I think we are on the same page: implementation should be efficient.
so quite a lot of generated markers are not on the first line. A lot of markers are on the second line because it is python or shell scripts which require shebang on the first line. But sometimes people place it on the third line and further. I don't know why people do that. Probably from some aesthetic preferences. That's why I suggested using top 5 lines.
I think there are no practical reasons to put in in the end of the file, but there might be reasons to read it below 5th line, for example:
But I'm fine with allowing |
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.
OK, I picked #4296 and applied the following changes:
Good or something need to be adjusted? |
Thanks @stepancheg. I didn't get a chance to look at the updates yet, but I like the idea of combining the best parts of both approaches, particularly since there's been some upstream changes in the AST around the mod spans we can now leverage. Will try to take a look tomorrow |
GitHub super-linter now has opt-in supports for Also, I have created a website to promote using |
Nice! I forgot to post but I'm good with the updated approach. I'd like to add some more tests and want to experiment with some simplification based on the spans, but I'll handle that separately. I have some PR juggling to do with a subtree sync so will be a bit longer before this makes it into nightly |
@generated
marker is used by certain tools to understand that thefile is generated, so it should be treated differently than a file
written by a human:
This PR proposes builtin support for
@generated
marker.I have not found a standard for a generated file marker, but:
@generated
markeralso understands
@generated
marker@generated
marker into generated Cargo.lock filesMy personal story is that rust-protobuf project which I maintain
was broken twice because of incompatibilities/bugs in rustfmt marker
handling: one, two.
(Also, rust-protobuf started generating
@generated
marker 6 years ago).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.