snorberhuis
snorberhuis

Reputation: 3276

goimports needs to ignore vendor package

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:

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

Upvotes: 7

Views: 5883

Answers (3)

bilalcaliskan
bilalcaliskan

Reputation: 31

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

Upvotes: 0

yee
yee

Reputation: 1985

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

Upvotes: 0

S. Diego
S. Diego

Reputation: 1026

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.

Upvotes: 10

Related Questions