Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove <rootpath>/vendor/ prefix from vendored packages #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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