-
-
Couldn't load subscription status.
- Fork 1.5k
Add Depguard to supported linters #47
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
Conversation
|
@dixonwille thank you very much! I understand how difficult it was without any documentation. I made wiki page about adding a new linter, feel free to contribute if you found anything incomprehensible. |
.golangci.yml
Outdated
| min-len: 2 | ||
| min-occurrences: 2 | ||
| depguard: | ||
| list-type: blacklist |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does it mean?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So depguard is based off a whitelist or a blacklist. If nothing is passed it, it defaults to a whitelist with your current packages in it. Since this project has many imports, and empty blacklist will suffice (since it will allow everything). I could disable the linter if you would prefer?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some users run golangci-lint like we do: golangci-lint run --enable-all, and it shouldn't blacklist anything if not explicitly configured. Else it breaks linting. Remove this configuration please and ensure that we don't need to disable linter explicitly after that. Also, linter should do nothing if it finds that nothing was configured.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So change the default to an empty blacklist if no configuration is passed?
Not sure I understand the reasoning behind not running a linter if it is enabled but not configured. Should it not have a default configuration (empty blacklist which allows everything)? Which brings up a good point I can short cut the code in that case.
| - [goimports](https://godoc.org/golang.org/x/tools/cmd/goimports): Goimports does everything that gofmt does. Additionally it checks unused imports | ||
| - [maligned](https://github.com/mdempsky/maligned): Tool to detect Go structs that would take less memory if their fields were sorted | ||
| - [megacheck](https://github.com/dominikh/go-tools/tree/master/cmd/megacheck): 3 sub-linters in one: unused, gosimple and staticcheck | ||
| - [depguard](https://github.com/OpenPeeDeeP/depguard): Go linter that checks if package imports are in a list of acceptable packages |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add it to thanks section please
pkg/enabled_linters.go
Outdated
| newLinterConfig(golinters.Maligned{}).WithFullImport().WithPresets(PresetPerformance).WithSpeed(10), | ||
| newLinterConfig(golinters.Megacheck{GosimpleEnabled: true, UnusedEnabled: true, StaticcheckEnabled: true}). | ||
| WithSSA().WithPresets(PresetStyle, PresetBugs, PresetUnused).WithSpeed(1), | ||
| newLinterConfig(golinters.Depguard{}).WithPresets(PresetAdmin).WithSpeed(6), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not add it to style preset?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WithFullImport() is needed: without that it won't load loader.Program if only this linter is enabled
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't really think it fit the style prefix but I will gladly move it if you think that is more proper. I was thinking it from a repository maintainer administrating what imports are allowed. Let me know what you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's a kind of style guide: what allowed and what not.
pkg/golinters/depguard.go
Outdated
| for _, i := range issues { | ||
| res = append(res, result.Issue{ | ||
| Pos: i.Position, | ||
| Text: fmt.Sprintf("%s %s", i.PackageName, msgSuffix), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use formatCode for i.PackageName please: it will be nice-formatted in GitHub comments
|
I'll address these tonight. At work currently. |
| @@ -0,0 +1,58 @@ | |||
| package golinters | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add a test file to pkg/testdata please
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can't run test locally... https://github.com/golangci/golangci-lint/blob/master/pkg/enabled_linters_test.go#L93 GOROOT does not have a test directory for me. Am I missing something?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found it. I use Fedora and the golang package that I installed strips out those files...
|
Having a few issues with fixing the test for depguard. I'll push it up when I get it all fixed |
|
So manually testing what I added works. But for some reason when ran against the |
|
I was also seeing something kind of strange. Seems like the list I was passing in was getting duplicated. Will look more into this later as it does not cause an issue for the linter itself. But it is more memory then it is needed |
|
@golangci So I did get everything working they way you would like! Just to document my issues
|
|
thank you very much! and thank you for documenting all issues, it will help us.
|
|
I've refactored code to simplify its changing and made automatic generation of README. |
So I created this linter that is very helpful for administrating a repository. It was created because I help maintain a few go repositories and I would prefer the CI to pickup imports I don't like then have to go through and find them myself.
https://github.com/OpenPeeDeeP/depguard
I was not sure what all needed to change (tests and docs), but I did get it running on my machine and found a few places in the Readme.
Feedback on the linter itself is always welcome as well.