-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
plugin: allow to use settings for plugins #3887
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
943205d
chore: move code related to plugin to a dedicated file
ldez 63621b4
feat: allow to use settings for plugins
ldez 9129bc8
feat: improve fallback
ldez f9fc8ba
docs: update doc to use the New function
ldez d0e8a23
feat: deprecate AnalyzerPlugin
ldez 16ca822
docs: add settings limitations
ldez b27c674
chore: downgrade to go1.19
ldez bb1ff3f
Update pkg/lint/lintersdb/custom_linters.go
ldez 3bb0ccb
chore: use multierror
ldez 0874476
fix: error handling
ldez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package lintersdb | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"plugin" | ||
|
||
"github.com/hashicorp/go-multierror" | ||
"github.com/spf13/viper" | ||
"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/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{}) | ||
} | ||
|
||
if m.cfg == nil { | ||
return m | ||
} | ||
|
||
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) | ||
} | ||
} | ||
|
||
return m | ||
} | ||
|
||
// loadCustomLinterConfig loads the configuration of private linters. | ||
// Private linters are dynamically loaded from .so plugin files. | ||
func (m *Manager) loadCustomLinterConfig(name string, settings config.CustomLinterSettings) (*linter.Config, error) { | ||
analyzers, err := m.getAnalyzerPlugin(settings.Path, settings.Settings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m.log.Infof("Loaded %s: %s", settings.Path, name) | ||
|
||
customLinter := goanalysis.NewLinter(name, settings.Description, analyzers, nil). | ||
WithLoadMode(goanalysis.LoadModeTypesInfo) | ||
|
||
linterConfig := linter.NewConfig(customLinter). | ||
WithEnabledByDefault(). | ||
WithLoadForGoAnalysis(). | ||
WithURL(settings.OriginalURL) | ||
|
||
return linterConfig, nil | ||
} | ||
|
||
// getAnalyzerPlugin loads a private linter as specified in the config file, | ||
// loads the plugin from a .so file, | ||
// and returns the 'AnalyzerPlugin' interface implemented by the private plugin. | ||
// An error is returned if the private linter cannot be loaded | ||
// or the linter does not implement the AnalyzerPlugin interface. | ||
func (m *Manager) getAnalyzerPlugin(path string, settings any) ([]*analysis.Analyzer, error) { | ||
if !filepath.IsAbs(path) { | ||
// resolve non-absolute paths relative to config file's directory | ||
configFilePath := viper.ConfigFileUsed() | ||
absConfigFilePath, err := filepath.Abs(configFilePath) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not get absolute representation of config file path %q: %v", configFilePath, err) | ||
} | ||
path = filepath.Join(filepath.Dir(absConfigFilePath), path) | ||
} | ||
|
||
plug, err := plugin.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
analyzers, err := m.lookupPlugin(plug, settings) | ||
if err != nil { | ||
return nil, fmt.Errorf("lookup plugin %s: %w", path, err) | ||
} | ||
|
||
return analyzers, nil | ||
} | ||
|
||
func (m *Manager) lookupPlugin(plug *plugin.Plugin, settings any) ([]*analysis.Analyzer, error) { | ||
symbol, err := plug.Lookup("New") | ||
bombsimon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
analyzers, errP := m.lookupAnalyzerPlugin(plug) | ||
if errP != nil { | ||
// TODO(ldez): use `errors.Join` when we will upgrade to go1.20. | ||
ldez marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return nil, multierror.Append(err, errP) | ||
} | ||
|
||
return analyzers, nil | ||
} | ||
|
||
// The type func cannot be used here, must be the explicit signature. | ||
constructor, ok := symbol.(func(any) ([]*analysis.Analyzer, error)) | ||
if !ok { | ||
return nil, fmt.Errorf("plugin does not abide by 'New' function: %T", symbol) | ||
} | ||
|
||
return constructor(settings) | ||
} | ||
|
||
func (m *Manager) lookupAnalyzerPlugin(plug *plugin.Plugin) ([]*analysis.Analyzer, error) { | ||
symbol, err := plug.Lookup("AnalyzerPlugin") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
m.log.Warnf("plugin: 'AnalyzerPlugin' plugins are deprecated, please use the new plugin signature: " + | ||
"https://golangci-lint.run/contributing/new-linters/#create-a-plugin") | ||
|
||
analyzerPlugin, ok := symbol.(AnalyzerPlugin) | ||
if !ok { | ||
return nil, fmt.Errorf("plugin does not abide by 'AnalyzerPlugin' interface: %T", symbol) | ||
} | ||
|
||
return analyzerPlugin.GetAnalyzers(), nil | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.