Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

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

Closed
perhallgren opened this issue Nov 3, 2017 · 12 comments
Closed

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

perhallgren opened this issue Nov 3, 2017 · 12 comments

Comments

@perhallgren
Copy link

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
Copy link
Contributor

xmik commented Nov 3, 2017

Duplicate to #116

@balshetzer
Copy link
Collaborator

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
Copy link
Author

perhallgren commented Nov 4, 2017

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

  • Old, seemingly broken, version:

     import (
     	"fmt"
     	"context" // this line said "golang.org/x/net/context" when I downloaded it
     	"reflect"
     	"runtime"
     	"sync"
     )
    
  • New, correct version:

     import (
     	"fmt"
     	"reflect"
     	"sync"
     )
    

@balshetzer
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

balshetzer commented Nov 9, 2017

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
Copy link
Collaborator

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
Copy link
Collaborator

Closing for now. Feel free to re-open.

@praveen691cs
Copy link

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

@poy
Copy link
Collaborator

poy commented Feb 26, 2019

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

@praveen691cs
Copy link

praveen691cs commented Feb 27, 2019

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
Copy link
Collaborator

poy commented Feb 27, 2019

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants