From 81a7f51e3984fdb746927c489b26729229b512cc Mon Sep 17 00:00:00 2001 From: keitax Date: Sun, 11 Jun 2017 04:22:13 +0900 Subject: [PATCH] Make to check working dir while considering symlinks --- context.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index c34f3b74d8..6cbb7e8b61 100644 --- a/context.go +++ b/context.go @@ -79,7 +79,11 @@ func (c *Ctx) SetPaths(workingDir string, gopaths ...string) error { for _, gp := range gopaths { gp = filepath.FromSlash(gp) - if fs.HasFilepathPrefix(wd, gp) { + ok, err := hasFilepathPrefix(wd, gp) + if err != nil { + return errors.Wrap(err, "failed to check the path") + } + if ok { c.GOPATH = gp } @@ -114,6 +118,19 @@ func defaultGOPATH() string { return "" } +// Similar to fs.HasFilepathPrefix() but aware of symlinks. +func hasFilepathPrefix(path, prefix string) (bool, error) { + p, err := filepath.EvalSymlinks(path) + if err != nil { + return false, errors.Wrap(err, "failed to resolve the symlink") + } + pre, err := filepath.EvalSymlinks(prefix) + if err != nil { + return false, errors.Wrap(err, "failed to resolve the symlink") + } + return fs.HasFilepathPrefix(p, pre), nil +} + func (c *Ctx) SourceManager() (*gps.SourceMgr, error) { return gps.NewSourceManager(filepath.Join(c.GOPATH, "pkg", "dep")) }