Skip to content

Commit 13c2a34

Browse files
Add home directory to config file search paths (#1325)
1 parent a35fd6e commit 13c2a34

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

docs/src/docs/usage/configuration.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ GolangCI-Lint looks for config files in the following paths from the current wor
2828
- `.golangci.json`
2929

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

3334
Config options inside the file are identical to command-line options.

pkg/config/reader.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ func (r *FileReader) setupConfigFileSearch() {
170170

171171
// find all dirs from it up to the root
172172
configSearchPaths := []string{"./"}
173+
173174
for {
174175
configSearchPaths = append(configSearchPaths, curDir)
175176
newCurDir := filepath.Dir(curDir)
@@ -179,13 +180,29 @@ func (r *FileReader) setupConfigFileSearch() {
179180
curDir = newCurDir
180181
}
181182

183+
// find home directory for global config
184+
if home, err := homedir.Dir(); err != nil {
185+
r.log.Warnf("Can't get user's home directory: %s", err.Error())
186+
} else if !sliceContains(configSearchPaths, home) {
187+
configSearchPaths = append(configSearchPaths, home)
188+
}
189+
182190
r.log.Infof("Config search paths: %s", configSearchPaths)
183191
viper.SetConfigName(".golangci")
184192
for _, p := range configSearchPaths {
185193
viper.AddConfigPath(p)
186194
}
187195
}
188196

197+
func sliceContains(slice []string, value string) bool {
198+
for _, v := range slice {
199+
if v == value {
200+
return true
201+
}
202+
}
203+
return false
204+
}
205+
189206
var errConfigDisabled = errors.New("config is disabled by --no-config")
190207

191208
func (r *FileReader) parseConfigOption() (string, error) {

0 commit comments

Comments
 (0)