From e9a36d481c34ee8018c9a39243dd5d02e168f39f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 5 Mar 2021 19:30:37 +0100 Subject: [PATCH 1/5] feat: add gomodreplace linter. --- .golangci.example.yml | 6 ++ go.mod | 1 + go.sum | 5 +- pkg/config/config.go | 6 ++ pkg/golinters/gomodreplace.go | 64 +++++++++++++++++++ pkg/lint/lintersdb/manager.go | 6 ++ .../processors/autogenerated_exclude.go | 4 ++ 7 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 pkg/golinters/gomodreplace.go diff --git a/.golangci.example.yml b/.golangci.example.yml index 8c55ddb27301..fc30e63e5fe2 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -449,6 +449,12 @@ linters-settings: servingv1: knative.dev/serving/pkg/apis/serving/v1 # using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package autoscalingv1alpha1: knative.dev/serving/pkg/apis/autoscaling/v1alpha1 + gomodreplace: + # Allow local `replace` directives. Default is false. + local: false + # List of allowed `replace` directives. Default is empty. + allow-list: + - launchpad.net/gocheck # The custom section can be used to define linter plugins to be loaded at runtime. See README doc # for more info. diff --git a/go.mod b/go.mod index 1e76ab4e7472..c338b43764e6 100644 --- a/go.mod +++ b/go.mod @@ -41,6 +41,7 @@ require ( github.com/kulti/thelper v0.4.0 github.com/kunwardeep/paralleltest v1.0.2 github.com/kyoh86/exportloopref v0.1.8 + github.com/ldez/gomodreplace v0.1.0 github.com/maratori/testpackage v1.0.1 github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0 github.com/mattn/go-colorable v0.1.8 diff --git a/go.sum b/go.sum index 9571cb8a4ef3..1d587b1ad8b1 100644 --- a/go.sum +++ b/go.sum @@ -246,6 +246,8 @@ github.com/kunwardeep/paralleltest v1.0.2 h1:/jJRv0TiqPoEy/Y8dQxCFJhD56uS/pnvtat github.com/kunwardeep/paralleltest v1.0.2/go.mod h1:ZPqNm1fVHPllh5LPVujzbVz1JN2GhLxSfY+oqUsvG30= github.com/kyoh86/exportloopref v0.1.8 h1:5Ry/at+eFdkX9Vsdw3qU4YkvGtzuVfzT4X7S77LoN/M= github.com/kyoh86/exportloopref v0.1.8/go.mod h1:1tUcJeiioIs7VWe5gcOObrux3lb66+sBqGZrRkMwPgg= +github.com/ldez/gomodreplace v0.1.0 h1:WurUDAl9AvpK9OOS1TNe9rFPw3TTV3TJVDM+Iceyk3E= +github.com/ldez/gomodreplace v0.1.0/go.mod h1:pP8BDbGsG/aVnjFdgOvInjHOg4GzKMnqJS4OM1goIyU= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= @@ -477,8 +479,9 @@ golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= -golang.org/x/mod v0.4.0 h1:8pl+sMODzuvGJkmj2W4kZihvVb5mKm8pB/X44PIQHv8= golang.org/x/mod v0.4.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= +golang.org/x/mod v0.4.1 h1:Kvvh58BN8Y9/lBi7hTekvtMpm07eUZ0ck5pRHpsMWrY= +golang.org/x/mod v0.4.1/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/pkg/config/config.go b/pkg/config/config.go index 202ae6bc44cb..f1343c74ec9a 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -276,6 +276,7 @@ type LintersSettings struct { Predeclared PredeclaredSettings Cyclop Cyclop ImportAs ImportAsSettings + GoModReplace GoModReplaceSettings Custom map[string]CustomLinterSettings } @@ -464,6 +465,11 @@ type Cyclop struct { type ImportAsSettings map[string]string +type GoModReplaceSettings struct { + AllowList []string `mapstructure:"allow-list"` + Local bool `mapstructure:"local"` +} + var defaultLintersSettings = LintersSettings{ Lll: LllSettings{ LineLength: 120, diff --git a/pkg/golinters/gomodreplace.go b/pkg/golinters/gomodreplace.go new file mode 100644 index 000000000000..b75c6b05d943 --- /dev/null +++ b/pkg/golinters/gomodreplace.go @@ -0,0 +1,64 @@ +package golinters + +import ( + "sync" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/ldez/gomodreplace" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" + "github.com/golangci/golangci-lint/pkg/lint/linter" + "github.com/golangci/golangci-lint/pkg/result" +) + +const goModReplaceName = "gomodreplace" + +// NewGoModReplace returns a new gomodreplace linter. +func NewGoModReplace(settings *config.GoModReplaceSettings) *goanalysis.Linter { + var issues []goanalysis.Issue + var mu sync.Mutex + + var opts gomodreplace.Options + if settings != nil { + opts.AllowLocal = settings.Local + opts.AllowList = settings.AllowList + } + + analyzer := &analysis.Analyzer{ + Name: goanalysis.TheOnlyAnalyzerName, + Doc: goanalysis.TheOnlyanalyzerDoc, + } + + return goanalysis.NewLinter( + goModReplaceName, + "Manage the use of replace directives in go.mod.", + []*analysis.Analyzer{analyzer}, + nil, + ).WithContextSetter(func(lintCtx *linter.Context) { + analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { + results, err := gomodreplace.Analyze(opts) + if err != nil { + lintCtx.Log.Warnf("running %s failed: %s: "+ + "if you are not using go modules it is suggested to disable this linter", goModReplaceName, err) + return nil, nil + } + + mu.Lock() + + for _, p := range results { + issues = append(issues, goanalysis.NewIssue(&result.Issue{ + FromLinter: goModReplaceName, + Pos: p.Start, + Text: p.Reason, + }, pass)) + } + + mu.Unlock() + + return nil, nil + } + }).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { + return issues + }).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 086309567b25..07fbba433ba0 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -100,6 +100,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var reviveCfg *config.ReviveSettings var cyclopCfg *config.Cyclop var importAsCfg *config.ImportAsSettings + var goModReplaceCfg *config.GoModReplaceSettings if m.cfg != nil { govetCfg = &m.cfg.LintersSettings.Govet testpackageCfg = &m.cfg.LintersSettings.Testpackage @@ -112,6 +113,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { reviveCfg = &m.cfg.LintersSettings.Revive cyclopCfg = &m.cfg.LintersSettings.Cyclop importAsCfg = &m.cfg.LintersSettings.ImportAs + goModReplaceCfg = &m.cfg.LintersSettings.GoModReplace } const megacheckName = "megacheck" lcs := []*linter.Config{ @@ -394,6 +396,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithLoadForGoAnalysis(). WithURL("https://github.com/gostaticanalysis/forcetypeassert"), + linter.NewConfig(golinters.NewGoModReplace(goModReplaceCfg)). + WithPresets(linter.PresetStyle). + WithLoadForGoAnalysis(). + WithURL("https://github.com/ldez/gomodreplace"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()). diff --git a/pkg/result/processors/autogenerated_exclude.go b/pkg/result/processors/autogenerated_exclude.go index 7894bbbcf613..57388f64fa48 100644 --- a/pkg/result/processors/autogenerated_exclude.go +++ b/pkg/result/processors/autogenerated_exclude.go @@ -53,6 +53,10 @@ func (p *AutogeneratedExclude) shouldPassIssue(i *result.Issue) (bool, error) { return true, nil } + if filepath.Base(i.FilePath()) == "go.mod" { + return true, nil + } + if isSpecialAutogeneratedFile(i.FilePath()) { return false, nil } From aa47c277bf0d7720cce2c0b2b568c80a7c01c4d6 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Fri, 5 Mar 2021 23:13:29 +0100 Subject: [PATCH 2/5] bump to v0.2.0 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index c338b43764e6..c43321df02e2 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( github.com/kulti/thelper v0.4.0 github.com/kunwardeep/paralleltest v1.0.2 github.com/kyoh86/exportloopref v0.1.8 - github.com/ldez/gomodreplace v0.1.0 + github.com/ldez/gomodreplace v0.2.0 github.com/maratori/testpackage v1.0.1 github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0 github.com/mattn/go-colorable v0.1.8 diff --git a/go.sum b/go.sum index 1d587b1ad8b1..0e20b986d74a 100644 --- a/go.sum +++ b/go.sum @@ -246,8 +246,8 @@ github.com/kunwardeep/paralleltest v1.0.2 h1:/jJRv0TiqPoEy/Y8dQxCFJhD56uS/pnvtat github.com/kunwardeep/paralleltest v1.0.2/go.mod h1:ZPqNm1fVHPllh5LPVujzbVz1JN2GhLxSfY+oqUsvG30= github.com/kyoh86/exportloopref v0.1.8 h1:5Ry/at+eFdkX9Vsdw3qU4YkvGtzuVfzT4X7S77LoN/M= github.com/kyoh86/exportloopref v0.1.8/go.mod h1:1tUcJeiioIs7VWe5gcOObrux3lb66+sBqGZrRkMwPgg= -github.com/ldez/gomodreplace v0.1.0 h1:WurUDAl9AvpK9OOS1TNe9rFPw3TTV3TJVDM+Iceyk3E= -github.com/ldez/gomodreplace v0.1.0/go.mod h1:pP8BDbGsG/aVnjFdgOvInjHOg4GzKMnqJS4OM1goIyU= +github.com/ldez/gomodreplace v0.2.0 h1:kSpO8kgsLCXj+y1K7i8qQGO5jOi8SxI47gtG4MiPzKU= +github.com/ldez/gomodreplace v0.2.0/go.mod h1:pP8BDbGsG/aVnjFdgOvInjHOg4GzKMnqJS4OM1goIyU= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= From 3fdbcfbfc69da8662d642ec49397fcee770362b4 Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 6 Mar 2021 01:09:05 +0100 Subject: [PATCH 3/5] gomodreplace -> gomoddirectives --- .golangci.example.yml | 10 +++++-- go.mod | 2 +- go.sum | 4 +-- pkg/config/config.go | 10 ++++--- .../{gomodreplace.go => gomoddirectives.go} | 28 ++++++++++--------- pkg/lint/lintersdb/manager.go | 8 +++--- 6 files changed, 35 insertions(+), 27 deletions(-) rename pkg/golinters/{gomodreplace.go => gomoddirectives.go} (62%) diff --git a/.golangci.example.yml b/.golangci.example.yml index fc30e63e5fe2..f81c646f8e63 100644 --- a/.golangci.example.yml +++ b/.golangci.example.yml @@ -449,12 +449,16 @@ linters-settings: servingv1: knative.dev/serving/pkg/apis/serving/v1 # using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package autoscalingv1alpha1: knative.dev/serving/pkg/apis/autoscaling/v1alpha1 - gomodreplace: + gomoddirectives: # Allow local `replace` directives. Default is false. - local: false + replace-local: false # List of allowed `replace` directives. Default is empty. - allow-list: + replace-allow-list: - launchpad.net/gocheck + # Allow to not explain why the version has been retracted in the `retract` directives. Default is false. + retract-allow-no-explanation: false + # Forbid the use of the `exclude` directives. Default is false. + exclude-forbidden: false # The custom section can be used to define linter plugins to be loaded at runtime. See README doc # for more info. diff --git a/go.mod b/go.mod index c43321df02e2..98d6707428e3 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( github.com/kulti/thelper v0.4.0 github.com/kunwardeep/paralleltest v1.0.2 github.com/kyoh86/exportloopref v0.1.8 - github.com/ldez/gomodreplace v0.2.0 + github.com/ldez/gomoddirectives v0.1.0 github.com/maratori/testpackage v1.0.1 github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0 github.com/mattn/go-colorable v0.1.8 diff --git a/go.sum b/go.sum index 0e20b986d74a..cbf9353f4c5d 100644 --- a/go.sum +++ b/go.sum @@ -246,8 +246,8 @@ github.com/kunwardeep/paralleltest v1.0.2 h1:/jJRv0TiqPoEy/Y8dQxCFJhD56uS/pnvtat github.com/kunwardeep/paralleltest v1.0.2/go.mod h1:ZPqNm1fVHPllh5LPVujzbVz1JN2GhLxSfY+oqUsvG30= github.com/kyoh86/exportloopref v0.1.8 h1:5Ry/at+eFdkX9Vsdw3qU4YkvGtzuVfzT4X7S77LoN/M= github.com/kyoh86/exportloopref v0.1.8/go.mod h1:1tUcJeiioIs7VWe5gcOObrux3lb66+sBqGZrRkMwPgg= -github.com/ldez/gomodreplace v0.2.0 h1:kSpO8kgsLCXj+y1K7i8qQGO5jOi8SxI47gtG4MiPzKU= -github.com/ldez/gomodreplace v0.2.0/go.mod h1:pP8BDbGsG/aVnjFdgOvInjHOg4GzKMnqJS4OM1goIyU= +github.com/ldez/gomoddirectives v0.1.0 h1:E4vz7c1lPkNQ+F8FDBhAmd62xMiYiIOKUIGGa+f22+4= +github.com/ldez/gomoddirectives v0.1.0/go.mod h1:sGicqkRgBOg//JfpXwkB9Hj0X5RyJ7mlACM5B9f6Me4= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= diff --git a/pkg/config/config.go b/pkg/config/config.go index f1343c74ec9a..8cd9fe85a8a9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -276,7 +276,7 @@ type LintersSettings struct { Predeclared PredeclaredSettings Cyclop Cyclop ImportAs ImportAsSettings - GoModReplace GoModReplaceSettings + GoModDirectives GoModDirectivesSettings Custom map[string]CustomLinterSettings } @@ -465,9 +465,11 @@ type Cyclop struct { type ImportAsSettings map[string]string -type GoModReplaceSettings struct { - AllowList []string `mapstructure:"allow-list"` - Local bool `mapstructure:"local"` +type GoModDirectivesSettings struct { + ReplaceAllowList []string `mapstructure:"replace-allow-list"` + ReplaceLocal bool `mapstructure:"replace-local"` + ExcludeForbidden bool `mapstructure:"exclude-forbidden"` + RetractAllowNoExplanation bool `mapstructure:"retract-allow-no-explanation"` } var defaultLintersSettings = LintersSettings{ diff --git a/pkg/golinters/gomodreplace.go b/pkg/golinters/gomoddirectives.go similarity index 62% rename from pkg/golinters/gomodreplace.go rename to pkg/golinters/gomoddirectives.go index b75c6b05d943..8808c2de119a 100644 --- a/pkg/golinters/gomodreplace.go +++ b/pkg/golinters/gomoddirectives.go @@ -3,26 +3,28 @@ package golinters import ( "sync" - "github.com/golangci/golangci-lint/pkg/config" - "github.com/ldez/gomodreplace" + "github.com/ldez/gomoddirectives" "golang.org/x/tools/go/analysis" + "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" "github.com/golangci/golangci-lint/pkg/lint/linter" "github.com/golangci/golangci-lint/pkg/result" ) -const goModReplaceName = "gomodreplace" +const goModDirectivesName = "gomoddirectives" -// NewGoModReplace returns a new gomodreplace linter. -func NewGoModReplace(settings *config.GoModReplaceSettings) *goanalysis.Linter { +// NewGoModDirectives returns a new gomoddirectives linter. +func NewGoModDirectives(settings *config.GoModDirectivesSettings) *goanalysis.Linter { var issues []goanalysis.Issue var mu sync.Mutex - var opts gomodreplace.Options + var opts gomoddirectives.Options if settings != nil { - opts.AllowLocal = settings.Local - opts.AllowList = settings.AllowList + opts.ReplaceAllowLocal = settings.ReplaceLocal + opts.ReplaceAllowList = settings.ReplaceAllowList + opts.RetractAllowNoExplanation = settings.RetractAllowNoExplanation + opts.ExcludeForbidden = settings.ExcludeForbidden } analyzer := &analysis.Analyzer{ @@ -31,16 +33,16 @@ func NewGoModReplace(settings *config.GoModReplaceSettings) *goanalysis.Linter { } return goanalysis.NewLinter( - goModReplaceName, - "Manage the use of replace directives in go.mod.", + goModDirectivesName, + "Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.", []*analysis.Analyzer{analyzer}, nil, ).WithContextSetter(func(lintCtx *linter.Context) { analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { - results, err := gomodreplace.Analyze(opts) + results, err := gomoddirectives.Analyze(opts) if err != nil { lintCtx.Log.Warnf("running %s failed: %s: "+ - "if you are not using go modules it is suggested to disable this linter", goModReplaceName, err) + "if you are not using go modules it is suggested to disable this linter", goModDirectivesName, err) return nil, nil } @@ -48,7 +50,7 @@ func NewGoModReplace(settings *config.GoModReplaceSettings) *goanalysis.Linter { for _, p := range results { issues = append(issues, goanalysis.NewIssue(&result.Issue{ - FromLinter: goModReplaceName, + FromLinter: goModDirectivesName, Pos: p.Start, Text: p.Reason, }, pass)) diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index 07fbba433ba0..5f1b1f0f5a45 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -100,7 +100,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { var reviveCfg *config.ReviveSettings var cyclopCfg *config.Cyclop var importAsCfg *config.ImportAsSettings - var goModReplaceCfg *config.GoModReplaceSettings + var goModDirectivesCfg *config.GoModDirectivesSettings if m.cfg != nil { govetCfg = &m.cfg.LintersSettings.Govet testpackageCfg = &m.cfg.LintersSettings.Testpackage @@ -113,7 +113,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { reviveCfg = &m.cfg.LintersSettings.Revive cyclopCfg = &m.cfg.LintersSettings.Cyclop importAsCfg = &m.cfg.LintersSettings.ImportAs - goModReplaceCfg = &m.cfg.LintersSettings.GoModReplace + goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives } const megacheckName = "megacheck" lcs := []*linter.Config{ @@ -396,10 +396,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithLoadForGoAnalysis(). WithURL("https://github.com/gostaticanalysis/forcetypeassert"), - linter.NewConfig(golinters.NewGoModReplace(goModReplaceCfg)). + linter.NewConfig(golinters.NewGoModDirectives(goModDirectivesCfg)). WithPresets(linter.PresetStyle). WithLoadForGoAnalysis(). - WithURL("https://github.com/ldez/gomodreplace"), + WithURL("https://github.com/ldez/gomoddirectives"), // nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives linter.NewConfig(golinters.NewNoLintLint()). From 22957f308dcc05ca2aa3b3a93625fc021868937f Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 6 Mar 2021 13:02:23 +0100 Subject: [PATCH 4/5] feat: once. --- pkg/golinters/gomoddirectives.go | 34 +++++++++++++++----------------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/pkg/golinters/gomoddirectives.go b/pkg/golinters/gomoddirectives.go index 8808c2de119a..40d3bf786e09 100644 --- a/pkg/golinters/gomoddirectives.go +++ b/pkg/golinters/gomoddirectives.go @@ -17,7 +17,7 @@ const goModDirectivesName = "gomoddirectives" // NewGoModDirectives returns a new gomoddirectives linter. func NewGoModDirectives(settings *config.GoModDirectivesSettings) *goanalysis.Linter { var issues []goanalysis.Issue - var mu sync.Mutex + var once sync.Once var opts gomoddirectives.Options if settings != nil { @@ -39,24 +39,22 @@ func NewGoModDirectives(settings *config.GoModDirectivesSettings) *goanalysis.Li nil, ).WithContextSetter(func(lintCtx *linter.Context) { analyzer.Run = func(pass *analysis.Pass) (interface{}, error) { - results, err := gomoddirectives.Analyze(opts) - if err != nil { - lintCtx.Log.Warnf("running %s failed: %s: "+ - "if you are not using go modules it is suggested to disable this linter", goModDirectivesName, err) - return nil, nil - } + once.Do(func() { + results, err := gomoddirectives.Analyze(opts) + if err != nil { + lintCtx.Log.Warnf("running %s failed: %s: "+ + "if you are not using go modules it is suggested to disable this linter", goModDirectivesName, err) + return + } - mu.Lock() - - for _, p := range results { - issues = append(issues, goanalysis.NewIssue(&result.Issue{ - FromLinter: goModDirectivesName, - Pos: p.Start, - Text: p.Reason, - }, pass)) - } - - mu.Unlock() + for _, p := range results { + issues = append(issues, goanalysis.NewIssue(&result.Issue{ + FromLinter: goModDirectivesName, + Pos: p.Start, + Text: p.Reason, + }, pass)) + } + }) return nil, nil } From c9a3a71409caf9d43600172fd81f9f76b2423c9d Mon Sep 17 00:00:00 2001 From: Fernandez Ludovic Date: Sat, 6 Mar 2021 14:28:37 +0100 Subject: [PATCH 5/5] bump to v0.2.1 --- go.mod | 2 +- go.sum | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index 98d6707428e3..5ad5edfafb18 100644 --- a/go.mod +++ b/go.mod @@ -41,7 +41,7 @@ require ( github.com/kulti/thelper v0.4.0 github.com/kunwardeep/paralleltest v1.0.2 github.com/kyoh86/exportloopref v0.1.8 - github.com/ldez/gomoddirectives v0.1.0 + github.com/ldez/gomoddirectives v0.2.1 github.com/maratori/testpackage v1.0.1 github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0 github.com/mattn/go-colorable v0.1.8 diff --git a/go.sum b/go.sum index cbf9353f4c5d..c703d6f5024a 100644 --- a/go.sum +++ b/go.sum @@ -246,8 +246,8 @@ github.com/kunwardeep/paralleltest v1.0.2 h1:/jJRv0TiqPoEy/Y8dQxCFJhD56uS/pnvtat github.com/kunwardeep/paralleltest v1.0.2/go.mod h1:ZPqNm1fVHPllh5LPVujzbVz1JN2GhLxSfY+oqUsvG30= github.com/kyoh86/exportloopref v0.1.8 h1:5Ry/at+eFdkX9Vsdw3qU4YkvGtzuVfzT4X7S77LoN/M= github.com/kyoh86/exportloopref v0.1.8/go.mod h1:1tUcJeiioIs7VWe5gcOObrux3lb66+sBqGZrRkMwPgg= -github.com/ldez/gomoddirectives v0.1.0 h1:E4vz7c1lPkNQ+F8FDBhAmd62xMiYiIOKUIGGa+f22+4= -github.com/ldez/gomoddirectives v0.1.0/go.mod h1:sGicqkRgBOg//JfpXwkB9Hj0X5RyJ7mlACM5B9f6Me4= +github.com/ldez/gomoddirectives v0.2.1 h1:9pAcW9KRZW7HQjFwbozNvFMcNVwdCBufU7os5QUwLIY= +github.com/ldez/gomoddirectives v0.2.1/go.mod h1:sGicqkRgBOg//JfpXwkB9Hj0X5RyJ7mlACM5B9f6Me4= github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo= github.com/lib/pq v1.9.0/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/logrusorgru/aurora v0.0.0-20181002194514-a7b3b318ed4e/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=