Skip to content

Conversation

@dixonwille
Copy link

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.

@golangci
Copy link
Collaborator

@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
Copy link
Collaborator

Choose a reason for hiding this comment

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

What does it mean?

Copy link
Author

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?

Copy link
Collaborator

@golangci golangci May 31, 2018

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.

Copy link
Author

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
Copy link
Collaborator

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

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),
Copy link
Collaborator

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?

Copy link
Collaborator

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

Copy link
Author

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?

Copy link
Collaborator

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.

for _, i := range issues {
res = append(res, result.Issue{
Pos: i.Position,
Text: fmt.Sprintf("%s %s", i.PackageName, msgSuffix),
Copy link
Collaborator

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

@dixonwille
Copy link
Author

I'll address these tonight. At work currently.

@@ -0,0 +1,58 @@
package golinters
Copy link
Collaborator

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

Copy link
Author

@dixonwille dixonwille Jun 1, 2018

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?

Copy link
Author

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...

@dixonwille
Copy link
Author

Having a few issues with fixing the test for depguard. I'll push it up when I get it all fixed

@dixonwille
Copy link
Author

So manually testing what I added works. But for some reason when ran against the testdata, it seems that the loader.Program does not have an initial set of packages. So depguard falls through as successful...

@dixonwille
Copy link
Author

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

@dixonwille
Copy link
Author

@golangci So I did get everything working they way you would like! Just to document my issues

  • Some package managers do not install the $(GOROOT)/test directory
    • Solution was to download the precompiled from golang.org and extract out the go/test/errchk file and move it to my $GOROOT
  • My default values for depguard are very permissive (allows everything) but it also doesn't make sense to have flags for it either as the values will not change often.
    • Solution add hidden pflags in the run command so that in the test I could set them to what the test expects
  • There was an issue with my linter *loader.Program.Imported doesn't include what I would have expected.
    • Solution You will almost always want to use *loader.Program.InitialPackages(). I am not sure exactly what the difference is (something around created and imported versus just imported).

@golangci
Copy link
Collaborator

golangci commented Jun 1, 2018

thank you very much! and thank you for documenting all issues, it will help us.

  1. errchck - I will rewrite it to Go to not depend on side perl script.
  2. it's awful, I will think about refactoring config and options parsing.
  3. yes, all linters must use loader.Program.InitialPackages(). It's not obvious and should be refactored too.

@golangci golangci merged commit 2f333be into golangci:master Jun 1, 2018
@dixonwille dixonwille deleted the feature/add-depguard branch June 1, 2018 21:04
@golangci
Copy link
Collaborator

golangci commented Jun 2, 2018

I've refactored code to simplify its changing and made automatic generation of README.

@ldez ldez added this to the v1.4 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.

3 participants