Skip to content
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

fix: include custom linters in enable-all #3911

Merged
merged 3 commits into from Jun 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions docs/src/docs/contributing/new-linters.mdx
Expand Up @@ -83,8 +83,11 @@ If you're looking for instructions on how to configure your own custom linter, t

That is all the configuration that is required to run a custom linter in your project.

Custom linters are disabled by default, and are not enabled when `linters.enable-all` is specified.
They can be enabled by adding them the `linters.enable` list, or providing the enabled option on the command line (`golangci-lint run -Eexample`).
Custom linters are enabled by default, but abide by the same rules as other linters.

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;
they can be re-enabled by adding them to the `linters:enable` list,
or providing the enabled option on the command line, `golangci-lint run -Eexample`.

The configuration inside the `settings` field of linter have some limitations (there are NOT related to the plugin system itself):
we use Viper to handle the configuration but Viper put all the keys in lowercase, and `.` cannot be used inside a key.
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/executor.go
Expand Up @@ -120,7 +120,7 @@ func NewExecutor(buildInfo BuildInfo) *Executor {
}

// recreate after getting config
e.DBManager = lintersdb.NewManager(e.cfg, e.log).WithCustomLinters()
e.DBManager = lintersdb.NewManager(e.cfg, e.log)

// Slice options must be explicitly set for proper merging of config and command-line options.
fixSlicesFlags(e.runCmd.Flags())
Expand Down
19 changes: 7 additions & 12 deletions pkg/lint/lintersdb/custom_linters.go
Expand Up @@ -12,35 +12,30 @@ import (
"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/logutils"
"github.com/golangci/golangci-lint/pkg/report"
)

type AnalyzerPlugin interface {
GetAnalyzers() []*analysis.Analyzer
}

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

if m.cfg == nil {
return m
}
var linters []*linter.Config

for name, settings := range m.cfg.LintersSettings.Custom {
lc, err := m.loadCustomLinterConfig(name, settings)

if err != nil {
m.log.Errorf("Unable to load custom analyzer %s:%s, %v", name, settings.Path, err)
} else {
m.nameToLCs[name] = append(m.nameToLCs[name], lc)
linters = append(linters, lc)
}
}

return m
return linters
}

// loadCustomLinterConfig loads the configuration of private linters.
Expand Down
18 changes: 13 additions & 5 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -8,13 +8,16 @@ import (
)

type Manager struct {
nameToLCs map[string][]*linter.Config
cfg *config.Config
log logutils.Log
cfg *config.Config
log logutils.Log

nameToLCs map[string][]*linter.Config
customLinters []*linter.Config
}

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

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

const megacheckName = "megacheck"

var linters []*linter.Config
linters = append(linters, m.customLinters...)

// The linters are sorted in the alphabetical order (case-insensitive).
// When a new linter is added the version in `WithSince(...)` must be the next minor version of golangci-lint.
return []*linter.Config{
linters = append(linters,
linter.NewConfig(golinters.NewAsasalint(asasalintCfg)).
WithSince("1.47.0").
WithPresets(linter.PresetBugs).
Expand Down Expand Up @@ -878,7 +884,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetBugs).
WithLoadForGoAnalysis().
WithURL("https://github.com/ykadowak/zerologlint"),
}
)

return linters
}

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