From afa0e274a027687fc0cd19ed3e883c936dbbfdb0 Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Wed, 1 Jan 2025 18:38:02 +0100 Subject: [PATCH] fix: filter files (#5272) --- pkg/goanalysis/position.go | 10 ++++++++ pkg/golinters/godox/godox.go | 5 +++- pkg/golinters/gofmt/gofmt.go | 5 +++- pkg/golinters/gofumpt/gofumpt.go | 5 +++- .../gofumpt/testdata/fix/in/gofumpt_cgo.go | 20 +++++++++++++++ .../gofumpt/testdata/fix/out/gofumpt_cgo.go | 20 +++++++++++++++ pkg/golinters/goheader/goheader.go | 5 ++-- pkg/golinters/goimports/goimports.go | 5 +++- .../testdata/fix/in/goimports_cgo.go | 25 +++++++++++++++++++ .../testdata/fix/out/goimports_cgo.go | 20 +++++++++++++++ pkg/golinters/lll/lll.go | 6 ++++- pkg/golinters/misspell/misspell.go | 9 ++++--- pkg/golinters/nestif/nestif.go | 5 +++- 13 files changed, 128 insertions(+), 12 deletions(-) create mode 100644 pkg/golinters/gofumpt/testdata/fix/in/gofumpt_cgo.go create mode 100644 pkg/golinters/gofumpt/testdata/fix/out/gofumpt_cgo.go create mode 100644 pkg/golinters/goimports/testdata/fix/in/goimports_cgo.go create mode 100644 pkg/golinters/goimports/testdata/fix/out/goimports_cgo.go diff --git a/pkg/goanalysis/position.go b/pkg/goanalysis/position.go index f90a70983573..a386d3bc1434 100644 --- a/pkg/goanalysis/position.go +++ b/pkg/goanalysis/position.go @@ -12,6 +12,16 @@ func GetFilePosition(pass *analysis.Pass, f *ast.File) token.Position { return GetFilePositionFor(pass.Fset, f.Pos()) } +func GetGoFilePosition(pass *analysis.Pass, f *ast.File) (token.Position, bool) { + position := GetFilePositionFor(pass.Fset, f.Pos()) + + if filepath.Ext(position.Filename) == ".go" { + return position, true + } + + return position, false +} + func GetFilePositionFor(fset *token.FileSet, p token.Pos) token.Position { pos := fset.PositionFor(p, true) diff --git a/pkg/golinters/godox/godox.go b/pkg/golinters/godox/godox.go index 4464e1d8ec5a..181e0a73abea 100644 --- a/pkg/golinters/godox/godox.go +++ b/pkg/golinters/godox/godox.go @@ -34,7 +34,10 @@ func New(settings *config.GodoxSettings) *goanalysis.Linter { func runGodox(pass *analysis.Pass, settings *config.GodoxSettings) { for _, file := range pass.Files { - position := goanalysis.GetFilePosition(pass, file) + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { + continue + } messages := godox.Run(file, pass.Fset, settings.Keywords...) if len(messages) == 0 { diff --git a/pkg/golinters/gofmt/gofmt.go b/pkg/golinters/gofmt/gofmt.go index a7ccfa899096..b6531d5314af 100644 --- a/pkg/golinters/gofmt/gofmt.go +++ b/pkg/golinters/gofmt/gofmt.go @@ -45,7 +45,10 @@ func runGofmt(lintCtx *linter.Context, pass *analysis.Pass, settings *config.GoF } for _, file := range pass.Files { - position := goanalysis.GetFilePosition(pass, file) + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { + continue + } diff, err := gofmtAPI.RunRewrite(position.Filename, settings.Simplify, rewriteRules) if err != nil { // TODO: skip diff --git a/pkg/golinters/gofumpt/gofumpt.go b/pkg/golinters/gofumpt/gofumpt.go index b27f20a250ed..7a11a9074d03 100644 --- a/pkg/golinters/gofumpt/gofumpt.go +++ b/pkg/golinters/gofumpt/gofumpt.go @@ -61,7 +61,10 @@ func New(settings *config.GofumptSettings) *goanalysis.Linter { func runGofumpt(lintCtx *linter.Context, pass *analysis.Pass, diff differ, options format.Options) error { for _, file := range pass.Files { - position := goanalysis.GetFilePosition(pass, file) + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { + continue + } input, err := os.ReadFile(position.Filename) if err != nil { diff --git a/pkg/golinters/gofumpt/testdata/fix/in/gofumpt_cgo.go b/pkg/golinters/gofumpt/testdata/fix/in/gofumpt_cgo.go new file mode 100644 index 000000000000..a5f3cedb7108 --- /dev/null +++ b/pkg/golinters/gofumpt/testdata/fix/in/gofumpt_cgo.go @@ -0,0 +1,20 @@ +//golangcitest:args -Egofumpt +//golangcitest:config_path testdata/gofumpt-fix.yml +//golangcitest:expected_exitcode 0 +package p + +/* + #include + #include + + void myprint(char* s) { + printf("%d\n", s); + } +*/ +import "C" + +import "fmt" + +func GofmtNotExtra(bar string, baz string) { + fmt.Print(bar, baz) +} diff --git a/pkg/golinters/gofumpt/testdata/fix/out/gofumpt_cgo.go b/pkg/golinters/gofumpt/testdata/fix/out/gofumpt_cgo.go new file mode 100644 index 000000000000..3aab3ea62be1 --- /dev/null +++ b/pkg/golinters/gofumpt/testdata/fix/out/gofumpt_cgo.go @@ -0,0 +1,20 @@ +//golangcitest:args -Egofumpt +//golangcitest:config_path testdata/gofumpt-fix.yml +//golangcitest:expected_exitcode 0 +package p + +/* + #include + #include + + void myprint(char* s) { + printf("%d\n", s); + } +*/ +import "C" + +import "fmt" + +func GofmtNotExtra(bar, baz string) { + fmt.Print(bar, baz) +} diff --git a/pkg/golinters/goheader/goheader.go b/pkg/golinters/goheader/goheader.go index 63a45d46fae2..843afff800b9 100644 --- a/pkg/golinters/goheader/goheader.go +++ b/pkg/golinters/goheader/goheader.go @@ -63,9 +63,8 @@ func runGoHeader(pass *analysis.Pass, conf *goheader.Configuration) error { a := goheader.New(goheader.WithTemplate(template), goheader.WithValues(values)) for _, file := range pass.Files { - position := goanalysis.GetFilePosition(pass, file) - - if !strings.HasSuffix(position.Filename, ".go") { + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { continue } diff --git a/pkg/golinters/goimports/goimports.go b/pkg/golinters/goimports/goimports.go index 2f08dcde060a..6ddc9a75b1f2 100644 --- a/pkg/golinters/goimports/goimports.go +++ b/pkg/golinters/goimports/goimports.go @@ -43,7 +43,10 @@ func New(settings *config.GoImportsSettings) *goanalysis.Linter { func runGoImports(lintCtx *linter.Context, pass *analysis.Pass) error { for _, file := range pass.Files { - position := goanalysis.GetFilePosition(pass, file) + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { + continue + } diff, err := goimportsAPI.Run(position.Filename) if err != nil { // TODO: skip diff --git a/pkg/golinters/goimports/testdata/fix/in/goimports_cgo.go b/pkg/golinters/goimports/testdata/fix/in/goimports_cgo.go new file mode 100644 index 000000000000..de8fa9a81856 --- /dev/null +++ b/pkg/golinters/goimports/testdata/fix/in/goimports_cgo.go @@ -0,0 +1,25 @@ +//golangcitest:args -Egoimports +//golangcitest:expected_exitcode 0 +package p + +/* + #include + #include + + void myprint(char* s) { + printf("%d\n", s); + } +*/ +import "C" + +import ( + "os" + "fmt" +) + + func goimports(a, b int) int { + if a != b { + return 1 + } + return 2 +} diff --git a/pkg/golinters/goimports/testdata/fix/out/goimports_cgo.go b/pkg/golinters/goimports/testdata/fix/out/goimports_cgo.go new file mode 100644 index 000000000000..693800c1b37e --- /dev/null +++ b/pkg/golinters/goimports/testdata/fix/out/goimports_cgo.go @@ -0,0 +1,20 @@ +//golangcitest:args -Egoimports +//golangcitest:expected_exitcode 0 +package p + +/* + #include + #include + + void myprint(char* s) { + printf("%d\n", s); + } +*/ +import "C" + +func goimports(a, b int) int { + if a != b { + return 1 + } + return 2 +} diff --git a/pkg/golinters/lll/lll.go b/pkg/golinters/lll/lll.go index b49a805b3fd6..bad3b0c4e25d 100644 --- a/pkg/golinters/lll/lll.go +++ b/pkg/golinters/lll/lll.go @@ -55,7 +55,11 @@ func runLll(pass *analysis.Pass, settings *config.LllSettings) error { } func getLLLIssuesForFile(pass *analysis.Pass, file *ast.File, maxLineLen int, tabSpaces string) error { - position := goanalysis.GetFilePosition(pass, file) + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { + return nil + } + nonAdjPosition := pass.Fset.PositionFor(file.Pos(), false) f, err := os.Open(position.Filename) diff --git a/pkg/golinters/misspell/misspell.go b/pkg/golinters/misspell/misspell.go index 0f13e61038e0..3ace5fddb9f8 100644 --- a/pkg/golinters/misspell/misspell.go +++ b/pkg/golinters/misspell/misspell.go @@ -91,11 +91,14 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac } func runMisspellOnFile(lintCtx *linter.Context, pass *analysis.Pass, file *ast.File, replacer *misspell.Replacer, mode string) error { - filename := goanalysis.GetFilePosition(pass, file).Filename + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { + return nil + } - fileContent, err := lintCtx.FileCache.GetFileBytes(filename) + fileContent, err := lintCtx.FileCache.GetFileBytes(position.Filename) if err != nil { - return fmt.Errorf("can't get file %s contents: %w", filename, err) + return fmt.Errorf("can't get file %s contents: %w", position.Filename, err) } // `r.ReplaceGo` doesn't find issues inside strings: it searches only inside comments. diff --git a/pkg/golinters/nestif/nestif.go b/pkg/golinters/nestif/nestif.go index 924f14e5fac3..b72538fd1674 100644 --- a/pkg/golinters/nestif/nestif.go +++ b/pkg/golinters/nestif/nestif.go @@ -35,7 +35,10 @@ func runNestIf(pass *analysis.Pass, settings *config.NestifSettings) { } for _, file := range pass.Files { - position := goanalysis.GetFilePosition(pass, file) + position, isGoFile := goanalysis.GetGoFilePosition(pass, file) + if !isGoFile { + continue + } issues := checker.Check(file, pass.Fset) if len(issues) == 0 {