diff --git a/go/buildutil/allpackages.go b/go/buildutil/allpackages.go index 0f909eeb..d28a5d0e 100644 --- a/go/buildutil/allpackages.go +++ b/go/buildutil/allpackages.go @@ -18,9 +18,10 @@ import ( "sync" ) -// AllPackages returns the import path of each Go package in any source +// AllPackages returns the package path of each Go package in any source // directory of the specified build context (e.g. $GOROOT or an element // of $GOPATH). Errors are ignored. The results are sorted. +// All package paths are canonical, and thus may contain "/vendor/". // // The result may include import paths for directories that contain no // *.go files, such as "archive" (in $GOROOT/src). @@ -37,9 +38,10 @@ func AllPackages(ctxt *build.Context) []string { return list } -// ForEachPackage calls the found function with the import path of +// ForEachPackage calls the found function with the package path of // each Go package it finds in any source directory of the specified // build context (e.g. $GOROOT or an element of $GOPATH). +// All package paths are canonical, and thus may contain "/vendor/". // // If the package directory exists but could not be read, the second // argument to the found function provides the error. diff --git a/go/buildutil/buildutil_go15.go b/go/buildutil/buildutil_go15.go new file mode 100644 index 00000000..d1640b14 --- /dev/null +++ b/go/buildutil/buildutil_go15.go @@ -0,0 +1,14 @@ +// Copyright 2015 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. + +// +build !go1.6 + +package buildutil + +import "go/build" + +// AllowVendor is a synonym for zero. +// It allows applications to refer to the go/build.AllowVendor +// feature whether or not it is supported. +const AllowVendor build.ImportMode = 0 diff --git a/go/buildutil/buildutil_go16.go b/go/buildutil/buildutil_go16.go new file mode 100644 index 00000000..c5703d72 --- /dev/null +++ b/go/buildutil/buildutil_go16.go @@ -0,0 +1,14 @@ +// Copyright 2015 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. + +// +build go1.6 + +package buildutil + +import "go/build" + +// AllowVendor is a synonym for go/build.AllowVendor. +// It allows applications to refer to the AllowVendor +// feature whether or not it is supported. +const AllowVendor build.ImportMode = build.AllowVendor diff --git a/go/buildutil/util.go b/go/buildutil/util.go index 0e093fc0..c4bdd29f 100644 --- a/go/buildutil/util.go +++ b/go/buildutil/util.go @@ -90,6 +90,15 @@ func dirHasPrefix(dir, prefix string) bool { return len(dir) >= len(prefix) && strings.EqualFold(dir[:len(prefix)], prefix) } +// StripVendor removes the "vendor" segment and all preceding ones +// from a slash-segmented path. (See go/build.AllowVendor.) +func StripVendor(path string) string { + if i := strings.LastIndex(path, "/vendor/"); i >= 0 { + return path[i+len("/vendor/"):] + } + return strings.TrimPrefix(path, "vendor/") +} + // -- Effective methods of file system interface ------------------------- // (go/build.Context defines these as methods, but does not export them.) diff --git a/go/buildutil/util_test.go b/go/buildutil/util_test.go index 10dae177..b6350229 100644 --- a/go/buildutil/util_test.go +++ b/go/buildutil/util_test.go @@ -29,6 +29,8 @@ func TestContainingPackage(t *testing.T) { {goroot + "/src/encoding/missing/foo.go", "(not found)"}, {gopath + "/src/golang.org/x/tools/go/buildutil/util_test.go", "golang.org/x/tools/go/buildutil"}, + {gopath + "/src/vendor/golang.org/x/net/http2/hpack/hpack.go", + "vendor/golang.org/x/net/http2/hpack"}, } { file, want := test[0], test[1] bp, err := buildutil.ContainingPackage(&build.Default, ".", file) @@ -43,3 +45,22 @@ func TestContainingPackage(t *testing.T) { // TODO(adonovan): test on virtualized GOPATH too. } + +func TestStripVendor(t *testing.T) { + for _, test := range []struct { + path, want string + }{ + {"", ""}, + {"a", "a"}, + {"a/b", "a/b"}, + {"a/vendor/b", "b"}, + {"a/b/vendor/c/d", "c/d"}, + {"vendor/a/b", "a/b"}, + {"a/vendor", "a/vendor"}, + } { + if got := buildutil.StripVendor(test.path); got != test.want { + t.Errorf("StripVendor(%q) = %q, want %q", + test.path, got, test.want) + } + } +}