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

handle some block comment to detect generated files #1161

Merged
merged 3 commits into from May 25, 2020

Conversation

ksoichiro
Copy link
Member

I changed autogenerated_exclude.go to handle block comments partially to detect generated comment inside or after the block comments.

I avoided to change to read whole file, because it might affect to performance.
Although the generated comment text may appear anywhere in the file, this change still only see around the top of the file.
It will quit reading when the line is other than comments, package statement, and empty.

I'm not sure it can be accepted because it's not a perfect solution, but I think it will be improved than it is now.

Related: #1083

for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "//") {
if inBlockComment {
Copy link
Member

Choose a reason for hiding this comment

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

sorry, I don't think it's a good idea to manually parse comments.
Maybe we can use ast.ParseFile with flag like PackageClauseOnly or similar to stop early? I'm sure that performance won't be an issue here if we implement it properly.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for your advice.
I was actually on the fence about using AST, but I'll try to do that.

Copy link
Member Author

Choose a reason for hiding this comment

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

I've changed to stop parsing manually by using parser.ParseFile().
Although parser.ParseFile() reads entire file regardless of mode, with PackageClauseOnly mode (and ParseComments mode), it stops parsing files after import statements, as you pointed out.

Copy link
Member

Choose a reason for hiding this comment

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

The text may appear anywhere in the file.

golang/go#13560 (comment)

Are we sure that avoiding possible performance regression has higher priority that compliance with convention?

Copy link
Member

Choose a reason for hiding this comment

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

I agree that it looks more important. I guess time still will be <100ms total. We can easily check it: golangci-lint run -v shows all processors' times.

Copy link
Member Author

Choose a reason for hiding this comment

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

Do you mean compliance with convention is more important?

Suppose you think so.. performance regression depends on how many issues will be reported on generated files, so I think it's difficult to choose which repository to measure for this purpose. But as you explain in usage, parsing file seems not so much cost.

Parsing one source file takes 200 us on average. Parsing of all files in $GOROOT/src takes 2 seconds. Currently we parse each file more than once because it's not the bottleneck.

However, you also write like this:

We're planning to parse each file only once.

If we check entire file for comments, should we use existing AST instead of parsing file here again?
I haven't confirmed if it's possible yet, I guess we can do so since this function is called on reporting phase.


// Issue 954: Some lines can be very long, e.g. auto-generated
// embedded resources. Reported on file of 86.2KB.
func TestGetDocFileWithLongLine(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This change replaces the whole implementation of getDoc(), so I also added a test for confirming previous issue (#954).

Copy link
Member

@jirfag jirfag left a comment

Choose a reason for hiding this comment

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

thank you,
@ernado we can add parsing of the full file later, I'm ok with that,
now merging

@jirfag jirfag merged commit 71b2f04 into golangci:master May 25, 2020
@ldez ldez added this to the v1.28 milestone Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve generated files detection unparam does not skip autogenerated files
4 participants