lightningnetwork / lnd

Lightning Network Daemon ⚡️
MIT License
7.67k stars 2.07k forks source link

Fail to import lnd/macaroons pkg in my own app #2934

Closed xsb closed 5 years ago

xsb commented 5 years ago

Background

I am trying to use lnd/macaroons in my Go program but I fail at it.

Your environment

Steps to reproduce

Here I provide a simple example in which I can reproduce the issue but notice that the application is actually bigger and uses many more packages.

package main

import (
    "io/ioutil"
    "log"

    "github.com/lightningnetwork/lnd/macaroons"
    "google.golang.org/grpc/credentials"
    macaroon "gopkg.in/macaroon.v2"
)

func main() {
    grpcCredential, err := credentials.NewClientTLSFromFile("/path/to/tls.cert", "")
    if err != nil {
        log.Fatal(err)
    }

    providedMacaroon, err := ioutil.ReadFile("/path/to/admin.macaroon")
    if err != nil {
        log.Fatal(err)
    }
    mac := &macaroon.Macaroon{}
    if err = mac.UnmarshalBinary(providedMacaroon); err != nil {
        log.Fatal(err)
    }
    // not doing anything because this is just an example
    _ = macaroons.NewMacaroonCredential(mac)
}

When getting the dependencies I see this error.

$ go get ./...
(...)
go: finding github.com/lightningnetwork/lnd/macaroons latest
build playg: cannot load github.com/lightningnetwork/lnd/macaroons: cannot find module providing package github.com/lightningnetwork/lnd/macaroons

I tried clearing modules cache with go clean -modcache but it did not help.

$ go get ./...
# github.com/lightningnetwork/lnd/macaroons
../github.com/lightningnetwork/lnd/macaroons/service.go:9:2: imported and not used: "github.com/coreos/bbolt" as bolt
../github.com/lightningnetwork/lnd/macaroons/store.go:10:2: imported and not used: "github.com/coreos/bbolt" as bolt
../github.com/lightningnetwork/lnd/macaroons/store.go:49:2: undefined: bbolt

The error says that the import was aliased to bolt so I checked and that's not the case in my filesystem:

$ head $GOPATH/src/github.com/lightningnetwork/lnd/macaroons/store.go | tail -n 1
    "github.com/coreos/bbolt"

Expected behaviour

I can import lnd packages in my project.

Actual behaviour

I fail to import lnd packages in my project.

wpaulino commented 5 years ago

When using Go modules, you should be able to just run

go mod -require=github.com/lightningnetwork/lnd@version

within your project's directory and run go build after.

xsb commented 5 years ago

That fixed it, thanks @wpaulino