Skip to content

Automatically detect local prefixes #2363

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ type FunlenSettings struct {
}

type GciSettings struct {
LocalPrefixes string `mapstructure:"local-prefixes"`
LocalPrefixes string `mapstructure:"local-prefixes"`
LocalPrefixModule bool `mapstructure:"local-prefix-module"`
}

type GocognitSettings struct {
Expand Down Expand Up @@ -277,7 +278,8 @@ type GoHeaderSettings struct {
}

type GoImportsSettings struct {
LocalPrefixes string `mapstructure:"local-prefixes"`
LocalPrefixes string `mapstructure:"local-prefixes"`
LocalPrefixModule bool `mapstructure:"local-prefix-module"`
}

type GoLintSettings struct {
Expand Down
22 changes: 22 additions & 0 deletions pkg/golinters/gci.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package golinters
import (
"bytes"
"fmt"
"strings"
"sync"

"github.com/daixiang0/gci/pkg/gci"
Expand Down Expand Up @@ -32,11 +33,32 @@ func NewGci() *goanalysis.Linter {
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
localFlag := lintCtx.Settings().Gci.LocalPrefixes

if localFlag == "" && lintCtx.Settings().Gci.LocalPrefixModule {
var modules []string

for _, module := range lintCtx.Modules {
modules = append(modules, module.Path)
}

localFlag = strings.Join(modules, ",")
}

goimportsFlag := lintCtx.Settings().Goimports.LocalPrefixes
if localFlag == "" && goimportsFlag != "" {
localFlag = goimportsFlag
}

if localFlag == "" && lintCtx.Settings().Goimports.LocalPrefixModule {
var modules []string

for _, module := range lintCtx.Modules {
modules = append(modules, module.Path)
}

localFlag = strings.Join(modules, ",")
}

Comment on lines +36 to +61
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These looks exactly the same with some duplicated code, maybe would be nice to create a function?

localFlagFn := func(modules) string {
	var modules []string

	for _, module := range lintCtx.Modules {
		modules = append(modules, module.Path)
	}

	return strings.Join(modules, ",")
}

Then just call it twice:

if localFlag == "" && lintCtx.Settings().Gci.LocalPrefixModule {
	localFlag = localFlagFn(lintCtx.Settings().Gci.LocalPrefixModule)
}

goimportsFlag := ...


if localFlag == "" && lintCtx.Settings().Goimports.LocalPrefixModule {
	localFlag = localFlagFn(lintCtx.Settings().Goimports.LocalPrefixModule)
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Absolutely!

analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
Expand Down
12 changes: 12 additions & 0 deletions pkg/golinters/goimports.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package golinters

import (
"strings"
"sync"

goimportsAPI "github.com/golangci/gofmt/goimports"
Expand Down Expand Up @@ -29,6 +30,17 @@ func NewGoimports() *goanalysis.Linter {
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
imports.LocalPrefix = lintCtx.Settings().Goimports.LocalPrefixes

if imports.LocalPrefix == "" && lintCtx.Settings().Goimports.LocalPrefixModule {
var modules []string

for _, module := range lintCtx.Modules {
modules = append(modules, module.Path)
}

imports.LocalPrefix = strings.Join(modules, ",")
}

analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
var fileNames []string
for _, f := range pass.Files {
Expand Down
2 changes: 2 additions & 0 deletions pkg/lint/linter/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Context struct {
// version for each of packages
OriginalPackages []*packages.Package

Modules []*packages.Module

Cfg *config.Config
FileCache *fsutils.FileCache
LineCache *fsutils.LineCache
Expand Down
20 changes: 19 additions & 1 deletion pkg/lint/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (cl *ContextLoader) prepareBuildContext() {
}

func (cl *ContextLoader) findLoadMode(linters []*linter.Config) packages.LoadMode {
loadMode := packages.LoadMode(0)
loadMode := packages.LoadMode(0) | packages.NeedModule // TODO: make this part of the linter load mode?
for _, lc := range linters {
loadMode |= lc.LoadMode
}
Expand Down Expand Up @@ -283,6 +283,20 @@ func (cl *ContextLoader) filterDuplicatePackages(pkgs []*packages.Package) []*pa
return retPkgs
}

func (cl *ContextLoader) getModules(pkgs []*packages.Package) []*packages.Module {
modules := []*packages.Module{}
moduleIndex := map[string]bool{}

for _, pkg := range pkgs {
if pkg.Module != nil && moduleIndex[pkg.Module.Path] == false {
moduleIndex[pkg.Module.Path] = true
modules = append(modules, pkg.Module)
}
}

return modules
}

func (cl *ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*linter.Context, error) {
loadMode := cl.findLoadMode(linters)
pkgs, err := cl.loadPackages(ctx, loadMode)
Expand All @@ -296,13 +310,17 @@ func (cl *ContextLoader) Load(ctx context.Context, linters []*linter.Config) (*l
return nil, exitcodes.ErrNoGoFiles
}

modules := cl.getModules(pkgs)

ret := &linter.Context{
Packages: deduplicatedPkgs,

// At least `unused` linters works properly only on original (not deduplicated) packages,
// see https://github.com/golangci/golangci-lint/pull/585.
OriginalPackages: pkgs,

Modules: modules,

Cfg: cl.cfg,
Log: cl.log,
FileCache: cl.fileCache,
Expand Down