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

Commit 64f48c9

Browse files
committed
context.go: Clean GOPATH with filepath.Clean() before returning
Removes artifacts such as trailing and repeated separators, directory navigation symbols ('./' '../'). Also add a test case to ensure that repeated separators are handled correctly (relevant for #1946).
1 parent 8d1120f commit 64f48c9

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ func (c *Ctx) detectGOPATH(path string) (string, error) {
302302
return "", errors.Wrap(err, "failed to detect GOPATH")
303303
}
304304
if isPrefix {
305-
return gp, nil
305+
return filepath.Clean(gp), nil
306306
}
307307
}
308308
return "", errors.Errorf("%s is not within a known GOPATH/src", path)

context_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -524,10 +524,14 @@ func TestDetectGOPATH(t *testing.T) {
524524
th.TempDir(filepath.Join("code", "src", "github.com", "username", "package"))
525525
th.TempDir(filepath.Join("go", "src", "github.com", "username", "package"))
526526
th.TempDir(filepath.Join("gotwo", "src", "github.com", "username", "package"))
527+
th.TempDir(filepath.Join("gothree", "sep", "src", "github.com", "username", "package"))
528+
529+
sep := string(os.PathSeparator)
527530

528531
ctx := &Ctx{GOPATHs: []string{
529532
th.Path("go"),
530533
th.Path("gotwo"),
534+
th.Path("gothree") + sep + sep + "sep",
531535
}}
532536

533537
testcases := []struct {
@@ -538,6 +542,8 @@ func TestDetectGOPATH(t *testing.T) {
538542
{th.Path("go"), th.Path(filepath.Join("go", "src", "github.com", "username", "package")), false},
539543
{th.Path("go"), th.Path(filepath.Join("go", "src", "github.com", "username", "package")), false},
540544
{th.Path("gotwo"), th.Path(filepath.Join("gotwo", "src", "github.com", "username", "package")), false},
545+
{th.Path(filepath.Join("gothree", "sep")),
546+
th.Path(filepath.Join("gothree", "sep", "src", "github.com", "username", "package")), false},
541547
{"", th.Path(filepath.Join("code", "src", "github.com", "username", "package")), true},
542548
}
543549

0 commit comments

Comments
 (0)