How to ignore a directory or a file while running goimports?

339 Views Asked by At

I have the following project structure

proejct/
├── README.md
├── go.mod
├── go.sum
├── src
│   ├── delivery
│   │   ├── grpc
│   │   │   ├── index.go
│   │   │   ├── pb
│   │   │   │   ├── project.pb.go
│   │   │   │   └── another.pb.go
│   │   │   └── proto
│   │   │       ├── project.proto
│   │   │       └── another.proto
│   │   └── rest/
│   ├── main.go
│   └── .goimportsignore
└── .git/

I use goimports to manage the import lines in my source code. I want it to ignore the generated proto files (*.pb.go).

The documentation says that

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.

So I set GOPATH to the root of the project in my terminal: export GOPATH=$PWD (yes, the PWD was correct). And added a .goimportsignore file in src/ with the following contents:

delivery/grpc/pb

Now from the root of the project, I run goimports -l -v ./. No effect, I can still see .pb.go files in the list. I try goimports -l -v src/ and still the same output. I tried moving the .goimportsignore file to project root and running the above commands. Again, I get the same output.

At this point, I am clueless to what I might be doing wrong. So, please help.

0

There are 0 best solutions below