Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

like image 432
snorberhuis Avatar asked Nov 28 '17 10:11

snorberhuis


People also ask

How to handle goimports?

In the case of goimports, the contract is given a valid Go file, return the same valid Go file formatted by gofmt, with exactly the imports needed for it to compile. In order to fulfill it, a possible solution could be: parse the file and find unresolved references (symbols not declared in that file),

How do I use goimports with Sublime?

Clone the repository to your plugins folder. Specify the path to the goimports binary in GoImports.sublime-settings. Add to your sublime keys the following command and whenever you want to run goimports hit F4. It will format/automatically adjust imports in the current go file.

How does goimports work with srcdir?

goimports will use srcDir to load all Go files in the directory of the file to fix, and parse all of them: if using the recommended Go architecture, this should yield all the package files.

What is goimports in Golang?

Command goimports updates your Go import lines, adding missing ones and removing unreferenced ones. $ go get golang.org/x/tools/cmd/goimports In addition to fixing imports, goimports also formats your code in the same style as gofmt so it can be used as a replacement for your editor's gofmt-on-save hook.


Video Answer


1 Answers

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.

like image 124
S. Diego Avatar answered Oct 21 '22 16:10

S. Diego