Skip to content

Commit 6c7aba6

Browse files
authored
fix: include custom linters in enable-all (#3911)
1 parent d147d8b commit 6c7aba6

File tree

4 files changed

+31
-25
lines changed

4 files changed

+31
-25
lines changed

docs/src/docs/contributing/new-linters.mdx

+5-2
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ If you're looking for instructions on how to configure your own custom linter, t
8383
8484
That is all the configuration that is required to run a custom linter in your project.
8585
86-
Custom linters are disabled by default, and are not enabled when `linters.enable-all` is specified.
87-
They can be enabled by adding them the `linters.enable` list, or providing the enabled option on the command line (`golangci-lint run -Eexample`).
86+
Custom linters are enabled by default, but abide by the same rules as other linters.
87+
88+
If the disable all option is specified either on command line or in `.golang.yml` files `linters.disable-all: true`, custom linters will be disabled;
89+
they can be re-enabled by adding them to the `linters:enable` list,
90+
or providing the enabled option on the command line, `golangci-lint run -Eexample`.
8891

8992
The configuration inside the `settings` field of linter have some limitations (there are NOT related to the plugin system itself):
9093
we use Viper to handle the configuration but Viper put all the keys in lowercase, and `.` cannot be used inside a key.

pkg/commands/executor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func NewExecutor(buildInfo BuildInfo) *Executor {
120120
}
121121

122122
// recreate after getting config
123-
e.DBManager = lintersdb.NewManager(e.cfg, e.log).WithCustomLinters()
123+
e.DBManager = lintersdb.NewManager(e.cfg, e.log)
124124

125125
// Slice options must be explicitly set for proper merging of config and command-line options.
126126
fixSlicesFlags(e.runCmd.Flags())

pkg/lint/lintersdb/custom_linters.go

+7-12
Original file line numberDiff line numberDiff line change
@@ -12,35 +12,30 @@ import (
1212
"github.com/golangci/golangci-lint/pkg/config"
1313
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
1414
"github.com/golangci/golangci-lint/pkg/lint/linter"
15-
"github.com/golangci/golangci-lint/pkg/logutils"
16-
"github.com/golangci/golangci-lint/pkg/report"
1715
)
1816

1917
type AnalyzerPlugin interface {
2018
GetAnalyzers() []*analysis.Analyzer
2119
}
2220

23-
// WithCustomLinters loads private linters that are specified in the golangci config file.
24-
func (m *Manager) WithCustomLinters() *Manager {
25-
if m.log == nil {
26-
m.log = report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
21+
// getCustomLinterConfigs loads private linters that are specified in the golangci config file.
22+
func (m *Manager) getCustomLinterConfigs() []*linter.Config {
23+
if m.cfg == nil || m.log == nil {
24+
return nil
2725
}
2826

29-
if m.cfg == nil {
30-
return m
31-
}
27+
var linters []*linter.Config
3228

3329
for name, settings := range m.cfg.LintersSettings.Custom {
3430
lc, err := m.loadCustomLinterConfig(name, settings)
35-
3631
if err != nil {
3732
m.log.Errorf("Unable to load custom analyzer %s:%s, %v", name, settings.Path, err)
3833
} else {
39-
m.nameToLCs[name] = append(m.nameToLCs[name], lc)
34+
linters = append(linters, lc)
4035
}
4136
}
4237

43-
return m
38+
return linters
4439
}
4540

4641
// loadCustomLinterConfig loads the configuration of private linters.

pkg/lint/lintersdb/manager.go

+18-10
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
)
99

1010
type Manager struct {
11-
nameToLCs map[string][]*linter.Config
12-
cfg *config.Config
13-
log logutils.Log
11+
cfg *config.Config
12+
log logutils.Log
13+
14+
nameToLCs map[string][]*linter.Config
15+
customLinters []*linter.Config
1416
}
1517

1618
func NewManager(cfg *config.Config, log logutils.Log) *Manager {
1719
m := &Manager{cfg: cfg, log: log}
20+
m.customLinters = m.getCustomLinterConfigs()
1821

1922
nameToLCs := make(map[string][]*linter.Config)
2023
for _, lc := range m.GetAllSupportedLinterConfigs() {
@@ -247,9 +250,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
247250

248251
const megacheckName = "megacheck"
249252

253+
var linters []*linter.Config
254+
linters = append(linters, m.customLinters...)
255+
250256
// The linters are sorted in the alphabetical order (case-insensitive).
251257
// When a new linter is added the version in `WithSince(...)` must be the next minor version of golangci-lint.
252-
return []*linter.Config{
258+
linters = append(linters,
253259
linter.NewConfig(golinters.NewAsasalint(asasalintCfg)).
254260
WithSince("1.47.0").
255261
WithPresets(linter.PresetBugs).
@@ -867,18 +873,20 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
867873
WithPresets(linter.PresetStyle).
868874
WithURL("https://github.com/bombsimon/wsl"),
869875

876+
linter.NewConfig(golinters.NewZerologLint()).
877+
WithSince("v1.53.0").
878+
WithPresets(linter.PresetBugs).
879+
WithLoadForGoAnalysis().
880+
WithURL("https://github.com/ykadowak/zerologlint"),
881+
870882
// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
871883
linter.NewConfig(golinters.NewNoLintLint(noLintLintCfg)).
872884
WithSince("v1.26.0").
873885
WithPresets(linter.PresetStyle).
874886
WithURL("https://github.com/golangci/golangci-lint/blob/master/pkg/golinters/nolintlint/README.md"),
887+
)
875888

876-
linter.NewConfig(golinters.NewZerologLint()).
877-
WithSince("v1.53.0").
878-
WithPresets(linter.PresetBugs).
879-
WithLoadForGoAnalysis().
880-
WithURL("https://github.com/ykadowak/zerologlint"),
881-
}
889+
return linters
882890
}
883891

884892
func (m Manager) GetAllEnabledByDefaultLinters() []*linter.Config {

0 commit comments

Comments
 (0)