0

I am following a guide on creating and using protocol buffers and gRPC for Golang. I have got up to the point where I have the generated Go files in an output directory, but I am faced with a few issues:

The imports in all 3 files start like this:

// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
//  protoc-gen-go v1.25.0
//  protoc        v3.15.5
// source: building.proto

package location

import (
    proto "github.com/golang/protobuf/proto"
    protoreflect "google.golang.org/protobuf/reflect/protoreflect"
    protoimpl "google.golang.org/protobuf/runtime/protoimpl"
    reflect "reflect"
    sync "sync"
)

Now when I try to build or run my main.go file, which just imports the buffers and generates a message with them (not that it even gets past the import stage), the following happens:

..\proto\out\loc\location\building.pb.go:10:2: cannot find package
..\proto\out\loc\location\building.pb.go:11:2: cannot find package
..\proto\out\loc\location\building.pb.go:12:2: cannot find package

Those lines are referring to the first 3 imports in the previous code snippet.

I can't seem to figure out why this is happening. Below I will describe the steps I have taken to try to resolve this:

  1. Reinstall protoc and the protoc-gen-go compiler (multiple times in both local user dirs and system dirs)
  2. Since I'm on Windows, I tried several options for generating the Protobuffers and building/running the go source code: Using Goland, CMD, Cygwin (MinGW), Powershell.
  3. I have tried editing the first import to use google.golang.org/protobuf/proto rather than the github import as it is deprecated from what I read online.
  4. I did a go get google.golang.org/protobuf/xxxx for each missing import. They went into $GOROOT/pkg/mod though instead of $GOROOT/src and I couldn't reach them, so I manually moved them to src. I then edited the auto-generated .go files from protoc to look like:
    proto "protobuf/proto"
    protoreflect "protobuf/reflect/protoreflect"
    protoimpl "protobuf/runtime/protoimpl"
    
    And this actually worked... although ALL imports in those files are also reading from google.golang.org so... I now have like 50 import errors... Plus I don't really want to have to edit generated protobuf files every time I rebuild them.
  5. I tried resolving dependencies using go mod init protobuf_example in my root dir and then I tried go mod tidy which looked like it did "stuff" (here's a snippet):
    go: found google.golang.org/protobuf/encoding/protowire in google.golang.org/protobuf v1.25.0
    go: found google.golang.org/protobuf/internal/encoding/messageset in google.golang.org/protobuf v1.25.0
    go: found google.golang.org/protobuf/internal/errors in google.golang.org/protobuf v1.25.0
    go: found google.golang.org/protobuf/internal/fieldsort in google.golang.org/protobuf v1.25.0
    go: found google.golang.org/protobuf/internal/flags in google.golang.org/protobuf v1.25.0
    go: found google.golang.org/protobuf/internal/genid in google.golang.org/protobuf v1.25.0
    

But in the end it didn't solve anything. I now have a go.mod file which contains require(*repos*) and when I hover over them in Goland it just says 'unresolved dependency'

I'm quite new to Go and its arcane package management system. Coming from Python and Pip, this is quite a headache.

Edit

As requested, I'm adding some extra info:

My go.mod is:

module protobuf_prac

go 1.16

require (
        github.com/golang/protobuf v1.4.3
        github.com/google/go-cmp v0.5.5 // indirect
        google.golang.org/protobuf v1.25.0
)

I changed my imports to be non-relative and to use the correct module as such:

package main
import (
        "fmt"
        "protobuf_prac/proto/out/loc/location"
)

func main() {
        city := location.City{Name: "Sofia", ZipCode: "1000", CountryName: "Bulgaria"}
        fmt.Println(city.String())
}

The issue persists.

halfer
  • 19,824
  • 17
  • 99
  • 186
zkscpqm
  • 69
  • 1
  • 5
  • 1
    You **must** use modules so always start with `go mod init`. Read How to Write Go Code and stick to it. All this looks not like a "dependency problem" but a simply a wrong setup. You did not show you go.mod, you did not show how you import the generated stuff. Manually go geting stuff is unneeded and moving such code around is totally wrong. Note that Go's dependency management is much simpler than pip. Do not follow random tutorials but stick to https://golang.org/doc/#getting-started. Really. – Volker Mar 09 '21 at 19:53
  • 1
    One more: an error like `..\proto\out\loc\location\building.pb.go:10:2:` indicates that you use relative imports: Don't do that even if some tutorial tells you. – Volker Mar 09 '21 at 20:36
  • I have added the extra info to my post, thanks for the tips. Still having the same problem :( – zkscpqm Mar 09 '21 at 20:54
  • 2
    Post the full output of `go build -v` run in the folder where main.go is. – Volker Mar 09 '21 at 21:01
  • 3
    Pay attention to your GoLand settings: `Preferences / Settings | Go | Go Modules` should be enabled and `Preferences / Settings | Go | GOPATH | Index entire GOPATH` disabled. – s0xzwasd Mar 10 '21 at 12:37

1 Answers1

1

As mentioned by @s0xzwasd the issue was disabled Go modules in Goland.

zkscpqm
  • 69
  • 1
  • 5