golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.28k stars 610 forks source link

importing "golang.org/x/net/context" in controller.go #124

Closed perhallgren closed 6 years ago

perhallgren commented 6 years ago

In github.com/golang/mock/gomock/controller.go I get an issue:

cannot find package "golang.org/x/net/context" in any of:
    /usr/local/Cellar/go/1.9/libexec/src/golang.org/x/net/context (from $GOROOT)
    ~/golang/src/golang.org/x/net/context (from $GOPATH)

This seems to be resolved by instead just importing context, as per https://stackoverflow.com/questions/42802278/install-context-package-in-golang. With an updated controller.go with lines 58-64 as below, it works fine for me.

import (
    "fmt"
    "context"
    "reflect"
    "runtime"
    "sync"
)

(running go version go1.9 darwin/amd64)

xmik commented 6 years ago

Duplicate to #116

balshetzer commented 6 years ago

This is actually the inverse of #116.

The problem in #116 was that we imported context from the standard library so users of an old version of gomock had compilation problems. To solve that we imported golang.org/x/net/context instead. This bug is saying that the compilation failed because golang.org/x/net/context was not found.

This issue is easily fixed by running "go get".

@zut please try "go get" and report back.

perhallgren commented 6 years ago

Hi!

I'm rather new to go, so not quite sure what's important to know to track this down.

I installed mockgen via go get github.com/golang/mock/mockgen this week (Thursday evening or Friday morning). This (I think) downloaded $GOPATH/src/github.com/golang/mock/gomock/controller.go, with the incorrect version containing the golang.org/x/net/context path.

I modified it to compile and worked on for a while. Then I did dep ensure for other reasons, which put a new copy in vendor/github.com/golang/mock/gomock/controller.go. This new copy has the correct import statements.

I don't know if I can find the version in the sources somehow to know what go get downloaded, but the files currently say

balshetzer commented 6 years ago

Older versions of Go (pre 1.7) didn't have a "context" package. If we import "context" then gomock breaks for them. There exists an external package, "golang.org/x/net/context", which is the same package. Using that works for both new and old versions of Go.

The problem that you ran into was that gomock has a dependency on that package but you hadn't downloaded that package. I believe that the go tool is supposed to get dependencies for you automatically but I'm not sure of the details. That's why I suggested running go get (with the unmodified code) to see if the go tool downloads the dependencies for you.

perhallgren commented 6 years ago

Do you mean I should first do go get github.com/golang/mock/mockgen with the lib specified, then just go get without parameters?

Right now if I run go get, I don't get anything downloaded into $GOPATH/src/golang.org/x/net.

balshetzer commented 6 years ago

You would run go get github.com/golang/mock then cd github.com/golang/mock/gomock then go get. This sequence worked for me.

balshetzer commented 6 years ago

I think the root of this issue is that gomock has an external dependency but I think it should be fine for gomock to have an external dependency.

I'm open to suggestions though on how to make this easier, especially if there are common practices for managing external deps in Go projects.

balshetzer commented 6 years ago

Closing for now. Feel free to re-open.

praveen691cs commented 5 years ago

goerror getting the error as show in the above image. I have all the packages in the directory but still getting the error

poy commented 5 years ago

@praveen691cs I'm not seeing that github.com/golang/mock is referenced in that screen shot. Am I missing it?

praveen691cs commented 5 years ago

sorry i didn't get you. could you please explain it a bit more. why should we refer refer to github.com/golang/mock?? It is a new app while using google sheets api i am getting that error even though the packages are availabe in the current directory

poy commented 5 years ago

@praveen691cs I am asking if you are suggesting controller.go from github.com/golang/mock is importing golang.org/x/net/context?