Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Resolve symlinks if project root has them. #247

Merged
merged 15 commits into from
Apr 11, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ func (c *Ctx) LoadProject(path string) (*Project, error) {
//
// The second returned string indicates which GOPATH value was used.
func (c *Ctx) SplitAbsoluteProjectRoot(path string) (string, error) {
// the path may lie within a symlinked directory, resolve those first
path, err := filepath.EvalSymlinks(path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of calling this within SplitAbsoluteProjectRoot(), let's do the symlink evaluation up in LoadProject(). That keeps this a bit cleaner, by keeping this function solely dealing with symbolic strings, and LoadProject() doing the actual filesystem work.


if err != nil {
return "", fmt.Errorf("failed attempt to resolve symlinks: %s", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our patterns around error handling aren't exactly the best right now, but let's be consistent - when wrapping an error returned from a called function, please use errors.Wrapf().

}

srcprefix := filepath.Join(c.GOPATH, "src") + string(filepath.Separator)
if strings.HasPrefix(path, srcprefix) {
// filepath.ToSlash because we're dealing with an import path now,
Expand Down
22 changes: 22 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {
defer h.Cleanup()

h.TempDir("src")

h.Setenv("GOPATH", h.Path("."))
depCtx := &Ctx{GOPATH: h.Path(".")}

Expand All @@ -44,6 +45,9 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {
}

for _, want := range importPaths {
// create the target directory so lstat doesn't fail
h.TempDir(filepath.Join("src", want))

fullpath := filepath.Join(depCtx.GOPATH, "src", want)
got, err := depCtx.SplitAbsoluteProjectRoot(fullpath)
if err != nil {
Expand All @@ -59,6 +63,24 @@ func TestSplitAbsoluteProjectRoot(t *testing.T) {
if err == nil {
t.Fatalf("should have gotten error but did not for tra/la/la/la: %s", got)
}

// test resolving project root is symlinked
want := "real/full/path"
symlinkedPath := filepath.Join(depCtx.GOPATH, "src", "sympath")

h.TempDir(filepath.Join("src", want))

os.Symlink(
filepath.Join(depCtx.GOPATH, "src", want),
symlinkedPath)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: just one line is fine, please


got, err = depCtx.SplitAbsoluteProjectRoot(symlinkedPath)
if err != nil {
t.Fatal(err)
}
if got != want {
t.Fatalf("expected %s, got %s", want, got)
}
}

func TestAbsoluteProjectRoot(t *testing.T) {
Expand Down