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: filter files #5272

Merged
merged 3 commits into from
Jan 1, 2025
Merged

fix: filter files #5272

merged 3 commits into from
Jan 1, 2025

Conversation

ldez
Copy link
Member

@ldez ldez commented Jan 1, 2025

Fixes #5271

@ldez ldez added bug Something isn't working linter: update Update the linter implementation inside golangci-lint area: cgo Related to CGO or line directives labels Jan 1, 2025
@ldez ldez added this to the next milestone Jan 1, 2025
Copy link
Member

@bombsimon bombsimon left a comment

Choose a reason for hiding this comment

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

I'm still not sure I fully understand how this is intended to work and when and why you'd want to analyze non go files. I know we talked about the generated files for cgo and all but this feels like a more fundamental thing. If we can't even run gofmt on these files, are we ever interested in the generated files for analysis? Even if we need them for the package to compile don't we always want to skip the non go files themselves for analysis?

To fix what you address here, what do you think about returning a bool from GetFilePosition instead so we know when we get the position if we got it from a .go file or a generated file? That would make it always available and less copying or forgetting to copy in any of the linters?

diff --git a/pkg/goanalysis/position.go b/pkg/goanalysis/position.go
index f90a7098..28afe6cb 100644
--- a/pkg/goanalysis/position.go
+++ b/pkg/goanalysis/position.go
@@ -8,20 +8,20 @@ import (
 	"golang.org/x/tools/go/analysis"
 )
 
-func GetFilePosition(pass *analysis.Pass, f *ast.File) token.Position {
+func GetFilePosition(pass *analysis.Pass, f *ast.File) (token.Position, bool) {
 	return GetFilePositionFor(pass.Fset, f.Pos())
 }
 
-func GetFilePositionFor(fset *token.FileSet, p token.Pos) token.Position {
+func GetFilePositionFor(fset *token.FileSet, p token.Pos) (token.Position, bool) {
 	pos := fset.PositionFor(p, true)
 
 	ext := filepath.Ext(pos.Filename)
 	if ext != ".go" {
 		// position has been adjusted to a non-go file, revert to original file
-		return fset.PositionFor(p, false)
+		return fset.PositionFor(p, false), false
 	}
 
-	return pos
+	return pos, true
 }
 
 func EndOfLinePos(f *token.File, line int) token.Pos {

Then we'd just do

position, hasGoSuffix := goanalysis.GetFilePosition(pass, file)
if !hasGoSuffix {
    continue
}

@ldez
Copy link
Member Author

ldez commented Jan 1, 2025

Your suggestion doesn't work because a non-adjusted file can have .go extension.

@ldez ldez requested a review from bombsimon January 1, 2025 17:03
@bombsimon
Copy link
Member

Do you mean that if pos.Filename doesn't have a .go file extension and we return fset.PositionFor(p, false) instead, maybe we still want to analyze the file if the adjusted token.Position have a .go suffix?

Wouldn't non-adjusted files with a .go extension return true in my example and also true for the lookup at call site?

Copy link
Member

@bombsimon bombsimon left a comment

Choose a reason for hiding this comment

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

Approving because I don't want to block you on this bug just to explain to me how it works, but it seems like if we have a .go extension in GetFilePositionFor we'd always return true which I think is correct? So I guess the issue is that we can't blindly return false from the other branch because the adjusted position could potentially also be a .go file?

@ldez
Copy link
Member Author

ldez commented Jan 1, 2025

I think an output example will help you to understand:

github.com/golangci/sandbox 0: adj: /home/ldez/sources/golangci/sandbox/main.go
github.com/golangci/sandbox 0: non-adj: /home/ldez/sources/golangci/sandbox/main.go
github.com/golangci/sandbox 0: adj: /home/ldez/sources/golangci/sandbox/main.go
github.com/golangci/sandbox 0: non-adj: /home/ldez/sources/golangci/sandbox/main.go
github.com/golangci/sandbox 1: adj: /home/ldez/sources/golangci/sandbox/main_test.go
github.com/golangci/sandbox 1: non-adj: /home/ldez/sources/golangci/sandbox/main_test.go
github.com/golangci/sandbox/cgo 0: adj: /home/ldez/.cache/go-build/62/6248342cd6ede467fbde8a115d730d771a2fcb65488a2bf6ac6dd981cfcf0e6b-d
github.com/golangci/sandbox/cgo 0: non-adj: /home/ldez/.cache/go-build/62/6248342cd6ede467fbde8a115d730d771a2fcb65488a2bf6ac6dd981cfcf0e6b-d
github.com/golangci/sandbox/cgo 1: adj: /home/ldez/sources/golangci/sandbox/cgo/cgo.go
github.com/golangci/sandbox/cgo 1: non-adj: /home/ldez/.cache/go-build/d2/d202ddb4ff2eb75186fb85b2d45b3abbede18e343180e149a0ae28f0849fa877-d
github.com/golangci/sandbox/cgo 2: adj: /home/ldez/.cache/go-build/52/52709e04346f162eeabb16dbe9df656d978effaafca09c371368789c95dd32d3-d
github.com/golangci/sandbox/cgo 2: non-adj: /home/ldez/.cache/go-build/52/52709e04346f162eeabb16dbe9df656d978effaafca09c371368789c95dd32d3-d
github.com/golangci/sandbox/line 0: adj: /home/ldez/sources/golangci/sandbox/line/hello.tmpl
github.com/golangci/sandbox/line 0: non-adj: /home/ldez/sources/golangci/sandbox/line/line.go
github.com/golangci/sandbox.test 0: adj: /home/ldez/.cache/go-build/42/42102fe476e4a606613a5b565e71218d109f6b06a5dfb121e376f66de471497b-d
github.com/golangci/sandbox.test 0: non-adj: /home/ldez/.cache/go-build/42/42102fe476e4a606613a5b565e71218d109f6b06a5dfb121e376f66de471497b-d

In this example, you have cgo file, go file, test file, and file with line directives.

.
├── cgo
│   └── cgo.go
├── go.mod
├── go.sum
├── line
│   ├── hello.tmpl
│   └── line.go
├── main.go
└── main_test.go

@ldez ldez merged commit afa0e27 into golangci:master Jan 1, 2025
15 checks passed
@ldez ldez deleted the fix/cgo-filter branch January 1, 2025 17:38
@ldez ldez modified the milestones: next, v1.63 Jan 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: auto-fix area: cgo Related to CGO or line directives bug Something isn't working linter: update Update the linter implementation inside golangci-lint
Projects
None yet
Development

Successfully merging this pull request may close these issues.

unexpected lint panic error in newest version v1.63.0
2 participants