Go protobuff import issue

43 Views Asked by At

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

2

There are 2 best solutions below

1
TehSphinX On BEST ANSWER

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.

0
Liber On

example

    package main
    
    import (
        "fmt"
    
        "buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go/meshtastic"
        mqtt "github.com/eclipse/paho.mqtt.golang"
        "google.golang.org/protobuf/proto"
    )
    
    func main() {
        _ = proto.Int32(1)
        _ = mqtt.DummyToken{}
        _ = meshtastic.QueueStatus{}
        _ = meshtastic.AdminMessage{}
        fmt.Println("liber test")
    }
  cat go.mod
    module proto


    go 1.21.6
    
    require (
        buf.build/gen/go/meshtastic/protobufs/protocolbuffers/go v1.28.1-20240228025038-216ffc09a4e9.4
        github.com/eclipse/paho.mqtt.golang v1.4.3
        google.golang.org/protobuf v1.32.0
    )
    
    require (
        github.com/google/go-cmp v0.5.9 // indirect
        github.com/gorilla/websocket v1.5.0 // indirect
        golang.org/x/net v0.8.0 // indirect
        golang.org/x/sync v0.1.0 // indirect
    )

enter image description here