goimports needs to ignore vendor package
Asked Answered
F

3

7

I am trying to implement dep in my project. This is all working well but it also adds a vendor directory. I now need to update my tooling to ignore this directory or my vendored packages will be modified or I get false positives of warnings. I am currently using the following tooling:

  • goimports -w
  • go vet
  • go lint

These tools are also used in CI. I do want to keep autoformatting using goimports, but I am willing to start using gometalinter. I am not really looking for a solution using grep and find magic.

How can I make these tools ignore vendor/?

Fulfil answered 28/11, 2017 at 10:45 Comment(0)
T
10

gometalinter has a "--vendor" flag to ignore the vendor folder. the flag passes the needed paramters to the underlying tools to ignore the folder.

so one solution would be to use only govet, golint und goimports with gometalinter

gometalinter --disable-all --enable=vet --enable=golint --enable=goimports --vendor ./...

another solution might be (copied from gist):

goimports -d $(find . -type f -name '*.go' -not -path "./vendor/*")

imho I would prefer the first solution. That way you could easily add other linters as needed.

Triangular answered 28/11, 2017 at 12:33 Comment(1)
I used both of your solutions combined. This allows me to automatically format for developers and later check this and other stuff in CI. Thanks!Fulfil
S
0

To exclude directories in your $GOPATH from being scanned for Go files, goimports respects a configuration file at $GOPATH/src/.goimportsignore which may contain blank lines, comment lines (beginning with '#'), or lines naming a directory relative to the configuration file to ignore when scanning. No globbing or regex patterns are allowed. Use the "-v" verbose flag to verify it's working and see what goimports is doing.

https://godoc.org/golang.org/x/tools/cmd/goimports

So you can add the vendor dir to $GOPATH/src/.goimportsignore file, e.g.:

github.com/foo/bar/vendor
Silvie answered 28/11, 2017 at 13:7 Comment(1)
Somehow this solution ignores the vendor package. I suspect this is due to the fact that the vendored code is used in the main source.Fulfil
V
0

yet another uggly but working solution is running it like that:

for item in `find . -type f -name '*.go' -not -path './vendor/*'`
do
    goimports -l -w $item
done

if you prefer single-liner:

for item in `find . -type f -name '*.go' -not -path './vendor/*'`; do goimports -l -w $item; done
Vishinsky answered 27/8, 2022 at 9:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.