Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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