gomodguard: fix problem where duplicate issues were being reported (#2018)

This commit is contained in:
Ryan Currah 2021-05-27 16:21:23 -04:00 committed by GitHub
parent b916c9318b
commit 7776b546b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 47 additions and 49 deletions

2
go.mod
View File

@ -58,7 +58,7 @@ require (
github.com/nishanths/predeclared v0.2.1 github.com/nishanths/predeclared v0.2.1
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/polyfloyd/go-errorlint v0.0.0-20210510181950-ab96adb96fea github.com/polyfloyd/go-errorlint v0.0.0-20210510181950-ab96adb96fea
github.com/ryancurrah/gomodguard v1.2.0 github.com/ryancurrah/gomodguard v1.2.1
github.com/ryanrolds/sqlclosecheck v0.3.0 github.com/ryanrolds/sqlclosecheck v0.3.0
github.com/sanposhiho/wastedassign v1.0.0 github.com/sanposhiho/wastedassign v1.0.0
github.com/securego/gosec/v2 v2.7.0 github.com/securego/gosec/v2 v2.7.0

4
go.sum generated
View File

@ -539,8 +539,8 @@ github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g= github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryancurrah/gomodguard v1.2.0 h1:YWfhGOrXwLGiqcC/u5EqG6YeS8nh+1fw0HEc85CVZro= github.com/ryancurrah/gomodguard v1.2.1 h1:t1WWL0RGJJBo5KZ0u2c/FGY1QQgx2gUbHWzBmOKWs98=
github.com/ryancurrah/gomodguard v1.2.0/go.mod h1:rNqbC4TOIdUDcVMSIpNNAzTbzXAZa6W5lnUepvuMMgQ= github.com/ryancurrah/gomodguard v1.2.1/go.mod h1:tpI+C/nzvfUR3bF28b5QHpTn/jM/zlGniI++6ZlIWeE=
github.com/ryanrolds/sqlclosecheck v0.3.0 h1:AZx+Bixh8zdUBxUA1NxbxVAS78vTPq4rCb8OUZI9xFw= github.com/ryanrolds/sqlclosecheck v0.3.0 h1:AZx+Bixh8zdUBxUA1NxbxVAS78vTPq4rCb8OUZI9xFw=
github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0KCtEdgEkHwDbigdA= github.com/ryanrolds/sqlclosecheck v0.3.0/go.mod h1:1gREqxyTGR3lVtpngyFo3hZAgk0KCtEdgEkHwDbigdA=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=

View File

@ -13,6 +13,9 @@ import (
const ( const (
gomodguardName = "gomodguard" gomodguardName = "gomodguard"
gomodguardDesc = "Allow and block list linter for direct Go module dependencies. " +
"This is different from depguard where there are different block " +
"types for example version constraints and module recommendations."
) )
// NewGomodguard returns a new Gomodguard linter. // NewGomodguard returns a new Gomodguard linter.
@ -28,20 +31,17 @@ func NewGomodguard() *goanalysis.Linter {
return goanalysis.NewLinter( return goanalysis.NewLinter(
gomodguardName, gomodguardName,
"Allow and block list linter for direct Go module dependencies. "+ gomodguardDesc,
"This is different from depguard where there are different block "+
"types for example version constraints and module recommendations.",
[]*analysis.Analyzer{analyzer}, []*analysis.Analyzer{analyzer},
nil, nil,
).WithContextSetter(func(lintCtx *linter.Context) { ).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { linterCfg := lintCtx.Cfg.LintersSettings.Gomodguard
var (
files = []string{} processorCfg := &gomodguard.Configuration{}
linterCfg = lintCtx.Cfg.LintersSettings.Gomodguard
processorCfg = &gomodguard.Configuration{}
)
processorCfg.Allowed.Modules = linterCfg.Allowed.Modules processorCfg.Allowed.Modules = linterCfg.Allowed.Modules
processorCfg.Allowed.Domains = linterCfg.Allowed.Domains processorCfg.Allowed.Domains = linterCfg.Allowed.Domains
processorCfg.Blocked.LocalReplaceDirectives = linterCfg.Blocked.LocalReplaceDirectives
for n := range linterCfg.Blocked.Modules { for n := range linterCfg.Blocked.Modules {
for k, v := range linterCfg.Blocked.Modules[n] { for k, v := range linterCfg.Blocked.Modules[n] {
m := map[string]gomodguard.BlockedModule{k: { m := map[string]gomodguard.BlockedModule{k: {
@ -64,32 +64,30 @@ func NewGomodguard() *goanalysis.Linter {
} }
} }
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}
processorCfg.Blocked.LocalReplaceDirectives = linterCfg.Blocked.LocalReplaceDirectives
processor, err := gomodguard.NewProcessor(processorCfg) processor, err := gomodguard.NewProcessor(processorCfg)
if err != nil { if err != nil {
lintCtx.Log.Warnf("running gomodguard failed: %s: if you are not using go modules "+ lintCtx.Log.Warnf("running gomodguard failed: %s: if you are not using go modules "+
"it is suggested to disable this linter", err) "it is suggested to disable this linter", err)
return nil, nil return
} }
gomodguardErrors := processor.ProcessFiles(files) analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
if len(gomodguardErrors) == 0 { var files []string
return nil, nil
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
} }
gomodguardIssues := processor.ProcessFiles(files)
mu.Lock() mu.Lock()
defer mu.Unlock() defer mu.Unlock()
for _, err := range gomodguardErrors { for _, gomodguardIssue := range gomodguardIssues {
issues = append(issues, goanalysis.NewIssue(&result.Issue{ issues = append(issues, goanalysis.NewIssue(&result.Issue{
FromLinter: gomodguardName, FromLinter: gomodguardName,
Pos: err.Position, Pos: gomodguardIssue.Position,
Text: err.Reason, Text: gomodguardIssue.Reason,
}, pass)) }, pass))
} }