1

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.

mrpandey
  • 627
  • 2
  • 9
  • 17
  • 1
    You shouldn’t be using GOPATH any longer, the goimports will use the current module. – JimB Apr 30 '23 at 18:56
  • @JimB I was just following the documentation. Where should I place the ignore file in my project with go modules? – mrpandey May 01 '23 at 01:50
  • 1
    `goimports` is currently not working with Go Modules. How to tackle this (and bigger design concerns) is being discussed [here](https://github.com/golang/go/issues/42965). – mrpandey May 01 '23 at 11:20

0 Answers0