@@ -9,7 +9,6 @@ package processors
9
9
import (
10
10
"errors"
11
11
"fmt"
12
- "go/format"
13
12
"os"
14
13
"slices"
15
14
@@ -18,6 +17,7 @@ import (
18
17
"github.com/golangci/golangci-lint/internal/x/tools/diff"
19
18
"github.com/golangci/golangci-lint/pkg/config"
20
19
"github.com/golangci/golangci-lint/pkg/fsutils"
20
+ "github.com/golangci/golangci-lint/pkg/goformatters"
21
21
"github.com/golangci/golangci-lint/pkg/goformatters/gci"
22
22
"github.com/golangci/golangci-lint/pkg/goformatters/gofmt"
23
23
"github.com/golangci/golangci-lint/pkg/goformatters/gofumpt"
@@ -36,14 +36,16 @@ type Fixer struct {
36
36
log logutils.Log
37
37
fileCache * fsutils.FileCache
38
38
sw * timeutils.Stopwatch
39
+ formatter * goformatters.MetaFormatter
39
40
}
40
41
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 {
42
43
return & Fixer {
43
44
cfg : cfg ,
44
45
log : log ,
45
46
fileCache : fileCache ,
46
47
sw : timeutils .NewStopwatch ("fixer" , log ),
48
+ formatter : formatter ,
47
49
}
48
50
}
49
51
@@ -79,11 +81,13 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
79
81
80
82
var notFixableIssues []result.Issue
81
83
84
+ toBeFormattedFiles := make (map [string ]struct {})
85
+
82
86
for i := range issues {
83
87
issue := issues [i ]
84
88
85
89
if slices .Contains (formatters , issue .FromLinter ) {
86
- notFixableIssues = append ( notFixableIssues , issue )
90
+ toBeFormattedFiles [ issue . FilePath ()] = struct {}{}
87
91
continue
88
92
}
89
93
@@ -173,6 +177,8 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
173
177
174
178
var editError error
175
179
180
+ var formattedFiles []string
181
+
176
182
// Now we've got a set of valid edits for each file. Apply them.
177
183
for path , edits := range editsByPath {
178
184
contents , err := p .fileCache .GetFileBytes (path )
@@ -188,14 +194,32 @@ func (p Fixer) process(issues []result.Issue) ([]result.Issue, error) {
188
194
}
189
195
190
196
// 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 )
194
198
195
199
if err := os .WriteFile (path , out , filePerm ); err != nil {
196
200
editError = errors .Join (editError , fmt .Errorf ("%s: %w" , path , err ))
197
201
continue
198
202
}
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
+ }
199
223
}
200
224
201
225
return notFixableIssues , editError
0 commit comments