6

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

snorberhuis
  • 3,108
  • 2
  • 22
  • 29

3 Answers3

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.

S. Diego
  • 876
  • 6
  • 6
  • 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! – snorberhuis Dec 01 '17 at 15:56
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
yee
  • 1,975
  • 1
  • 15
  • 14
  • 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. – snorberhuis Nov 29 '17 at 08:50
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