Golang intellisence issue for protobuf auto-generated go packages in a nakama-based solution using VSCode

Hi there. First of all, I have to say that I am using the word “intellisence” to refer to code-completion, linting, code formatting, and stuff like that. I am using the setup env provided by @novabyte in this gist, with a small difference that I am using version 2.14.1 of Nakama and Nakama-pluginbuilder, and Nakama-common v1.9.0 due to a bug in matchmaking I encountered during upgrade from Nakama-common 1.4.0 to 1.8.0.
I am using vscode-insiders 1.51.0 and the official Go plugin v0.18.1.
we have a monolithic repository consisting of seperate directories for our unity client, backend, and protobuffers which are shared between the other two.

davaa/
├── Backend
├── Client
└── ProtoBuffers
// ./Backend/go.mod

module gitlab.com/mahdad-baghani/davaa

go 1.14

require (
	github.com/golang/protobuf v1.4.3
	github.com/heroiclabs/nakama-common v1.9.0
	google.golang.org/protobuf v1.25.0
)

here is one simple protobuf I defined:

// ./ProtoBuffers/netcode_message_buffers.proto
syntax = "proto3";
package netcode;

option go_package = "gitlab.com/mahdad-baghani/davaa/protobuffers;netcode_buffers";
option csharp_namespace="Davaa.ProtocolBuffers";

message MatchResultBuf {
    string winner = 1;
    string loser = 2;
    string sender = 3;
}

I compile protobufs to specific folders. In the case of my backend, its ./Backend/protobuffers/. Up to now, everything is fine when it comes to “intellisence”. I have code completion, function signature tooltips, linter suggestions, error squigglies, etc. When I try to start my backend using docker-compose -f my_yml.yml up --build nakama, I get the following error:
match/match.go:10:2: cannot find package "." in: /backend/protobuffers
In line 10, I am importing my compiled protobuf package as following :

netcode_buffers "gitlab.com/mahdad-baghani/davaa/protobuffers"

I suspect that it has to do with my this specific line in my Dockerfile:

WORKDIR /backend

and

COPY --from=builder /backend/backend.so /nakama/data/modules

as the error indicates that the package was not found in /backend/protobuffers.

I can compile by changing the go_package option in my protobuf definition by adding the /Bakcend/ so it actually points to the physical file that exists on disk

option go_package = "gitlab.com/mahdad-baghani/davaa/Backend/protobuffers;netcode_buffers";

Now I can compile regardless of VS Code showing just one error: cannot find package "gitlab.com/mahdad-baghani/davaa/protobuffers" in any of .... But, now, I cannot have intellisence if I want to use my type MatchResultBuf, or any other auto-generated types from protobuffer definitions for that matter.

What am I doing wrong?

@Mahdad-Baghani At a glance it looks like you might be misusing some paths.

match/match.go:10:2: cannot find package “.” in: /backend/protobuffers

This one usually means there are no Go files in a given path. Make sure whenever you run docker-compose up you do so from the root of your backend code, not from within your protobuf package.

cannot find package “gitlab.com/mahdad-baghani/davaa/protobuffers” in any of …

You’re trying to import gitlab.com/mahdad-baghani/davaa/protobuffers but you’ve defined your proto output package as gitlab.com/mahdad-baghani/davaa/Backend/protobuffers. There’s a missing/extra “Backend” in there.

Start by making sure your go.mod's module directive is "gitlab.com/mahdad-baghani/davaa", your proto files are in Backend/protobuffers, your proto go_package directive is "gitlab.com/mahdad-baghani/davaa/protobuffers", and any imports for your proto stubs are gitlab.com/mahdad-baghani/davaa/protobuffers. Also make sure you clean up any running containers and delete existing images before you rebuild, just in case.

I’d also recommend avoiding mixed-case package names and/or directories where possible.

Thanks for the reply.

I compile my .proto file from ./ProtoBuffers/ in two different locations, one of which is ./Backend/protobuffers. So this is the part I suspected it is related to my dockerfile. Because backend/ is defined as the working directory in dockerfile.

That’s what I do.

As I mentioned, I started with the corrected version of the path that you mentioned here. The thing was I had intellisence but also had compile errors. When I added the extra /Backend/ in between my path, neither I had intellisence, nor compile errors.

Here is how I managed to solve the problem in case anyone is curious.

First, I changed my module name to not be mixed case anymore as @zyro suggested. The name was mixed-case because my gitlab account was named that way, but I also changed that, so my module name actually points to a valid remote git repo.

Second, I changed my module name from gitlab.com/name/reponame to gitlab.com/name/reponame/backend so it respects the hierarchy I had in my monolithic repository. My backend was in backend directory, and I named my module that way.

Third, I change all my import paths to include the /backend subdirectory. In the case of my auto-generated protobuf stubs, I changed the import path from
netcode_buffers "gitlab.com/mahdad-baghani/davaa/protobuffers"
to
netcode_buffers "gitlab.com/newname/davaa/backend/protobuffers"
and I changed the package name of the protobuf definition to match this name exactly.

1 Like