Skip to content

Commit

Permalink
fix: comma in exclude pattern leads to unexpected results
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Apr 19, 2021
1 parent 5d10450 commit 8a38c4d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
19 changes: 18 additions & 1 deletion pkg/commands/run.go
Expand Up @@ -213,6 +213,7 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is

// Issues config
ic := &cfg.Issues
// FIXME here
fs.StringSliceVarP(&ic.ExcludePatterns, "exclude", "e", nil, wh("Exclude issue by regexp"))
fs.BoolVar(&ic.UseDefaultExcludes, "exclude-use-default", true, getDefaultIssueExcludeHelp())
fs.BoolVar(&ic.ExcludeCaseSensitive, "exclude-case-sensitive", false, wh("If set to true exclude "+
Expand Down Expand Up @@ -313,8 +314,24 @@ func fixSlicesFlags(fs *pflag.FlagSet) {
return
}

// custom join to handle string with comma.
var g string
for i, v := range s {
if strings.Contains(v, ",") {
// add quotes to escape comma because spf13/pflag use a CSV parser:
// https://github.com/spf13/pflag/blob/85dd5c8bc61cfa382fecd072378089d4e856579d/string_slice.go#L43
g += `"` + v + `"`
} else {
g += v
}

if i < len(s)-1 {
g += ","
}
}

// calling Set sets Changed to true: next Set calls will append, not overwrite
_ = f.Value.Set(strings.Join(s, ","))
_ = f.Value.Set(g)
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/config/reader.go
Expand Up @@ -7,7 +7,7 @@ import (
"path/filepath"
"strings"

homedir "github.com/mitchellh/go-homedir"
"github.com/mitchellh/go-homedir"
"github.com/spf13/viper"

"github.com/golangci/golangci-lint/pkg/fsutils"
Expand Down
6 changes: 3 additions & 3 deletions pkg/lint/runner.go
Expand Up @@ -237,9 +237,9 @@ func (r *Runner) processIssues(issues []result.Issue, sw *timeutils.Stopwatch, s

func getExcludeProcessor(cfg *config.Issues) processors.Processor {
var excludeTotalPattern string
excludeGlobalPatterns := cfg.ExcludePatterns
if len(excludeGlobalPatterns) != 0 {
excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(excludeGlobalPatterns, "|"))

if len(cfg.ExcludePatterns) != 0 {
excludeTotalPattern = fmt.Sprintf("(%s)", strings.Join(cfg.ExcludePatterns, "|"))
}

var excludeProcessor processors.Processor
Expand Down

0 comments on commit 8a38c4d

Please sign in to comment.