3

Everything I read about the vendor directory gives me the understanding that if I have a directory:

$GOPATH/src/vendor

And put my dependencies in there (I am using godeps), when doing go run, go should check in that directory first.

If I run my code in a Docker image I have, this works fine. However now that I try to run the same code on my Windows machine, go simply ignores the vendor/ directory, and fails to find the dependencies.

What am I doing wrong?

main.go:7:2: cannot find package "gopkg.in/alecthomas/kingpin.v2" in any of:
        C:\Go\src\gopkg.in\alecthomas\kingpin.v2 (from $GOROOT)
        C:\Users\js\dev\my_project\rest\src\gopkg.in\alecthomas\kingpin.v2 (from $GOPATH)
        C:\Users\js\dev\go\src\gopkg.in\alecthomas\kingpin.v2

Is the output when I try to do:

go run main.go

A directory vendor/ exists in this directory.

go version go1.7 windows/amd64

The exact commands I run (in windows cmd.exe)

> cd C:\Users\js\dev\my_project\rest\
> set GOPATH=C:\Users\js\dev\my_project\rest\;c:\Users\js\dev\go

> cd src
> dir
...
2016-09-01  23:20             2 923 main.go
...
2016-09-03  01:27    <DIR>          vendor

> go run main.go
Joakim
  • 11,468
  • 9
  • 44
  • 50
  • You were quick, but I just realised I forgot it and added it before I saw your comment. go1.7 – Joakim Sep 02 '16 at 23:45
  • And where exactly is your `main.go`? – tkausl Sep 02 '16 at 23:48
  • `$GOPATH/src/main.go` and `$GOPATH/src/vendor/`... This exact same `vendor` dir works when I mount the exact same directory in Docker. – Joakim Sep 02 '16 at 23:51
  • To be clear: `cd C:\Users\js\dev\my_project\rest\src\` then `set GOPATH=C:\Users\js\dev\my_project\rest\src\;C:\Users\js\dev\go` after that I do `go run main.go` – Joakim Sep 02 '16 at 23:52
  • Don't use `go run`, and make sure you go source is in a directory in GOPATH. There's also not much point in putting the vendor outside your project directory, since the idea is you vendor the dependencies for your project. – JimB Sep 03 '16 at 00:08
  • Why shouldn't I use `go run`? Also `go build` gives the same issue. And as I state my source is in `GOPATH`, under `$GOPATH/src/` specifically. – Joakim Sep 03 '16 at 00:11
  • `go run` operates on single files, not packages, so it's a common point of confusion around GOPATH issues. Your main.go is not in an import directory. You shouldn't have any source files in GOPATH/src – JimB Sep 03 '16 at 00:50

3 Answers3

4

The reason this did not work is because you are not supposed to put any code directly into the $GOPATH/src/ directory.

The solution is to put your project into a sub-directory like so:

$GOPATH/src/app/*.go

Joakim
  • 11,468
  • 9
  • 44
  • 50
1

Seems your GOPATH is incorrect? The GOPATH should specify the location of your workspace i.e. directory containing src, pkg and bin directories at its root.

Try doing

set GOPATH=C:\Users\js\dev\my_project\rest\;c:\Users\js\dev\go

More details at: https://golang.org/doc/code.html

ppp
  • 975
  • 7
  • 15
  • Sorry I had made a typo when writing the example of what I am writing. This does not work either, it never checks the vendor directory. I had set the path like you suggest all along... In my example I accidentally added `src/` at the end – Joakim Sep 03 '16 at 00:09
0

The first thing to understand is that godep save is simply copying dependencies from your $GOPATH to a vendor directory inside your project.

You will have to go get your dependencies first. After you have them in $GOPATH, you can do a godep save to copy the current version to your project, and be assured that even if the version in $GOPATH changes, you will have a fixed version in your project until you explicitly change it via godep.

So, if I have a $GOPATH of /home/me/go_workspace, and a project called $GOPATH/src/github.com/project_x with a dependency of github.com/you/xyz, then I would do go get github.com/you/xyz, and godep save from within my project directory. This would create a vendor folder with the dependency at its current commit inside.

jxstanford
  • 3,339
  • 3
  • 27
  • 39
  • This does not help with my problem however. I have done `go get` and `godep save`. The dependencies are there in the `vendor/` directory. Everything works fine with this setup if I run inside of my Docker, it uses the dependencies in the `vendor/` directory. But if I instead run it on Windows, exact same directory and files, it doesn't look in `vendor/` – Joakim Sep 03 '16 at 00:17
  • Did you try setting GO15VENDOREXPERIMENT=1 in your environment explicitly? You shouldn't have to, but it's not impossibly that you hit a bug, and might be worth a quick try. – jxstanford Sep 03 '16 at 00:20
  • You should also ensure that your version of `godep` is up to date. I have `godep v74 (darwin/amd64/go1.7)`. I ran into some trouble when I had an outdated `godep`. – jxstanford Sep 03 '16 at 00:22
  • Yes I actually tried that already. And I retried it now, but no help. (In regards to using GO15VENDOREXPERIMENT=1) – Joakim Sep 03 '16 at 00:23
  • Ok, but I shouldn't need godep at all should I? It is just there as a help. As I understand it, I could manually create a `vendor/` dir and copy stuff there myself (I haven't, but that should work) – Joakim Sep 03 '16 at 00:27
  • I'm not sure about that. For what it's worth, I generally create a binary for each platform I'm interested in and execute it (rather than using `go run`). Something like: `env GOOS=darwin GOARCH=amd64 CGO_ENABLED=0 go build -o bin/$(BINNAME).darwin` for mac and `env GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o bin/$(BINNAME).linux`. Hopefully this isn't making you chase your tail around :-) – jxstanford Sep 03 '16 at 00:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122552/discussion-between-jxstanford-and-joakim). – jxstanford Sep 03 '16 at 00:31