Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions typeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"io"
"sort"
"strconv"
"strings"
)

// walkNamedTypes runs the callback for all named types contained in the given type.
Expand Down Expand Up @@ -165,20 +166,32 @@ 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())
fmt.Fprintf(w, "\t%s %q\n", s.importNames[pkg.Path()], unvendor(pkg.Path()))
} else {
fmt.Fprintf(w, "\t%q\n", pkg.Path())
fmt.Fprintf(w, "\t%q\n", unvendor(pkg.Path()))
}
}
fmt.Fprintln(w, ")")
}

// 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))
Expand Down