From 00d4fcd8413f88bea933725034db27a8dd8b46ba Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Fri, 10 Aug 2018 12:49:12 -0400 Subject: [PATCH] go/packages: remove dependency on imports Copy imports.VendorlessPath into go/packages to avoid an unnecessary dependency on imports. Change-Id: Ie77c2fb87f2199f139ece9f3d1b707f065fc1a79 Reviewed-on: https://go-review.googlesource.com/128996 Reviewed-by: Michael Matloob Run-TryBot: Michael Matloob TryBot-Result: Gobot Gobot --- go/packages/golist_fallback.go | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/go/packages/golist_fallback.go b/go/packages/golist_fallback.go index 60357758..79400dea 100644 --- a/go/packages/golist_fallback.go +++ b/go/packages/golist_fallback.go @@ -16,7 +16,6 @@ import ( "strings" "golang.org/x/tools/go/internal/cgo" - "golang.org/x/tools/imports" ) // TODO(matloob): Delete this file once Go 1.12 is released. @@ -62,7 +61,7 @@ func golistDriverFallback(cfg *Config, words ...string) (*driverResponse, error) } continue } - importMap[imports.VendorlessPath(id)] = &Package{ID: id} + importMap[vendorlessPath(id)] = &Package{ID: id} } return importMap } @@ -172,6 +171,20 @@ func golistDriverFallback(cfg *Config, words ...string) (*driverResponse, error) return &response, nil } +// vendorlessPath returns the devendorized version of the import path ipath. +// For example, VendorlessPath("foo/bar/vendor/a/b") returns "a/b". +// Copied from golang.org/x/tools/imports/fix.go. +func vendorlessPath(ipath string) string { + // Devendorize for use in import statement. + if i := strings.LastIndex(ipath, "/vendor/"); i >= 0 { + return ipath[i+len("/vendor/"):] + } + if strings.HasPrefix(ipath, "vendor/") { + return ipath[len("vendor/"):] + } + return ipath +} + // getDeps runs an initial go list to determine all the dependency packages. func getDeps(cfg *Config, words ...string) (originalSet map[string]*jsonPackage, deps []string, err error) { buf, err := golist(cfg, golistArgsFallback(cfg, words))