goimports needs to ignore vendor package

5.4k views Asked by At

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/?

3

There are 3 answers

1
S. Diego On BEST ANSWER

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.

1
yee On

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
0
bilalcaliskan On

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