From 912b21774307b3d4d75dd1c9e215b71623ea2188 Mon Sep 17 00:00:00 2001 From: Dimitri Roche Date: Wed, 24 Oct 2018 22:06:18 -0400 Subject: [PATCH 1/2] Remove /vendor/ prefix from vendored packages --- typeutil.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/typeutil.go b/typeutil.go index 3a68dc8..06cf6af 100644 --- a/typeutil.go +++ b/typeutil.go @@ -11,6 +11,7 @@ import ( "io" "sort" "strconv" + "strings" ) // walkNamedTypes runs the callback for all named types contained in the given type. @@ -165,13 +166,24 @@ func newFileScope(imp types.Importer, pkg *types.Package) *fileScope { return &fileScope{otherNames: make(map[string]bool), pkg: pkg, imp: imp} } +func unvendor(path string) string { + // path might include "vendor" + // These paths do not compile, so we need to remove everything + // up to and including "/vendor/" + // see https://github.com/golang/go/issues/12019 + if i := strings.LastIndex(path, "/vendor/"); i != -1 { + path = path[i+len("/vendor/"):] + } + return path +} + func (s *fileScope) writeImportDecl(w io.Writer) { fmt.Fprintln(w, "import (") for _, pkg := range s.imports { if s.importNames[pkg.Path()] != pkg.Name() { fmt.Fprintf(w, "\t%s %q\n", s.importNames[pkg.Path()], pkg.Path()) } else { - fmt.Fprintf(w, "\t%q\n", pkg.Path()) + fmt.Fprintf(w, "\t%q\n", unvendor(pkg.Path())) } } fmt.Fprintln(w, ")") @@ -179,6 +191,7 @@ func (s *fileScope) writeImportDecl(w io.Writer) { // addImport loads a package and adds it to the import set. func (s *fileScope) addImport(path string) { + path = unvendor(path) pkg, err := s.imp.Import(path) if err != nil { panic(fmt.Errorf("can't import %q: %v", path, err)) From 9fdc78a7554ca78caadd7c21461f66088722b38b Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Fri, 15 Feb 2019 00:14:55 -0500 Subject: [PATCH 2/2] Update typeutil.go to also handle importNames Co-Authored-By: dimroc --- typeutil.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/typeutil.go b/typeutil.go index 06cf6af..2bffd10 100644 --- a/typeutil.go +++ b/typeutil.go @@ -181,7 +181,7 @@ func (s *fileScope) writeImportDecl(w io.Writer) { fmt.Fprintln(w, "import (") for _, pkg := range s.imports { if s.importNames[pkg.Path()] != pkg.Name() { - fmt.Fprintf(w, "\t%s %q\n", s.importNames[pkg.Path()], pkg.Path()) + fmt.Fprintf(w, "\t%s %q\n", s.importNames[pkg.Path()], unvendor(pkg.Path())) } else { fmt.Fprintf(w, "\t%q\n", unvendor(pkg.Path())) }