@@ -9,7 +9,6 @@ package processors
99import (
1010 "errors"
1111 "fmt"
12- "go/format"
1312 "os"
1413 "slices"
1514
@@ -18,6 +17,7 @@ import (
1817 "github.com/golangci/golangci-lint/internal/x/tools/diff"
1918 "github.com/golangci/golangci-lint/pkg/config"
2019 "github.com/golangci/golangci-lint/pkg/fsutils"
20+ "github.com/golangci/golangci-lint/pkg/goformatters"
2121 "github.com/golangci/golangci-lint/pkg/goformatters/gci"
2222 "github.com/golangci/golangci-lint/pkg/goformatters/gofmt"
2323 "github.com/golangci/golangci-lint/pkg/goformatters/gofumpt"
@@ -36,14 +36,16 @@ type Fixer struct {
3636 log logutils.Log
3737 fileCache * fsutils.FileCache
3838 sw * timeutils.Stopwatch
39+ formatter * goformatters.MetaFormatter
3940}
4041
41- func NewFixer (cfg * config.Config , log logutils.Log , fileCache * fsutils.FileCache ) * Fixer {
42+ func NewFixer (cfg * config.Config , log logutils.Log , fileCache * fsutils.FileCache , formatter * goformatters. MetaFormatter ) * Fixer {
4243 return & Fixer {
4344 cfg : cfg ,
4445 log : log ,
4546 fileCache : fileCache ,
4647 sw : timeutils .NewStopwatch ("fixer" , log ),
48+ formatter : formatter ,
4749 }
4850}
4951
@@ -79,11 +81,13 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
7981
8082 var notFixableIssues []result.Issue
8183
84+ toBeFormattedFiles := make (map [string ]struct {})
85+
8286 for i := range issues {
8387 issue := issues [i ]
8488
8589 if slices .Contains (formatters , issue .FromLinter ) {
86- notFixableIssues = append ( notFixableIssues , issue )
90+ toBeFormattedFiles [ issue . FilePath ()] = struct {}{}
8791 continue
8892 }
8993
@@ -173,6 +177,8 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
173177
174178 var editError error
175179
180+ var formattedFiles []string
181+
176182 // Now we've got a set of valid edits for each file. Apply them.
177183 for path , edits := range editsByPath {
178184 contents , err := p .fileCache .GetFileBytes (path )
@@ -188,14 +194,32 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
188194 }
189195
190196 // Try to format the file.
191- if formatted , err := format .Source (out ); err == nil {
192- out = formatted
193- }
197+ out = p .formatter .Format (path , out )
194198
195199 if err := os .WriteFile (path , out , filePerm ); err != nil {
196200 editError = errors .Join (editError , fmt .Errorf ("%s: %w" , path , err ))
197201 continue
198202 }
203+
204+ formattedFiles = append (formattedFiles , path )
205+ }
206+
207+ for path := range toBeFormattedFiles {
208+ // Skips files already formatted by the previous fix step.
209+ if ! slices .Contains (formattedFiles , path ) {
210+ content , err := p .fileCache .GetFileBytes (path )
211+ if err != nil {
212+ p .log .Warnf ("Error reading file %s: %v" , path , err )
213+ continue
214+ }
215+
216+ out := p .formatter .Format (path , content )
217+
218+ if err := os .WriteFile (path , out , filePerm ); err != nil {
219+ editError = errors .Join (editError , fmt .Errorf ("%s: %w" , path , err ))
220+ continue
221+ }
222+ }
199223 }
200224
201225 return notFixableIssues , editError
0 commit comments