diff --git a/cmd/dep/init.go b/cmd/dep/init.go index 1a74306455..189ad039c0 100644 --- a/cmd/dep/init.go +++ b/cmd/dep/init.go @@ -17,6 +17,7 @@ import ( fb "github.com/golang/dep/internal/feedback" "github.com/golang/dep/internal/fs" "github.com/golang/dep/internal/gps" + "github.com/golang/dep/internal/gps/paths" "github.com/golang/dep/internal/gps/pkgtree" "github.com/pkg/errors" ) @@ -245,20 +246,6 @@ func contains(a []string, b string) bool { return false } -// isStdLib reports whether $GOROOT/src/path should be considered -// part of the standard distribution. For historical reasons we allow people to add -// their own code to $GOROOT instead of using $GOPATH, but we assume that -// code will start with a domain name (dot in the first element). -// This was loving taken from src/cmd/go/pkg.go in Go's code (isStandardImportPath). -func isStdLib(path string) bool { - i := strings.Index(path, "/") - if i < 0 { - i = len(path) - } - elem := path[:i] - return !strings.Contains(elem, ".") -} - // TODO solve failures can be really creative - we need to be similarly creative // in handling them and informing the user appropriately func handleAllTheFailuresOfTheWorld(err error) { @@ -370,7 +357,7 @@ func getProjectData(ctx *dep.Ctx, pkgT pkgtree.PackageTree, cpr string, sm gps.S return projectData{}, nil } - for _, ip := range rm.FlattenOmitStdLib() { + for _, ip := range rm.FlattenFn(paths.IsStandardImportPath) { pr, err := sm.DeduceProjectRoot(ip) if err != nil { return projectData{}, errors.Wrap(err, "sm.DeduceProjectRoot") // TODO: Skip and report ? @@ -506,7 +493,7 @@ func getProjectData(ctx *dep.Ctx, pkgT pkgtree.PackageTree, cpr string, sm gps.S // recurse for _, rpkg := range reached.External { - if isStdLib(rpkg) { + if paths.IsStandardImportPath(rpkg) { continue } diff --git a/cmd/dep/init_test.go b/cmd/dep/init_test.go index d654be8f25..37b6cfdf18 100644 --- a/cmd/dep/init_test.go +++ b/cmd/dep/init_test.go @@ -24,25 +24,6 @@ func TestContains(t *testing.T) { } } -func TestIsStdLib(t *testing.T) { - t.Parallel() - - tests := map[string]bool{ - "github.com/Sirupsen/logrus": false, - "encoding/json": true, - "golang.org/x/net/context": false, - "net/context": true, - ".": false, - } - - for p, e := range tests { - b := isStdLib(p) - if b != e { - t.Fatalf("%s: expected %t got %t", p, e, b) - } - } -} - func TestGetProjectPropertiesFromVersion(t *testing.T) { t.Parallel() diff --git a/cmd/dep/remove.go b/cmd/dep/remove.go index 1051664e1e..6cbe312b4b 100644 --- a/cmd/dep/remove.go +++ b/cmd/dep/remove.go @@ -10,6 +10,7 @@ import ( "github.com/golang/dep" "github.com/golang/dep/internal/gps" + "github.com/golang/dep/internal/gps/paths" "github.com/golang/dep/internal/gps/pkgtree" "github.com/pkg/errors" ) @@ -71,7 +72,7 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error { return errors.Errorf("remove takes no arguments when running with -unused") } - reachlist := reachmap.FlattenOmitStdLib() + reachlist := reachmap.FlattenFn(paths.IsStandardImportPath) // warm the cache in parallel, in case any paths require go get metadata // discovery @@ -81,7 +82,7 @@ func (cmd *removeCommand) Run(ctx *dep.Ctx, args []string) error { otherroots := make(map[gps.ProjectRoot]bool) for _, im := range reachlist { - if isStdLib(im) { + if paths.IsStandardImportPath(im) { continue } pr, err := sm.DeduceProjectRoot(im) diff --git a/cmd/dep/status.go b/cmd/dep/status.go index eb57abc9f4..3dfedc35fc 100644 --- a/cmd/dep/status.go +++ b/cmd/dep/status.go @@ -15,6 +15,7 @@ import ( "github.com/golang/dep" "github.com/golang/dep/internal/gps" + "github.com/golang/dep/internal/gps/paths" "github.com/golang/dep/internal/gps/pkgtree" "github.com/pkg/errors" ) @@ -164,7 +165,7 @@ func (out *dotOutput) BasicHeader() { ptree, _ := pkgtree.ListPackages(out.p.AbsRoot, string(out.p.ImportRoot)) prm, _ := ptree.ToReachMap(true, false, false, nil) - out.g.createNode(string(out.p.ImportRoot), "", prm.FlattenOmitStdLib()) + out.g.createNode(string(out.p.ImportRoot), "", prm.FlattenFn(paths.IsStandardImportPath)) } func (out *dotOutput) BasicFooter() { @@ -301,7 +302,7 @@ func runStatusAll(loggers *dep.Loggers, out outputter, p *dep.Project, sm gps.So } prm, _ := ptr.ToReachMap(true, false, false, nil) - bs.Children = prm.FlattenOmitStdLib() + bs.Children = prm.FlattenFn(paths.IsStandardImportPath) } // Split apart the version from the lock into its constituent parts @@ -370,7 +371,7 @@ func runStatusAll(loggers *dep.Loggers, out outputter, p *dep.Project, sm gps.So // lock. rm, _ := ptree.ToReachMap(true, true, false, nil) - external := rm.FlattenOmitStdLib() + external := rm.FlattenFn(paths.IsStandardImportPath) roots := make(map[gps.ProjectRoot][]string, len(external)) type fail struct { diff --git a/internal/gps/internal/paths/paths.go b/internal/gps/internal/paths/paths.go deleted file mode 100644 index a901e49603..0000000000 --- a/internal/gps/internal/paths/paths.go +++ /dev/null @@ -1,20 +0,0 @@ -// Copyright 2017 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package paths - -import "strings" - -// IsStandardImportPath indicates whether the provided path should be considered -// part of the standard library by checking the first path element for a '.'. -// -// This was lovingly lifted from src/cmd/go/pkg.go in Go's code. -func IsStandardImportPath(path string) bool { - i := strings.Index(path, "/") - if i < 0 { - i = len(path) - } - - return !strings.Contains(path[:i], ".") -} diff --git a/internal/gps/paths/paths.go b/internal/gps/paths/paths.go new file mode 100644 index 0000000000..3c18defe9c --- /dev/null +++ b/internal/gps/paths/paths.go @@ -0,0 +1,21 @@ +// Copyright 2017 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package paths + +import "strings" + +// IsStandardImportPath reports whether $GOROOT/src/path should be considered +// part of the standard distribution. For historical reasons we allow people to add +// their own code to $GOROOT instead of using $GOPATH, but we assume that +// code will start with a domain name (dot in the first element). +// This was loving taken from src/cmd/go/pkg.go in Go's code (isStandardImportPath). +func IsStandardImportPath(path string) bool { + i := strings.Index(path, "/") + if i < 0 { + i = len(path) + } + + return !strings.Contains(path[:i], ".") +} diff --git a/internal/gps/internal/paths/paths_test.go b/internal/gps/paths/paths_test.go similarity index 97% rename from internal/gps/internal/paths/paths_test.go rename to internal/gps/paths/paths_test.go index 5ead3409b3..aaaafc1cf1 100644 --- a/internal/gps/internal/paths/paths_test.go +++ b/internal/gps/paths/paths_test.go @@ -16,6 +16,7 @@ func TestIsStandardImportPath(t *testing.T) { {"github.com/anything", false}, {"github.com", false}, {"foo", true}, + {".", false}, } for _, f := range fix { diff --git a/internal/gps/pkgtree/pkgtree_test.go b/internal/gps/pkgtree/pkgtree_test.go index 9004bf106a..ab134f02e7 100644 --- a/internal/gps/pkgtree/pkgtree_test.go +++ b/internal/gps/pkgtree/pkgtree_test.go @@ -18,7 +18,7 @@ import ( "testing" "github.com/golang/dep/internal/fs" - "github.com/golang/dep/internal/gps/internal/paths" + "github.com/golang/dep/internal/gps/paths" ) // PackageTree.ToReachMap() uses an easily separable algorithm, wmToReach(), diff --git a/internal/gps/pkgtree/reachmap.go b/internal/gps/pkgtree/reachmap.go index 4952419ea4..27af5e90ba 100644 --- a/internal/gps/pkgtree/reachmap.go +++ b/internal/gps/pkgtree/reachmap.go @@ -7,8 +7,6 @@ package pkgtree import ( "sort" "strings" - - "github.com/golang/dep/internal/gps/internal/paths" ) // ReachMap maps a set of import paths (keys) to the sets of transitively @@ -20,11 +18,6 @@ type ReachMap map[string]struct { Internal, External []string } -// FlattenOmitStdLib calls FlattenFn with a function to exclude standard library import paths. -func (rm ReachMap) FlattenOmitStdLib() []string { - return rm.FlattenFn(paths.IsStandardImportPath) -} - // Eliminate import paths with any elements having leading dots, leading // underscores, or testdata. If these are internally reachable (which is // a no-no, but possible), any external imports will have already been diff --git a/internal/gps/solver.go b/internal/gps/solver.go index b272d63a11..c3621ff262 100644 --- a/internal/gps/solver.go +++ b/internal/gps/solver.go @@ -12,7 +12,7 @@ import ( "strings" "github.com/armon/go-radix" - "github.com/golang/dep/internal/gps/internal/paths" + "github.com/golang/dep/internal/gps/paths" "github.com/golang/dep/internal/gps/pkgtree" )