perlin-network / noise

A decentralized P2P networking stack written in Go.
https://godoc.org/github.com/perlin-network/noise
MIT License
1.79k stars 211 forks source link

Error importing packages from project #189

Closed gh0st42 closed 4 years ago

gh0st42 commented 5 years ago

Cloning this repo, calling go mod vendor and then go run examples/chat/main.go works fine.

The problems begin when I try to use noise from my own projects. I did the usual go mod init etc with a simple test source:

package main

import (
    "github.com/perlin-network/noise/crypto/ed25519"
    "github.com/perlin-network/noise/log"
)

func main() {
    keys := ed25519.RandomKeyPair()
    println(keys)
    // Print out loaded public/private keys.
    log.Info().
        Str("private_key", keys.PrivateKeyHex()).
        Msg("")
    log.Info().
        Str("public_key", keys.PublicKeyHex()).
        Msg("")
}

Calling this with go run main.go results in the following error:

go: finding github.com/perlin-network/noise/log latest
go: finding github.com/perlin-network/noise/crypto/ed25519 latest
go: finding github.com/perlin-network/noise/crypto latest
main.go:5:2: unknown import path "github.com/perlin-network/noise/log": cannot find module providing package github.com/perlin-network/noise/log

If I remove the logging function and only use the crypto stuff it works. If I try the same with the chat example from the noise repo but copied to my project I get the same error and also an error for the opcodes package.

Since I am rather new to go modules, I am probably doing something really stupid. It's just extra confusing that the crypto package works..

My go version go version go1.11.4 linux/amd64

JekaMas commented 5 years ago

@gh0st42 Could you help me recheck the issue? I've tried to reproduce it and haven't managed in it.

My code

package main

import (
    "fmt"

    "github.com/perlin-network/noise/crypto/ed25519"
    "github.com/perlin-network/noise/log"
)

func main() {
    keys := ed25519.RandomKeyPair()
    fmt.Println(keys.String())
    // Print out loaded public/private keys.
    log.Info().
        Str("private_key", keys.PrivateKeyHex()).
        Msg("")
    log.Info().
        Str("public_key", keys.PublicKeyHex()).
        Msg("")
}

Commands to run the code:

export GO111MODULE=on
go mod vendor
dep init
go run main.go 

Output:

go run main.go 
319ee8fe9e1c40ba54fdc2d90dd67e20d16b0e9fd0272ae7a9171c4c47906bbebcc35480f07aa3ff3efbae4ac3dcd1f85c2b035d171ed0cfd268d5fcfd5dc559 bcc35480f07aa3ff3efbae4ac3dcd1f85c2b035d171ed0cfd268d5fcfd5dc559
6:59PM INF %!s(<nil>) private_key=319ee8fe9e1c40ba54fdc2d90dd67e20d16b0e9fd0272ae7a9171c4c47906bbebcc35480f07aa3ff3efbae4ac3dcd1f85c2b035d171ed0cfd268d5fcfd5dc559
6:59PM INF %!s(<nil>) public_key=bcc35480f07aa3ff3efbae4ac3dcd1f85c2b035d171ed0cfd268d5fcfd5dc559

Go version go version go1.11.4 linux/amd64

Could you recheck and if the issue reproduced comment here all the steps how you start the code?

gh0st42 commented 5 years ago

Okay, I tried to follow your steps exactly:

/tmp $ mkdir testgo
/tmp $ cd testgo/
/tmp/testgo $ cat <<EOF > main.go
> package main
>
> import (
>     "fmt"
>
>     "github.com/perlin-network/noise/crypto/ed25519"
>     "github.com/perlin-network/noise/log"
> )
>
> func main() {
>     keys := ed25519.RandomKeyPair()
>     fmt.Println(keys.String())
>     // Print out loaded public/private keys.
>     log.Info().
>         Str("private_key", keys.PrivateKeyHex()).
>         Msg("")
>     log.Info().
>         Str("public_key", keys.PublicKeyHex()).
>         Msg("")
> }
> EOF
/tmp/testgo $ export GO111MODULE=on
/tmp/testgo $ go mod vendor
go: cannot find main module; see 'go help modules'
/tmp/testgo $

If I follow the official modules quick start guide (https://github.com/golang/go/wiki/Modules#quick-start) I get the following:

/tmp/testgo $ go mod init github.com/gh0st42/testgo
go: creating new go.mod: module github.com/gh0st42/testgo
/tmp/testgo $ go build
go: finding github.com/perlin-network/noise/crypto/ed25519 latest
go: finding github.com/perlin-network/noise/log latest
go: finding github.com/perlin-network/noise/crypto latest
main.go:7:5: unknown import path "github.com/perlin-network/noise/log": cannot find module providing package github.com/perlin-network/noise/log

My go version: go version go1.11.5 darwin/amd64 but also tried this with 1.11.4.

Could it be that you are still within your GOPATH? If I invoke dep I get the following:

$ dep init
init failed: unable to detect the containing GOPATH: both /tmp/testgo and /private/tmp/testgo are not within any known GOPATH

As of my understanding for current go modules support dep should not be needed anymore and the "official go way" is the one documented in https://github.com/golang/go/wiki/Modules or am I mistaken?

iwasaki-kenta commented 5 years ago

Care to try out the new setup instructions in the README? We just released out Noise v2.0.

gh0st42 commented 5 years ago

The first instructions fail, the second ones of course work as they did before since the example are within the project itself. Also if I check go.mod it contains the following contents:

module bla

require github.com/perlin-network/noise v1.1.0 // indirect

The version is v1.1.0 NOT v2.. If I specifically request v2 I get an error indicated that no matching version was found. I used many other modules successfully, also with different versions, never had problems like this. Usually should just import the modules and use them in their source code, go mod/go build should take care of the rest.

Copying the benchmark example to a new directory blub, calling go mod init blub and then go build:

go build
go: finding github.com/perlin-network/noise/protocol latest
go: finding github.com/perlin-network/noise/cipher/aead latest
go: finding github.com/perlin-network/noise/payload latest
go: finding github.com/perlin-network/noise/log latest
go: finding github.com/perlin-network/noise/handshake/ecdh latest
go: finding github.com/perlin-network/noise/skademlia latest
go: finding github.com/perlin-network/noise/cipher latest
go: finding github.com/perlin-network/noise/handshake latest
build blub: cannot load github.com/perlin-network/noise/cipher/aead: cannot find module providing package github.com/perlin-network/noise/cipher/aead

Oh and I also just tried it on brand new go 1.12, same error there.

Are there any other public projects/repos using noise as a lib?

instantaphex commented 5 years ago

I'm having the same problem but with the cipher package

build command-line-arguments: cannot load github.com/perlin-network/noise/cipher/aead: cannot find module providing package github.com/perlin-network/noise/cipher/aead

I'm on MacOS 10.14.2 and go 1.12

If I create a package in my GOPATH and turn off modules, I am able to run it just fine, but not in a module

masonforest commented 5 years ago

I've made some progress on this but still haven't fully figured out what's going on. I'm also on MacOS 10.13.6 and go 1.12.

@gh0st42:

It looks like Perlin 2.0.0 no longer contains the "crypto/ed25519" module. I imagine that's why it's using Perlin 1.1.0 which doesn't have a the log module. I did however get this to build:

// main.go
package main
import (
    "github.com/perlin-network/noise/log"
)

func main() {
    log.Info().Msg("ok")
}

With the following go.mod

module "https://github.com/masonforest/noise"

go 1.12

require github.com/perlin-network/noise v0.0.0-20190219190757-3c13535b725d
$ go run main.go
12:54PM INF usr/local/Cellar/go/1.12.1/libexec/src/runtime/proc.go:200 > ok

@instantaphex:

I had a similar issue when compiling the chat example.

To fix that I edited go.mod and changed

github.com/perlin-network/noise v1.1.0 // indirect

to

github.com/perlin-network/noise v2.0.0 // indirect

I still haven't figured out why it's selecting noise v1.1.0 by default though 🤔

Excited for the dust to settle around go modules. It seems like they are moving in the right direction!

mintzhao commented 4 years ago

require github.com/perlin-network/noise: version "v2.0.0" invalid: module contains a go.mod file, so major version must be compatible: should be v0 or v1, not v2

dhjw commented 4 years ago

Cloning this repo, calling go mod vendor and then go run examples/chat/main.go works fine.

This is the only way I'm able to run the chat example. If I move main.go to the folder above the repo or anywhere else and run it I get:

build command-line-arguments: cannot load github.com/perlin-network/noise/cipher/aead: module github.com/perlin-network/noise@latest found (v1.1.0), but does not contain package github.com/perlin-network/noise/cipher/aead

I tried all the recommended setup commands and more but nothing fixes it. Go 1.13.

cbertinato commented 4 years ago

Bump. When attempting to run examples/chat.go in a separate project I still get:

build command-line-arguments: cannot load github.com/perlin-network/noise/cipher/aead: module github.com/perlin-network/noise@latest found (v1.1.0), but does not contain package github.com/perlin-network/noise/cipher/aead

go.mod:

module github.com/cbertinato/noise_test

go 1.13

require (
        github.com/perlin-network/noise v1.1.0 // indirect
        github.com/pkg/errors v0.9.1 // indirect
)
asedov commented 4 years ago

Hi there. According to https://blog.golang.org/v2-go-modules

Starting with v2, the major version must appear at the end of the module path (declared in the module statement in the go.mod file). For example, when the authors of the module github.com/googleapis/gax-go developed v2, they used the new module path github.com/googleapis/gax-go/v2. Users who wanted to use v2 had to change their package imports and module requirements to github.com/googleapis/gax-go/v2.

The workaround is to go get the commit ID corresponding to the tag you're interested in: go get github.com/perlin-network/noise@3c13535b725d786f2d649e256c5383d49bb9c868

Thanks.

iwasaki-kenta commented 4 years ago

This should no longer be an issue once #266 is merged in.