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

Add home directory to config file search paths #1325

merged 4 commits into from
Aug 24, 2020

Conversation

System-Glitch
Copy link
Contributor

See #1324

Guidance required: tests don't pass if you have a configuration file in your home directory. This is because some tests check that config is not found. I need assistance on how to update tests properly.

@boring-cyborg
Copy link

boring-cyborg bot commented Aug 21, 2020

Hey, thank you for opening your first Pull Request !

@CLAassistant
Copy link

CLAassistant commented Aug 21, 2020

CLA assistant check
All committers have signed the CLA.

@SVilgelm
Copy link
Member

SVilgelm commented Aug 21, 2020

So, dumb question, but I must ask it: have you test it on your env? Since we cannot write the unit test for this. And I cannot test it on my laptop, I don't have external adds or usb sticks

@System-Glitch
Copy link
Contributor Author

System-Glitch commented Aug 21, 2020

I have tested it but have not written automated tests for it.

From another drive (/mnt/6C161AE6161AB156), I ran it and got

$ ./golangci-lint run -v
INFO [config_reader] Config search paths: [./ /mnt/6C161AE6161AB156 /mnt / /home/jeremy] 
INFO [config_reader] Used config file ../../home/jeremy/.golangci.yml 
INFO [lintersdb] Active 14 linters: [deadcode errcheck gocyclo gofmt golint gosimple govet ineffassign misspell staticcheck structcheck typecheck unused varcheck] 
INFO [loader] Go packages loading at mode 575 (compiled_files|imports|types_sizes|deps|exports_file|files|name) took 446.118075ms 
ERRO Running error: context loading failed: no go files to analyze 
INFO Memory: 6 samples, avg is 72.8MB, max is 72.8MB 
INFO Execution took 449.689682ms

With v1.30.0:

$ golangci-lint run -v
INFO [config_reader] Config search paths: [./ /mnt/6C161AE6161AB156 /mnt /] 
INFO [lintersdb] Active 10 linters: [deadcode errcheck gosimple govet ineffassign staticcheck structcheck typecheck unused varcheck] 
INFO [loader] Go packages loading at mode 575 (imports|compiled_files|exports_file|files|name|types_sizes|deps) took 580.5053ms 
ERRO Running error: context loading failed: no go files to analyze 
INFO Memory: 7 samples, avg is 72.1MB, max is 72.1MB 
INFO Execution took 583.579037ms

/home/jeremy/.golanci.yml:

run:
  skip-dirs:
    - docs_src
    - docs
    - .github

linters-settings:
  gocyclo:
    min-complexity: 15
  gofmt:
    simplify: true
  misspell:
    locale: US

linters:
  enable:
    - gofmt
    - golint
    - gocyclo
    - misspell
  disable-all: false
  fast: false

issues:
  exclude-use-default: false

You can notice the additional linters I enabled are correctly enabled.

@SVilgelm
Copy link
Member

Cool, thanks

@System-Glitch
Copy link
Contributor Author

So, we leave it like that and write no tests?

@System-Glitch System-Glitch changed the title WIP: Add home directory to config file search paths Add home directory to config file search paths Aug 21, 2020
@bombsimon
Copy link
Member

Another question just to be sure since I haven't dug into the code, is the search order like this now so /home doesn't come after /mnt/path/go but before /mnt/path? Because I think that could be breaking for some users.

Given code in /mnt/path/go

/mnt/path/go
/mnt/path
/mnt
/
/home

@SVilgelm
Copy link
Member

We can write the test, but it will require some refactoring. viper.SetConfigName(".golangci") must be changed and the ".golangci" must be moved to a variable, so it's not a constant. Then unit tests can change that variable and generate the config files and put them to the all possible folders and test that the expected file is used.

@SVilgelm
Copy link
Member

it will work in this order:

/mnt/path/go
/mnt/path
/mnt
/
/home

the /home is the latest folder

@System-Glitch
Copy link
Contributor Author

System-Glitch commented Aug 21, 2020

@bombsimon The home directory is added last, so it should be checked last.
See my comment above:

INFO [config_reader] Config search paths: [./ /mnt/6C161AE6161AB156 /mnt / /home/jeremy] 

However, there is a possibility for duplicate paths (effectively checking the same path twice). I can add a check to prevent this if needed.

Proof:

$ ./golangci-lint run -v
INFO [config_reader] Config search paths: [./ /home/jeremy/Projects/go/randomproject /home/jeremy/Projects/go /home/jeremy/Projects /home/jeremy /home / /home/jeremy] 

@SVilgelm I am still a bit lost on how I could write those tests. Maybe it can be merged and tests added later by the core team?

@System-Glitch
Copy link
Contributor Author

System-Glitch commented Aug 21, 2020

Duplicate paths fixed:

$ ./golangci-lint run -v
INFO [config_reader] Config search paths: [./ /home/jeremy/Projects/go/randomproject /home/jeremy/Projects/go /home/jeremy/Projects /home/jeremy /home /]

Copy link
Member

@SVilgelm SVilgelm left a comment

Choose a reason for hiding this comment

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

Looks good to me. Thank you

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

Copy link
Member

@sayboras sayboras left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@bombsimon bombsimon left a comment

Choose a reason for hiding this comment

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

LGTM

@sayboras sayboras merged commit 13c2a34 into golangci:master Aug 24, 2020
@golangci-automator
Copy link

Hey, @System-Glitch — we just merged your PR to golangci-lint! 🔥🚀

golangci-lint is built by awesome people like you. Let us say “thanks”: we just invited you to join the GolangCI organization on GitHub.
This will add you to our team of maintainers. Accept the invite by visiting this link.

By joining the team, you’ll be able to label issues, review pull requests, and merge approved pull requests.
More information about contributing is here.

Thanks again!

@System-Glitch System-Glitch deleted the home-global-config branch August 24, 2020 08:29
@ldez ldez added this to the v1.31 milestone Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants