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

Fix lint errors on files with //line directive #1065

Merged

Conversation

ksoichiro
Copy link
Contributor

This will fix #998.
If the target files contains //line directive and it indicates a non-go file, the linter is going to handle it as a go file, which results in failure.
The cause of this issue is that the linters (Analyzers) are using pass.Fset.Position().
This func returns the adjusted position using //line directive.
The example project reported in #998 has //line directive that indicates other non-go file.
According to the description of Compiler Directives, line directives is mainly used for reporting original positions to the generators or something.
On linters of golangci-lint, pass.Fset.Position() is used just to aggregate file names; we don't have to adjust positions.
This pull request changes Analyzers that use pass.Fset.Position() to aggregate file names to use pass.Fset.PositionFor() with adjusted == false.

If the target files contains `//line` directive and it indicates
a non-go file, the linter is going to handle it as a go file,
which results in failure.
The cause of this issue is that the linters (`Analyzer`s) are using
`pass.Fset.Position()`. This func returns the adjusted position using
`//line` directive.
The example project reported in golangci#998 has `//line` directive that
indicates other non-go file.
According to the description of "Compiler Directives”
(https://golang.org/cmd/compile/#hdr-Compiler_Directives),
line directives is mainly used for reporting original positions to
the generators or something.
On linters of golangci-lint, `pass.Fset.Position()` is used just to
aggregate file names; we don't have to adjust positions.
This changes `Analyzer`s that use `pass.Fset.Position()` to aggregate
file names to use `pass.Fset.PositionFor()` with `adjusted == false`.

Relates: golangci#998
@jirfag
Copy link
Contributor

jirfag commented May 3, 2020

hi, thank you for the pull request!

Please, add some tests for it. For example, when I've done a similar fix I've added these tests.

@ksoichiro
Copy link
Contributor Author

Thank you for you review. I see, I'll do it.

@ksoichiro
Copy link
Contributor Author

I've added tests for the fix.
But some checks of GitHub Actions are failing, and I don't know why...
Should I do something for that?
(It seems #1064 is also failing with same checks.)

@jirfag
Copy link
Contributor

jirfag commented May 3, 2020

thank you, I'll look at it a bit later,
CI fails are ok now

@jirfag jirfag merged commit 7f48cc8 into golangci:master May 5, 2020
@jirfag
Copy link
Contributor

jirfag commented May 5, 2020

thank you!

@ksoichiro ksoichiro deleted the fix-lint-errors-on-files-with-line-directive branch May 6, 2020 14:06
ansiwen added a commit to ansiwen/golangci-lint that referenced this pull request Jul 30, 2022
This change fixes a bug caused that many linters ignored all files that are
using Cgo. It was introduced by PR golangci#1065, which assumed that all files
referenced by //line directives are non-Go files, which is not true. In the case
of Cgo they point to the original Go files which are used by Cgo as templates to
generate other Go files.

The fix replaces all calls of Pass.Fset.PositionFor with a function
`getFileNames()` that first calls Pass.Fset.PositionFor with adjustment, and
only if it results in a non-Go file it calls Pass.Fset.PositionFor again without
adjustment.

Fixes: golangci#2910

Signed-off-by: Sven Anderson <sven@anderson.de>
ansiwen added a commit to ansiwen/golangci-lint that referenced this pull request Jul 30, 2022
This change fixes a bug caused that many linters ignored all files that are
using Cgo. It was introduced by PR golangci#1065, which assumed that all files
referenced by //line directives are non-Go files, which is not true. In the case
of Cgo they point to the original Go files which are used by Cgo as templates to
generate other Go files.

The fix replaces all calls of Pass.Fset.PositionFor with a function
getFileNames() that first calls Pass.Fset.PositionFor with adjustment, and only
if it results in a non-Go file it calls Pass.Fset.PositionFor again without
adjustment.

Fixes: golangci#2910

Signed-off-by: Sven Anderson <sven@anderson.de>
ansiwen added a commit to ansiwen/golangci-lint that referenced this pull request Jul 30, 2022
This change fixes a bug caused that many linters ignored all files that are
using Cgo. It was introduced by PR golangci#1065, which assumed that all files
referenced by //line directives are non-Go files, which is not true. In the case
of Cgo they point to the original Go files which are used by Cgo as templates to
generate other Go files.

The fix replaces all calls of Pass.Fset.PositionFor with a function
getFileNames() that first calls Pass.Fset.PositionFor with adjustment, and only
if it results in a non-Go file it calls Pass.Fset.PositionFor again without
adjustment.

Fixes: golangci#2910

Signed-off-by: Sven Anderson <sven@anderson.de>
@ldez ldez added this to the v1.27 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.

linting is failing on a file that should be ignored
3 participants