Skip to content

Commit 9fa9e2b

Browse files
committed
Fix #106: fix transitive expanding of nolint: we could nolint more lines than needed
1 parent 7495c4d commit 9fa9e2b

File tree

14 files changed

+104
-53
lines changed

14 files changed

+104
-53
lines changed

pkg/commands/linters.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"github.com/fatih/color"
99
"github.com/golangci/golangci-lint/pkg/lint/linter"
1010
"github.com/golangci/golangci-lint/pkg/lint/lintersdb"
11-
"github.com/golangci/golangci-lint/pkg/printers"
11+
"github.com/golangci/golangci-lint/pkg/logutils"
1212
"github.com/spf13/cobra"
1313
)
1414

@@ -23,7 +23,7 @@ func (e *Executor) initLinters() {
2323

2424
func printLinterConfigs(lcs []linter.Config) {
2525
for _, lc := range lcs {
26-
fmt.Fprintf(printers.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()),
26+
fmt.Fprintf(logutils.StdOut, "%s: %s [fast: %t]\n", color.YellowString(lc.Linter.Name()),
2727
lc.Linter.Desc(), !lc.DoesFullImport)
2828
}
2929
}
@@ -50,7 +50,7 @@ func (e Executor) executeLinters(cmd *cobra.Command, args []string) {
5050
for _, lc := range linters {
5151
linterNames = append(linterNames, lc.Linter.Name())
5252
}
53-
fmt.Fprintf(printers.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
53+
fmt.Fprintf(logutils.StdOut, "%s: %s\n", color.YellowString(p), strings.Join(linterNames, ", "))
5454
}
5555

5656
os.Exit(0)

pkg/commands/root.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import (
88

99
"github.com/golangci/golangci-lint/pkg/config"
1010
"github.com/golangci/golangci-lint/pkg/logutils"
11-
"github.com/golangci/golangci-lint/pkg/printers"
1211
"github.com/spf13/cobra"
1312
"github.com/spf13/pflag"
1413
)
1514

1615
func (e *Executor) persistentPreRun(cmd *cobra.Command, args []string) {
1716
if e.cfg.Run.PrintVersion {
18-
fmt.Fprintf(printers.StdOut, "golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
17+
fmt.Fprintf(logutils.StdOut, "golangci-lint has version %s built from %s on %s\n", e.version, e.commit, e.date)
1918
os.Exit(0)
2019
}
2120

pkg/commands/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ func (e *Executor) initRun() {
150150
}
151151
e.rootCmd.AddCommand(runCmd)
152152

153-
runCmd.SetOutput(printers.StdOut) // use custom output to properly color it in Windows terminals
153+
runCmd.SetOutput(logutils.StdOut) // use custom output to properly color it in Windows terminals
154154

155155
fs := runCmd.Flags()
156156
fs.SortFlags = false // sort them as they are defined here

pkg/config/reader.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/golangci/golangci-lint/pkg/fsutils"
1010
"github.com/golangci/golangci-lint/pkg/logutils"
11-
"github.com/golangci/golangci-lint/pkg/printers"
1211
"github.com/spf13/pflag"
1312
"github.com/spf13/viper"
1413
)
@@ -81,7 +80,7 @@ func (r *FileReader) parseConfig() error {
8180
}
8281

8382
if r.cfg.InternalTest { // just for testing purposes: to detect config file usage
84-
fmt.Fprintln(printers.StdOut, "test")
83+
fmt.Fprintln(logutils.StdOut, "test")
8584
os.Exit(0)
8685
}
8786

pkg/logutils/out.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package logutils
2+
3+
import (
4+
"github.com/fatih/color"
5+
colorable "github.com/mattn/go-colorable"
6+
)
7+
8+
var StdOut = color.Output // https://github.com/golangci/golangci-lint/issues/14
9+
var StdErr = colorable.NewColorableStderr()

pkg/logutils/stderr_log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func NewStderrLog(name string) *StderrLog {
2727

2828
// control log level in logutils, not in logrus
2929
sl.logger.SetLevel(logrus.DebugLevel)
30+
sl.logger.Out = StdErr
3031
sl.logger.Formatter = &logrus.TextFormatter{
3132
DisableTimestamp: true, // `INFO[0007] msg` -> `INFO msg`
3233
}

pkg/printers/checkstyle.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/xml"
66
"fmt"
77

8+
"github.com/golangci/golangci-lint/pkg/logutils"
89
"github.com/golangci/golangci-lint/pkg/result"
910
)
1011

@@ -73,6 +74,6 @@ func (Checkstyle) Print(ctx context.Context, issues <-chan result.Issue) (bool,
7374
return false, err
7475
}
7576

76-
fmt.Fprintf(StdOut, "%s%s\n", xml.Header, data)
77+
fmt.Fprintf(logutils.StdOut, "%s%s\n", xml.Header, data)
7778
return len(files) > 0, nil
7879
}

pkg/printers/json.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77

8+
"github.com/golangci/golangci-lint/pkg/logutils"
89
"github.com/golangci/golangci-lint/pkg/result"
910
)
1011

@@ -33,6 +34,6 @@ func (JSON) Print(ctx context.Context, issues <-chan result.Issue) (bool, error)
3334
return false, err
3435
}
3536

36-
fmt.Fprint(StdOut, string(outputJSON))
37+
fmt.Fprint(logutils.StdOut, string(outputJSON))
3738
return len(allIssues) != 0, nil
3839
}

pkg/printers/tab.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func (p Tab) SprintfColored(ca color.Attribute, format string, args ...interface
2929
}
3030

3131
func (p *Tab) Print(ctx context.Context, issues <-chan result.Issue) (bool, error) {
32-
w := tabwriter.NewWriter(StdOut, 0, 0, 2, ' ', 0)
32+
w := tabwriter.NewWriter(logutils.StdOut, 0, 0, 2, ' ', 0)
3333

3434
issuesN := 0
3535
for i := range issues {
@@ -41,7 +41,7 @@ func (p *Tab) Print(ctx context.Context, issues <-chan result.Issue) (bool, erro
4141
p.log.Infof("Found %d issues", issuesN)
4242
} else if ctx.Err() == nil { // don't print "congrats" if timeouted
4343
outStr := p.SprintfColored(color.FgGreen, "Congrats! No issues were found.")
44-
fmt.Fprintln(StdOut, outStr)
44+
fmt.Fprintln(logutils.StdOut, outStr)
4545
}
4646

4747
if err := w.Flush(); err != nil {

pkg/printers/text.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (p *Text) Print(ctx context.Context, issues <-chan result.Issue) (bool, err
9393
p.log.Infof("Found %d issues", issuesN)
9494
} else if ctx.Err() == nil { // don't print "congrats" if timeouted
9595
outStr := p.SprintfColored(color.FgGreen, "Congrats! No issues were found.")
96-
fmt.Fprintln(StdOut, outStr)
96+
fmt.Fprintln(logutils.StdOut, outStr)
9797
}
9898

9999
return issuesN != 0, nil
@@ -108,7 +108,7 @@ func (p Text) printIssue(i *result.Issue) {
108108
if i.Pos.Column != 0 {
109109
pos += fmt.Sprintf(":%d", i.Pos.Column)
110110
}
111-
fmt.Fprintf(StdOut, "%s: %s\n", pos, text)
111+
fmt.Fprintf(logutils.StdOut, "%s: %s\n", pos, text)
112112
}
113113

114114
func (p Text) printIssuedLines(i *result.Issue, lines linesCache) {
@@ -126,7 +126,7 @@ func (p Text) printIssuedLines(i *result.Issue, lines linesCache) {
126126
}
127127

128128
lineStr = string(bytes.Trim(lines[zeroIndexedLine], "\r"))
129-
fmt.Fprintln(StdOut, lineStr)
129+
fmt.Fprintln(logutils.StdOut, lineStr)
130130
}
131131
}
132132

@@ -149,5 +149,5 @@ func (p Text) printUnderLinePointer(i *result.Issue, line string) {
149149
prefix += strings.Repeat(" ", spacesCount)
150150
}
151151

152-
fmt.Fprintf(StdOut, "%s%s\n", prefix, p.SprintfColored(color.FgYellow, "^"))
152+
fmt.Fprintf(logutils.StdOut, "%s%s\n", prefix, p.SprintfColored(color.FgYellow, "^"))
153153
}

0 commit comments

Comments
 (0)