Skip to content

Commit d92b38c

Browse files
pohlyldez
andauthored
fix: combination of --fix and --path-prefix (#3700)
Co-authored-by: Fernandez Ludovic <[email protected]>
1 parent 076f6b9 commit d92b38c

File tree

4 files changed

+69
-19
lines changed

4 files changed

+69
-19
lines changed

pkg/commands/run.go

+2-9
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
@@ -360,18 +359,12 @@ func (e *Executor) runAnalysis(ctx context.Context, args []string) ([]result.Iss
360359
lintCtx.Log = e.log.Child(logutils.DebugKeyLintersContext)
361360

362361
runner, err := lint.NewRunner(e.cfg, e.log.Child(logutils.DebugKeyRunner),
363-
e.goenv, e.EnabledLintersSet, e.lineCache, e.DBManager, lintCtx.Packages)
362+
e.goenv, e.EnabledLintersSet, e.lineCache, e.fileCache, e.DBManager, lintCtx.Packages)
364363
if err != nil {
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/lint/runner.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ type Runner struct {
2727
Log logutils.Log
2828
}
2929

30-
func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lintersdb.EnabledSet,
31-
lineCache *fsutils.LineCache, dbManager *lintersdb.Manager, pkgs []*gopackages.Package) (*Runner, error) {
30+
func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
31+
es *lintersdb.EnabledSet,
32+
lineCache *fsutils.LineCache, fileCache *fsutils.FileCache,
33+
dbManager *lintersdb.Manager, pkgs []*gopackages.Package) (*Runner, error) {
3234
// Beware that some processors need to add the path prefix when working with paths
3335
// because they get invoked before the path prefixer (exclude and severity rules)
3436
// or process other paths (skip files).
@@ -98,6 +100,11 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env, es *lint
98100
processors.NewSourceCode(lineCache, log.Child(logutils.DebugKeySourceCode)),
99101
processors.NewPathShortener(),
100102
getSeverityRulesProcessor(&cfg.Severity, log, files),
103+
104+
// The fixer still needs to see paths for the issues that are relative to the current directory.
105+
processors.NewFixer(cfg, log, fileCache),
106+
107+
// Now we can modify the issues for output.
101108
processors.NewPathPrefixer(cfg.Output.PathPrefix),
102109
processors.NewSortResults(cfg),
103110
},

pkg/result/processors/fixer.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import (
1616
"github.com/golangci/golangci-lint/pkg/timeutils"
1717
)
1818

19+
var _ Processor = Fixer{}
20+
1921
type Fixer struct {
2022
cfg *config.Config
2123
log logutils.Log
@@ -36,9 +38,9 @@ func (f Fixer) printStat() {
3638
f.sw.PrintStages()
3739
}
3840

39-
func (f Fixer) Process(issues []result.Issue) []result.Issue {
41+
func (f Fixer) Process(issues []result.Issue) ([]result.Issue, error) {
4042
if !f.cfg.Issues.NeedFix {
41-
return issues
43+
return issues, nil
4244
}
4345

4446
outIssues := make([]result.Issue, 0, len(issues))
@@ -67,9 +69,15 @@ func (f Fixer) Process(issues []result.Issue) []result.Issue {
6769
}
6870

6971
f.printStat()
70-
return outIssues
72+
return outIssues, nil
73+
}
74+
75+
func (f Fixer) Name() string {
76+
return "fixer"
7177
}
7278

79+
func (f Fixer) Finish() {}
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

test/fix_test.go

+47-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ import (
1414
// value: "1"
1515
const envKeepTempFiles = "GL_KEEP_TEMP_FILES"
1616

17-
func TestFix(t *testing.T) {
17+
func setupTestFix(t *testing.T) []string {
18+
t.Helper()
19+
1820
testshared.SkipOnWindows(t)
1921

2022
tmpDir := filepath.Join(testdataDir, "fix.tmp")
@@ -33,7 +35,48 @@ func TestFix(t *testing.T) {
3335

3436
testshared.InstallGolangciLint(t)
3537

36-
sources := findSources(t, tmpDir, "in", "*.go")
38+
return findSources(t, tmpDir, "in", "*.go")
39+
}
40+
41+
func TestFix(t *testing.T) {
42+
sources := setupTestFix(t)
43+
44+
for _, input := range sources {
45+
input := input
46+
t.Run(filepath.Base(input), func(t *testing.T) {
47+
t.Parallel()
48+
49+
rc := testshared.ParseTestDirectives(t, input)
50+
if rc == nil {
51+
t.Logf("Skipped: %s", input)
52+
return
53+
}
54+
55+
testshared.NewRunnerBuilder(t).
56+
WithArgs("--disable-all",
57+
"--print-issued-lines=false",
58+
"--print-linter-name=false",
59+
"--out-format=line-number",
60+
"--fix").
61+
WithRunContext(rc).
62+
WithTargetPath(input).
63+
Runner().
64+
Run().
65+
ExpectExitCode(rc.ExitCode)
66+
67+
output, err := os.ReadFile(input)
68+
require.NoError(t, err)
69+
70+
expectedOutput, err := os.ReadFile(filepath.Join(testdataDir, "fix", "out", filepath.Base(input)))
71+
require.NoError(t, err)
72+
73+
require.Equal(t, string(expectedOutput), string(output))
74+
})
75+
}
76+
}
77+
78+
func TestFix_pathPrefix(t *testing.T) {
79+
sources := setupTestFix(t)
3780

3881
for _, input := range sources {
3982
input := input
@@ -47,13 +90,12 @@ func TestFix(t *testing.T) {
4790
}
4891

4992
testshared.NewRunnerBuilder(t).
50-
WithArgs(
51-
"--disable-all",
93+
WithArgs("--disable-all",
5294
"--print-issued-lines=false",
5395
"--print-linter-name=false",
5496
"--out-format=line-number",
5597
"--fix",
56-
).
98+
"--path-prefix=foobar/").
5799
WithRunContext(rc).
58100
WithTargetPath(input).
59101
Runner().

0 commit comments

Comments
 (0)