Skip to content

Add home directory to config file search paths #1325

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 4 commits into from
Aug 24, 2020
Merged
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
1 change: 1 addition & 0 deletions docs/src/docs/usage/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ GolangCI-Lint looks for config files in the following paths from the current wor
- `.golangci.json`

GolangCI-Lint also searches for config files in all directories from the directory of the first analyzed path up to the root.
If no configuration file has been found, GolangCI-Lint will try to find one in your home directory.
To see which config file is being used and where it was sourced from run golangci-lint with `-v` option.

Config options inside the file are identical to command-line options.
Expand Down
17 changes: 17 additions & 0 deletions pkg/config/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ func (r *FileReader) setupConfigFileSearch() {

// find all dirs from it up to the root
configSearchPaths := []string{"./"}

for {
configSearchPaths = append(configSearchPaths, curDir)
newCurDir := filepath.Dir(curDir)
Expand All @@ -179,13 +180,29 @@ func (r *FileReader) setupConfigFileSearch() {
curDir = newCurDir
}

// find home directory for global config
if home, err := homedir.Dir(); err != nil {
r.log.Warnf("Can't get user's home directory: %s", err.Error())
} else if !sliceContains(configSearchPaths, home) {
configSearchPaths = append(configSearchPaths, home)
}

r.log.Infof("Config search paths: %s", configSearchPaths)
viper.SetConfigName(".golangci")
for _, p := range configSearchPaths {
viper.AddConfigPath(p)
}
}

func sliceContains(slice []string, value string) bool {
Copy link
Member

Choose a reason for hiding this comment

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

I can't find where this function is used.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

I think it would be better to move to slice utils or so, so we can reuse it later

Copy link
Member

Choose a reason for hiding this comment

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

oops, my bad :(

Copy link
Member

Choose a reason for hiding this comment

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

it's needed to avoid duplications in the list of folders

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should I create a new package pkg/sliceutil and put it there?

Copy link
Member

Choose a reason for hiding this comment

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

yeah, it would be better if you move it to pkg/sliceutil

for _, v := range slice {
if v == value {
return true
}
}
return false
}

var errConfigDisabled = errors.New("config is disabled by --no-config")

func (r *FileReader) parseConfigOption() (string, error) {
Expand Down