Skip to content

Commit 7776b54

Browse files
authored
gomodguard: fix problem where duplicate issues were being reported (#2018)
1 parent b916c93 commit 7776b54

File tree

3 files changed

+46
-48
lines changed

3 files changed

+46
-48
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ require (
5858
github.com/nishanths/predeclared v0.2.1
5959
github.com/pkg/errors v0.9.1
6060
github.com/polyfloyd/go-errorlint v0.0.0-20210510181950-ab96adb96fea
61-
github.com/ryancurrah/gomodguard v1.2.0
61+
github.com/ryancurrah/gomodguard v1.2.1
6262
github.com/ryanrolds/sqlclosecheck v0.3.0
6363
github.com/sanposhiho/wastedassign v1.0.0
6464
github.com/securego/gosec/v2 v2.7.0

go.sum

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/golinters/gomodguard.go

+43-45
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import (
1313

1414
const (
1515
gomodguardName = "gomodguard"
16+
gomodguardDesc = "Allow and block list linter for direct Go module dependencies. " +
17+
"This is different from depguard where there are different block " +
18+
"types for example version constraints and module recommendations."
1619
)
1720

1821
// NewGomodguard returns a new Gomodguard linter.
@@ -28,68 +31,63 @@ func NewGomodguard() *goanalysis.Linter {
2831

2932
return goanalysis.NewLinter(
3033
gomodguardName,
31-
"Allow and block list linter for direct Go module dependencies. "+
32-
"This is different from depguard where there are different block "+
33-
"types for example version constraints and module recommendations.",
34+
gomodguardDesc,
3435
[]*analysis.Analyzer{analyzer},
3536
nil,
3637
).WithContextSetter(func(lintCtx *linter.Context) {
37-
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
38-
var (
39-
files = []string{}
40-
linterCfg = lintCtx.Cfg.LintersSettings.Gomodguard
41-
processorCfg = &gomodguard.Configuration{}
42-
)
43-
processorCfg.Allowed.Modules = linterCfg.Allowed.Modules
44-
processorCfg.Allowed.Domains = linterCfg.Allowed.Domains
45-
for n := range linterCfg.Blocked.Modules {
46-
for k, v := range linterCfg.Blocked.Modules[n] {
47-
m := map[string]gomodguard.BlockedModule{k: {
48-
Recommendations: v.Recommendations,
49-
Reason: v.Reason,
50-
}}
51-
processorCfg.Blocked.Modules = append(processorCfg.Blocked.Modules, m)
52-
break
53-
}
54-
}
38+
linterCfg := lintCtx.Cfg.LintersSettings.Gomodguard
5539

56-
for n := range linterCfg.Blocked.Versions {
57-
for k, v := range linterCfg.Blocked.Versions[n] {
58-
m := map[string]gomodguard.BlockedVersion{k: {
59-
Version: v.Version,
60-
Reason: v.Reason,
61-
}}
62-
processorCfg.Blocked.Versions = append(processorCfg.Blocked.Versions, m)
63-
break
64-
}
40+
processorCfg := &gomodguard.Configuration{}
41+
processorCfg.Allowed.Modules = linterCfg.Allowed.Modules
42+
processorCfg.Allowed.Domains = linterCfg.Allowed.Domains
43+
processorCfg.Blocked.LocalReplaceDirectives = linterCfg.Blocked.LocalReplaceDirectives
44+
45+
for n := range linterCfg.Blocked.Modules {
46+
for k, v := range linterCfg.Blocked.Modules[n] {
47+
m := map[string]gomodguard.BlockedModule{k: {
48+
Recommendations: v.Recommendations,
49+
Reason: v.Reason,
50+
}}
51+
processorCfg.Blocked.Modules = append(processorCfg.Blocked.Modules, m)
52+
break
6553
}
54+
}
6655

67-
for _, file := range pass.Files {
68-
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
56+
for n := range linterCfg.Blocked.Versions {
57+
for k, v := range linterCfg.Blocked.Versions[n] {
58+
m := map[string]gomodguard.BlockedVersion{k: {
59+
Version: v.Version,
60+
Reason: v.Reason,
61+
}}
62+
processorCfg.Blocked.Versions = append(processorCfg.Blocked.Versions, m)
63+
break
6964
}
65+
}
7066

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

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

80-
gomodguardErrors := processor.ProcessFiles(files)
81-
if len(gomodguardErrors) == 0 {
82-
return nil, nil
77+
for _, file := range pass.Files {
78+
files = append(files, pass.Fset.PositionFor(file.Pos(), false).Filename)
8379
}
8480

81+
gomodguardIssues := processor.ProcessFiles(files)
82+
8583
mu.Lock()
8684
defer mu.Unlock()
8785

88-
for _, err := range gomodguardErrors {
86+
for _, gomodguardIssue := range gomodguardIssues {
8987
issues = append(issues, goanalysis.NewIssue(&result.Issue{
9088
FromLinter: gomodguardName,
91-
Pos: err.Position,
92-
Text: err.Reason,
89+
Pos: gomodguardIssue.Position,
90+
Text: gomodguardIssue.Reason,
9391
}, pass))
9492
}
9593

0 commit comments

Comments
 (0)