Skip to content

Commit cfacb9f

Browse files
committed
fix combination of --fix and --path-prefix
This combination used to fail because the fixer was executed after the path prefixer. Then the file paths that it processed didn't match the current working directory, leading to: ERRO Failed to fix issues in file test/fix_sample/main.go: failed to get file bytes for test/fix_sample/main.go: can't read file test/fix_sample/main.go: open test/fix_sample/main.go: no such file or directory Making the fixer a normal processor and moving it before the path prefixer avoids this problem.
1 parent 7aaec72 commit cfacb9f

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

pkg/commands/run.go

+1-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/golangci/golangci-lint/pkg/packages"
2424
"github.com/golangci/golangci-lint/pkg/printers"
2525
"github.com/golangci/golangci-lint/pkg/result"
26-
"github.com/golangci/golangci-lint/pkg/result/processors"
2726
)
2827

2928
const defaultFileMode = 0644
@@ -365,13 +364,7 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss
365364
return nil, err
366365
}
367366

368-
issues, err := runner.Run(ctx, lintersToRun, lintCtx)
369-
if err != nil {
370-
return nil, err
371-
}
372-
373-
fixer := processors.NewFixer(e.cfg, e.log, e.fileCache)
374-
return fixer.Process(issues), nil
367+
return runner.Run(ctx, lintersToRun, lintCtx)
375368
}
376369

377370
func (e *Executor) setOutputToDevNull() (savedStdout, savedStderr *os.File) {

pkg/fsutils/linecache.go

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ func NewLineCache(fc *FileCache) *LineCache {
1919
}
2020
}
2121

22+
func (lc *LineCache) GetFileCache() *FileCache { return lc.fileCache }
23+
2224
// GetLine returns the index1-th (1-based index) line from the file on filePath
2325
func (lc *LineCache) GetLine(filePath string, index1 int) (string, error) {
2426
if index1 == 0 { // some linters, e.g. gosec can do it: it really means first line

pkg/lint/runner.go

+3
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lint
9393
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
9494
processors.NewPathShortener(),
9595
getSeverityRulesProcessor(&cfg.Severity, log, lineCache),
96+
// The fixer still needs to see paths for the issues that are relative to the current directory.
97+
processors.NewFixer(cfg, log, lineCache.GetFileCache()),
98+
// Now we can modify the issues for output.
9699
processors.NewPathPrefixer(cfg.Output.PathPrefix),
97100
processors.NewSortResults(cfg),
98101
},

pkg/result/processors/fixer.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ func (f Fixer) printStat() {
3636
f.sw.PrintStages()
3737
}
3838

39-
func (f Fixer) Process(issues []result.Issue) []result.Issue {
39+
func (f Fixer) Process(issues []result.Issue) ([]result.Issue, error) {
4040
if !f.cfg.Issues.NeedFix {
41-
return issues
41+
return issues, nil
4242
}
4343

4444
outIssues := make([]result.Issue, 0, len(issues))
@@ -67,9 +67,17 @@ func (f Fixer) Process(issues []result.Issue) []result.Issue {
6767
}
6868

6969
f.printStat()
70-
return outIssues
70+
return outIssues, nil
7171
}
7272

73+
func (f Fixer) Name() string {
74+
return "fixer"
75+
}
76+
77+
func (f Fixer) Finish() {}
78+
79+
var _ Processor = Fixer{}
80+
7381
func (f Fixer) fixIssuesInFile(filePath string, issues []result.Issue) error {
7482
// TODO: don't read the whole file into memory: read line by line;
7583
// can't just use bufio.scanner: it has a line length limit

0 commit comments

Comments
 (0)