Skip to content

Commit

Permalink
gomodguard: fix problem where duplicate issues were being reported (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ryancurrah committed May 27, 2021
1 parent b916c93 commit 7776b54
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 48 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ require (
github.com/nishanths/predeclared v0.2.1
github.com/pkg/errors v0.9.1
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/sanposhiho/wastedassign v1.0.0
github.com/securego/gosec/v2 v2.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

88 changes: 43 additions & 45 deletions pkg/golinters/gomodguard.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (

const (
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.
Expand All @@ -28,68 +31,63 @@ func NewGomodguard() *goanalysis.Linter {

return goanalysis.NewLinter(
gomodguardName,
"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.",
gomodguardDesc,
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var (
files = []string{}
linterCfg = lintCtx.Cfg.LintersSettings.Gomodguard
processorCfg = &gomodguard.Configuration{}
)
processorCfg.Allowed.Modules = linterCfg.Allowed.Modules
processorCfg.Allowed.Domains = linterCfg.Allowed.Domains
for n := range linterCfg.Blocked.Modules {
for k, v := range linterCfg.Blocked.Modules[n] {
m := map[string]gomodguard.BlockedModule{k: {
Recommendations: v.Recommendations,
Reason: v.Reason,
}}
processorCfg.Blocked.Modules = append(processorCfg.Blocked.Modules, m)
break
}
}
linterCfg := lintCtx.Cfg.LintersSettings.Gomodguard

for n := range linterCfg.Blocked.Versions {
for k, v := range linterCfg.Blocked.Versions[n] {
m := map[string]gomodguard.BlockedVersion{k: {
Version: v.Version,
Reason: v.Reason,
}}
processorCfg.Blocked.Versions = append(processorCfg.Blocked.Versions, m)
break
}
processorCfg := &gomodguard.Configuration{}
processorCfg.Allowed.Modules = linterCfg.Allowed.Modules
processorCfg.Allowed.Domains = linterCfg.Allowed.Domains
processorCfg.Blocked.LocalReplaceDirectives = linterCfg.Blocked.LocalReplaceDirectives

for n := range linterCfg.Blocked.Modules {
for k, v := range linterCfg.Blocked.Modules[n] {
m := map[string]gomodguard.BlockedModule{k: {
Recommendations: v.Recommendations,
Reason: v.Reason,
}}
processorCfg.Blocked.Modules = append(processorCfg.Blocked.Modules, m)
break
}
}

for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
for n := range linterCfg.Blocked.Versions {
for k, v := range linterCfg.Blocked.Versions[n] {
m := map[string]gomodguard.BlockedVersion{k: {
Version: v.Version,
Reason: v.Reason,
}}
processorCfg.Blocked.Versions = append(processorCfg.Blocked.Versions, m)
break
}
}

processorCfg.Blocked.LocalReplaceDirectives = linterCfg.Blocked.LocalReplaceDirectives
processor, err := gomodguard.NewProcessor(processorCfg)
if err != nil {
lintCtx.Log.Warnf("running gomodguard failed: %s: if you are not using go modules "+
"it is suggested to disable this linter", err)
return
}

processor, err := gomodguard.NewProcessor(processorCfg)
if err != nil {
lintCtx.Log.Warnf("running gomodguard failed: %s: if you are not using go modules "+
"it is suggested to disable this linter", err)
return nil, nil
}
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var files []string

gomodguardErrors := processor.ProcessFiles(files)
if len(gomodguardErrors) == 0 {
return nil, nil
for _, file := range pass.Files {
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
}

gomodguardIssues := processor.ProcessFiles(files)

mu.Lock()
defer mu.Unlock()

for _, err := range gomodguardErrors {
for _, gomodguardIssue := range gomodguardIssues {
issues = append(issues, goanalysis.NewIssue(&result.Issue{
FromLinter: gomodguardName,
Pos: err.Position,
Text: err.Reason,
Pos: gomodguardIssue.Position,
Text: gomodguardIssue.Reason,
}, pass))
}

Expand Down

0 comments on commit 7776b54

Please sign in to comment.