Dep: GOPATH should resolve symlinks

Created on 16 Jun 2018  ยท  10Comments  ยท  Source: golang/dep

What version of dep are you using (dep version)?

dep: version : v0.4.1 build date : 2018-01-27 git hash : 37d9ea0 go version : go1.9.3 go compiler : gc platform : darwin/amd64

What dep command did you run?

โฏ echo $GOPATH && dep init /Users/andig/Documents/htdocs/go init failed: unable to determine the import path for the root project /Users/andig/htdocs/go/src/github.com/andig/gosml: /Users/andig/htdocs/go/src/github.com/andig/gosml is not within any GOPATH/src

What did you expect to see?

since /Users/andig/htdocs is a symlink to /Users/andig/Documents/htdocs/go this should have worked as the path is identical.

What did you see instead?

See error above.

Most helpful comment

Dep should do a 'realpath' of both the cwd and the $GOPATH, since this also happens if your cwd has a symlink in it.

All 10 comments

Same here. I have GOPATH added into .bashrc

This is one of the closed issues. dep does not find second/alternate path in GOPATH and Go has it registered in env. It fails with init failed: unable to determine the import path for the root project.

What version of Go are you using (go version)?

go version go1.10 linux/amd64

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

GOARCH="amd64"
GOBIN=""
GOCACHE="/home/gb/.cache/go-build"
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/home/gb/src:/media/gb/myfolder/"
GORACE=""
GOROOT="/usr/lib/go-1.10"
GOTMPDIR=""
GOTOOLDIR="/usr/lib/go-1.10/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build555332572=/tmp/go-build -gno-record-gcc-switches"

What did you do?

dep init

If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.

What did you expect to see?

initialise repository

What did you see instead?

init failed: unable to determine the import path for the root project /home/gb/src/projects/a1: /home/gb/src/projects/a1 is not within any GOPATH/src

init failed: unable to determine the import path for the root project /media/gb/myfolder/projects/a1: /media/gb/myfolder/src/projects/a1 is not within any GOPATH/src

Same issue here on FreeBSD

/home is just a symlink to /usr/home by default on FreeBSD, so when dep init gets the pwd of a project in $GOPATH/src and compares them, it fails saying it's not in $GOPATH/src, even with the default $GOPATH/one set by GOPATH=$(go env GOPATH)

Dep should do a 'realpath' of both the cwd and the $GOPATH, since this also happens if your cwd has a symlink in it.

I should add that all combinations should be allowed; check both cwd and its realpath, and check both $GOPATH and its realpath, to allow symlinks in and out of both.

Hi. Ran into this issue as well, the diff below fixed it for me. I am relatively new to both go and dep, so I'm not certain about the impact of this patch. Feedback welcome, once all is good perhaps this could make it into a PR?

diff --git a/context.go b/context.go
index 9dc33dc3..6caa0284 100644
--- a/context.go
+++ b/context.go
@@ -250,6 +250,11 @@ func (c *Ctx) detectGOPATH(path string) (string, error) {
 // ImportForAbs returns the import path for an absolute project path by trimming the
 // `$GOPATH/src/` prefix.  Returns an error for paths equal to, or without this prefix.
 func (c *Ctx) ImportForAbs(path string) (string, error) {
+       rpath, err := filepath.EvalSymlinks(path)
+       if err != nil {
+               return "", errors.New("Symlink eval failed")
+       }
+       path = rpath
        srcprefix := filepath.Join(c.GOPATH, "src") + string(filepath.Separator)
        isPrefix, err := fs.HasFilepathPrefix(path, srcprefix)
        if err != nil {

@jpleau unfortunately, that only fixes one of three cases, where the GOPATH has a symlink.

There are two other
1) cwd contain in a symlink
2) cmd AND GOPATH contain a symlink

in case 1, cwd and its realpath need to be checked against GOPATH

in case 2, cmd and its realpath need to be checked against GOPATH and its realpath.

And it isn't as simple as converting them both to realpath, since symlinks may be what are used to give them the same prefix - eg different subdirectories of GOPATH are located along different realpaths.

@nyetwurk do you have examples for your 2 issues? I'm not sure I fully grasp what you are saying and am unable to reproduce..

My use case is (the above diff fixes it):

GOPATH=/mnt/shared/go
Symlink: $HOME/go -> $GOPATH
Symlink: $HOME/project -> $HOME/go/src/gitlab.com/jpleau/project

Going either in $HOME/go/src/gitlab.com/jpleau/project or $HOME/project I can run dep init and dep ensure

hi folks! Just to note: we're generally not investing time in symlink issues, as they are a massive time suck and there's a long history of getting them wrong, and just making things worse. Fortunately, you can now probably achieve a workaround with tip/the next release of dep, via the new DEPPROJECTROOT env var. It'd be great if anyone here struggling with symlinks could give that a try.

I have the same issue (dep 0.5.2 on macos), and following is my workaround:

alias dep='cd $(pwd -P) && dep'

Dep was officially deprecated earlier this year, and the proposal to archive this repository was accepted. As such, I'm closing outstanding issues before archiving the repository. For any further comments, please use the proposal thread on the Go issue tracker. Thanks!

Was this page helpful?
0 / 5 - 0 ratings