Search code examples
goprotocol-buffersprotobuf-go

Go protobuff import issue


wanted to be able to read meshtastic protobuf packets. I used buf.buid to generate the SDK. Then I imported them to my mqtt.go file:

package main

import (
"encoding/json"
"fmt"
"io"
"log"
"os"
"os/signal"
"strings"
"sync"
"syscall"

mqttProto "buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go"
mqtt "github.com/eclipse/paho.mqtt.golang"
"google.golang.org/protobuf/proto"
)

However when I try to run with go run mqtt.go I obtain this error:

no required module provides package buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go; to add it: go get buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go

When I try to use go get buf.build/... I get an empty output and the problem remains.

I also tried to run go mod tidy and I got this error:

go: finding module for package buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go
go: router imports buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go: module buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go@latest found (v1.32.0-20240228025038-216ffc09a4e9.1), but does not contain package buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go

I also tried do run go clean -modcache and it didn't help. Any idea on how to fix the problem? thanks


Solution

  • You can have a look at what the go module actually contains by go getting it and then checking its content in the module cache. The module cache is located in $GOPATH/pkg/mod (on mac that defaults to ~/go/pkg/mod). From there you should be able to navigate to the module by following the folders of the import path.

    In this case it turns out there are no Go files in the module itself, just the go.mod file and a LICENSE. All Go files are in the sub-folder meshtastic.

    Meaning the import path should be:

    import mqttProto "buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go/meshtastic"
    

    Making that change should fix the issue.