Skip to content

feat: add auto detection of module name for goimport #3504

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
wants to merge 1 commit into from
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
3 changes: 3 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,9 @@ linters-settings:
# with the given prefixes are grouped after 3rd-party packages.
# Default: ""
local-prefixes: github.com/org/project
# If set to true and local-prefixes is empty, the module name from go.mod
# will automatically be used as the local prefix.
auto-local-prefix: false

golint:
# Minimal confidence for issues.
Expand Down
10 changes: 8 additions & 2 deletions pkg/commands/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,14 @@ func NewExecutor(version, commit, date string) *Executor {
e.log.Fatalf("Can't read config: %s", err)
}

if (commandLineCfg == nil || commandLineCfg.Run.Go == "") && e.cfg != nil && e.cfg.Run.Go == "" {
e.cfg.Run.Go = config.DetectGoVersion()
if e.cfg != nil {
if (commandLineCfg == nil || commandLineCfg.Run.Go == "") && e.cfg.Run.Go == "" {
e.cfg.Run.Go = config.DetectGoVersion()
}

if (commandLineCfg == nil || commandLineCfg.Run.Module == "") && e.cfg.Run.Module == "" {
e.cfg.Run.Module = config.DetectGoModuleName()
}
}

// recreate after getting config
Expand Down
5 changes: 5 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,8 @@ func DetectGoVersion() string {

return "1.17"
}

func DetectGoModuleName() string {
file, _ := gomoddirectives.GetModuleFile()
return file.Module.Mod.Path
}
3 changes: 2 additions & 1 deletion pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,8 @@ type GoHeaderSettings struct {
}

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

type GoLintSettings struct {
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ type Run struct {

Args []string

Go string `mapstructure:"go"`
Go string `mapstructure:"go"`
Module string `mapstructure:"module"`

BuildTags []string `mapstructure:"build-tags"`
ModulesDownloadMode string `mapstructure:"modules-download-mode"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
if unusedCfg != nil && unusedCfg.GoVersion == "" {
unusedCfg.GoVersion = m.cfg.Run.Go
}
if goimportsCfg != nil && goimportsCfg.LocalPrefixes == "" && goimportsCfg.AutoLocalPrefix {
goimportsCfg.LocalPrefixes = m.cfg.Run.Module
}
}

const megacheckName = "megacheck"
Expand Down
3 changes: 3 additions & 0 deletions test/testdata/configs/goimports_local_auto.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
linters-settings:
goimports:
auto-local-prefix: true
16 changes: 16 additions & 0 deletions test/testdata/goimports_local_auto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//golangcitest:args -Egoimports
//golangcitest:config_path testdata/configs/goimports_local_auto.yml
package testdata

import (
"fmt"

"github.com/golangci/golangci-lint/pkg/config" // want "File is not `goimports`-ed with -local github.com/golangci/golangci-lint"
"github.com/pkg/errors"
)

func GoimportsLocalAutoPrefixTest() {
fmt.Print("x")
_ = config.Config{}
_ = errors.New("")
}