mirror of
https://github.com/mgechev/revive.git
synced 2025-07-17 01:12:27 +02:00
* Support go workspaces when detecting the go version. When a module is part of a workspace, a call to `go list -m` lists all modules in the workspace, and we need to parse multiple modinfos. * Do not invoke `go list` for every package. * Add a go language version override config option for golangci-lint.
This commit is contained in:
@ -1,5 +1,9 @@
|
|||||||
package lint
|
package lint
|
||||||
|
|
||||||
|
import (
|
||||||
|
goversion "github.com/hashicorp/go-version"
|
||||||
|
)
|
||||||
|
|
||||||
// Arguments is type used for the arguments of a rule.
|
// Arguments is type used for the arguments of a rule.
|
||||||
type Arguments = []interface{}
|
type Arguments = []interface{}
|
||||||
|
|
||||||
@ -61,4 +65,7 @@ type Config struct {
|
|||||||
WarningCode int `toml:"warningCode"`
|
WarningCode int `toml:"warningCode"`
|
||||||
Directives DirectivesConfig `toml:"directive"`
|
Directives DirectivesConfig `toml:"directive"`
|
||||||
Exclude []string `toml:"exclude"`
|
Exclude []string `toml:"exclude"`
|
||||||
|
// If set, overrides the go language version specified in go.mod of
|
||||||
|
// packages being linted, and assumes this specific language version.
|
||||||
|
GoVersion *goversion.Version
|
||||||
}
|
}
|
||||||
|
@ -63,16 +63,52 @@ var (
|
|||||||
func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-chan Failure, error) {
|
func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-chan Failure, error) {
|
||||||
failures := make(chan Failure)
|
failures := make(chan Failure)
|
||||||
|
|
||||||
|
perModVersions := make(map[string]*goversion.Version)
|
||||||
|
perPkgVersions := make([]*goversion.Version, len(packages))
|
||||||
|
for n, files := range packages {
|
||||||
|
if len(files) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if config.GoVersion != nil {
|
||||||
|
perPkgVersions[n] = config.GoVersion
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
dir, err := filepath.Abs(filepath.Dir(files[0]))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
alreadyKnownMod := false
|
||||||
|
for d, v := range perModVersions {
|
||||||
|
if strings.HasPrefix(dir, d) {
|
||||||
|
perPkgVersions[n] = v
|
||||||
|
alreadyKnownMod = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if alreadyKnownMod {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
d, v, err := detectGoMod(dir)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
perModVersions[d] = v
|
||||||
|
perPkgVersions[n] = v
|
||||||
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for _, pkg := range packages {
|
for n := range packages {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(pkg []string) {
|
go func(pkg []string, gover *goversion.Version) {
|
||||||
if err := l.lintPackage(pkg, ruleSet, config, failures); err != nil {
|
if err := l.lintPackage(pkg, gover, ruleSet, config, failures); err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
}(pkg)
|
}(packages[n], perPkgVersions[n])
|
||||||
}
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
@ -83,20 +119,15 @@ func (l *Linter) Lint(packages [][]string, ruleSet []Rule, config Config) (<-cha
|
|||||||
return failures, nil
|
return failures, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Linter) lintPackage(filenames []string, ruleSet []Rule, config Config, failures chan Failure) error {
|
func (l *Linter) lintPackage(filenames []string, gover *goversion.Version, ruleSet []Rule, config Config, failures chan Failure) error {
|
||||||
if len(filenames) == 0 {
|
if len(filenames) == 0 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
goVersion, err := detectGoVersion(filepath.Dir(filenames[0]))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
pkg := &Package{
|
pkg := &Package{
|
||||||
fset: token.NewFileSet(),
|
fset: token.NewFileSet(),
|
||||||
files: map[string]*File{},
|
files: map[string]*File{},
|
||||||
goVersion: goVersion,
|
goVersion: gover,
|
||||||
}
|
}
|
||||||
for _, filename := range filenames {
|
for _, filename := range filenames {
|
||||||
content, err := l.readFile(filename)
|
content, err := l.readFile(filename)
|
||||||
@ -124,37 +155,38 @@ func (l *Linter) lintPackage(filenames []string, ruleSet []Rule, config Config,
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func detectGoVersion(dir string) (ver *goversion.Version, err error) {
|
func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error) {
|
||||||
// https://github.com/golang/go/issues/44753#issuecomment-790089020
|
// https://github.com/golang/go/issues/44753#issuecomment-790089020
|
||||||
cmd := exec.Command("go", "list", "-m", "-json")
|
cmd := exec.Command("go", "list", "-m", "-json")
|
||||||
cmd.Dir = dir
|
cmd.Dir = dir
|
||||||
|
|
||||||
raw, err := cmd.Output()
|
out, err := cmd.Output()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("command go list: %w", err)
|
return "", nil, fmt.Errorf("command go list: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var v struct {
|
// NOTE: A package may be part of a go workspace. In this case `go list -m`
|
||||||
GoMod string `json:"GoMod"`
|
// lists all modules in the workspace, so we need to go through them all.
|
||||||
GoVersion string `json:"GoVersion"`
|
d := json.NewDecoder(bytes.NewBuffer(out))
|
||||||
}
|
for d.More() {
|
||||||
if err = json.Unmarshal(raw, &v); err != nil {
|
var v struct {
|
||||||
return nil, fmt.Errorf("can't parse the output of go list: %w", err)
|
GoMod string `json:"GoMod"`
|
||||||
}
|
GoVersion string `json:"GoVersion"`
|
||||||
|
Dir string `json:"Dir"`
|
||||||
if v.GoMod == "" {
|
}
|
||||||
// this package is outside a module, so assume
|
if err = d.Decode(&v); err != nil {
|
||||||
// an old-style source directory
|
return "", nil, err
|
||||||
|
}
|
||||||
if v := os.Getenv("GOVERSION"); v != "" {
|
if v.GoMod == "" {
|
||||||
return goversion.NewVersion(strings.TrimPrefix(v, "go"))
|
return "", nil, fmt.Errorf("not part of a module: %q", dir)
|
||||||
|
}
|
||||||
|
if v.Dir != "" && strings.HasPrefix(dir, v.Dir) {
|
||||||
|
rootDir = v.Dir
|
||||||
|
ver, err = goversion.NewVersion(strings.TrimPrefix(v.GoVersion, "go"))
|
||||||
|
return rootDir, ver, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// assume the last version that does not have generics
|
|
||||||
return goversion.Must(goversion.NewVersion("1.17")), nil
|
|
||||||
}
|
}
|
||||||
|
return "", nil, fmt.Errorf("not part of a module: %q", dir)
|
||||||
return goversion.NewVersion(strings.TrimPrefix(v.GoVersion, "go"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// isGenerated reports whether the source file is generated code
|
// isGenerated reports whether the source file is generated code
|
||||||
|
Reference in New Issue
Block a user