-
Notifications
You must be signed in to change notification settings - Fork 1k
Use evaluated project root path #812
Changes from 7 commits
849e187
f9b8523
1ad2706
88faf50
c28ab32
45540a7
96a9333
40d9942
3d59125
b124a46
618951f
92886c5
93dfae7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
package dep | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
|
@@ -222,15 +223,26 @@ 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) { | ||
srcprefix := filepath.Join(c.GOPATH, "src") + string(filepath.Separator) | ||
if fs.HasFilepathPrefix(path, srcprefix) { | ||
if len(path) <= len(srcprefix) { | ||
|
||
|
||
gopathEvaluated, err := filepath.EvalSymlinks(c.GOPATH) | ||
if err != nil { | ||
return "", fmt.Errorf("Error evaluating symlinks in GOPATH %s: %s", c.GOPATH, err.Error()) | ||
|
||
} | ||
|
||
pathEvaluated, err := filepath.EvalSymlinks(path) | ||
if err != nil { | ||
return "", fmt.Errorf("Error evaluating symlinks in %s: %s", path, err.Error()) | ||
|
||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't we just do If they are the same, we did what line 223 is doing. If they are different, we did the if block logic. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or just use |
||
|
||
srcprefix := filepath.Join(gopathEvaluated, "src") + string(filepath.Separator) | ||
if fs.HasFilepathPrefix(pathEvaluated, srcprefix) { | ||
if len(pathEvaluated) <= len(srcprefix) { | ||
return "", errors.New("dep does not currently support using GOPATH/src as the project root") | ||
} | ||
|
||
// filepath.ToSlash because we're dealing with an import path now, | ||
// not an fs path | ||
return filepath.ToSlash(path[len(srcprefix):]), nil | ||
return filepath.ToSlash(pathEvaluated[len(srcprefix):]), nil | ||
} | ||
|
||
return "", errors.Errorf("%s not in GOPATH", path) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -491,3 +491,32 @@ func TestDetectGOPATH(t *testing.T) { | |
} | ||
} | ||
} | ||
|
||
func TestDetectProjectSymlinkedOutsideGOPATH(t *testing.T) { | ||
h := test.NewHelper(t) | ||
defer h.Cleanup() | ||
|
||
h.TempDir("src") | ||
|
||
h.Setenv("GOPATH", h.Path(".")) | ||
depCtx := &Ctx{GOPATH: h.Path(".")} | ||
|
||
importPath := "ti/ga/fato/la/večera" | ||
h.TempDir(filepath.Join("src", importPath)) | ||
h.TempDir("symlink/to/project") | ||
|
||
symlinkSrc := h.OriginalPath(filepath.Join("src", importPath)) | ||
symlinkTarget := h.OriginalPath("symlink/to/project/sym") | ||
err := os.Symlink(symlinkSrc, symlinkTarget) | ||
if err != nil { | ||
t.Errorf("Error creating symlink from %s to %s: %s", symlinkSrc, symlinkTarget, err.Error()) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since if err := os.Symlink(symlinkSrc, symlinkTarget); err != nil {
t.Errorf("Error creating symlink from %s to %s: %s", symlinkSrc, symlinkTarget, err.Error())
} |
||
|
||
got, err := depCtx.ImportForAbs(symlinkTarget) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if got != importPath { | ||
t.Fatalf("expected %s, got %s", importPath, got) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -498,8 +498,21 @@ func (h *Helper) TempDir(path string) { | |
} | ||
|
||
// Path returns the absolute pathname to file with the temporary | ||
// directory. | ||
// directory. Symlinks are evaluated. | ||
func (h *Helper) Path(name string) string { | ||
path := h.OriginalPath(name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we instead keep There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ibrasho yes, it makes sense. But I'd propose to delay that change after @sdboyer decides if this PR is mergeable or not. Because, this change will just increase the diff by a few hundred lines and increase the possibility of merge conflicts with the current If everything is OK, I'll make sure that the PR with this additional change merges in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sdboyer What's your opinion? |
||
|
||
// Ensure it's the absolute, symlink-less path we're returning | ||
abs, err := filepath.EvalSymlinks(path) | ||
if err != nil { | ||
h.t.Fatalf("%+v", errors.Wrapf(err, "internal testsuite error: could not get absolute path for dir(%q)", path)) | ||
} | ||
return abs | ||
} | ||
|
||
// OriginalPath returns the absolute pathname to file with the temporary | ||
// directory. Symlinks are NOT evaluated. | ||
func (h *Helper) OriginalPath(name string) string { | ||
if h.tempdir == "" { | ||
h.t.Fatalf("%+v", errors.Errorf("internal testsuite error: path(%q) with no tempdir", name)) | ||
} | ||
|
@@ -511,12 +524,7 @@ func (h *Helper) Path(name string) string { | |
joined = filepath.Join(h.tempdir, name) | ||
} | ||
|
||
// Ensure it's the absolute, symlink-less path we're returning | ||
abs, err := filepath.EvalSymlinks(joined) | ||
if err != nil { | ||
h.t.Fatalf("%+v", errors.Wrapf(err, "internal testsuite error: could not get absolute path for dir(%q)", joined)) | ||
} | ||
return abs | ||
return joined | ||
} | ||
|
||
// MustExist fails if path does not exist. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sure you run
gofmt
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, not really sure what's the problem here. I did run gofmt (in fact one of the pust-push checks fails when the code is not gofmt'ed).