cmd/go: added verbose error from matchGoImport

The error coming out of matchGoImport does not differentiate between
having no imports, and having some invalid imports.

This some extra context to the error message to help debug these issues.

Fixes #16467

Change-Id: I3e9a119ed73da1bed5e07365be0221ea6b7f19db
Reviewed-on: https://go-review.googlesource.com/25121
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Russ Cox <rsc@golang.org>
This commit is contained in:
Tristan Ooohry 2016-07-22 04:11:58 +00:00 committed by Russ Cox
parent 87b1aaa37c
commit d2ef288c8b
2 changed files with 31 additions and 10 deletions

View file

@ -679,10 +679,10 @@ func repoRootForImportDynamic(importPath string, security securityMode) (*repoRo
// Find the matched meta import. // Find the matched meta import.
mmi, err := matchGoImport(imports, importPath) mmi, err := matchGoImport(imports, importPath)
if err != nil { if err != nil {
if err != errNoMatch { if _, ok := err.(ImportMismatchError); !ok {
return nil, fmt.Errorf("parse %s: %v", urlStr, err) return nil, fmt.Errorf("parse %s: %v", urlStr, err)
} }
return nil, fmt.Errorf("parse %s: no go-import meta tags", urlStr) return nil, fmt.Errorf("parse %s: no go-import meta tags (%s)", urlStr, err)
} }
if buildV { if buildV {
log.Printf("get %q: found meta tag %#v at %s", importPath, mmi, urlStr) log.Printf("get %q: found meta tag %#v at %s", importPath, mmi, urlStr)
@ -782,9 +782,6 @@ type metaImport struct {
Prefix, VCS, RepoRoot string Prefix, VCS, RepoRoot string
} }
// errNoMatch is returned from matchGoImport when there's no applicable match.
var errNoMatch = errors.New("no import match")
func splitPathHasPrefix(path, prefix []string) bool { func splitPathHasPrefix(path, prefix []string) bool {
if len(path) < len(prefix) { if len(path) < len(prefix) {
return false return false
@ -797,28 +794,45 @@ func splitPathHasPrefix(path, prefix []string) bool {
return true return true
} }
// A ImportMismatchError is returned where metaImport/s are present
// but none match our import path.
type ImportMismatchError struct {
importPath string
mismatches []string // the meta imports that were discarded for not matching our importPath
}
func (m ImportMismatchError) Error() string {
formattedStrings := make([]string, len(m.mismatches))
for i, pre := range m.mismatches {
formattedStrings[i] = fmt.Sprintf("meta tag %s did not match import path %s", pre, m.importPath)
}
return strings.Join(formattedStrings, ", ")
}
// matchGoImport returns the metaImport from imports matching importPath. // matchGoImport returns the metaImport from imports matching importPath.
// An error is returned if there are multiple matches. // An error is returned if there are multiple matches.
// errNoMatch is returned if none match. // errNoMatch is returned if none match.
func matchGoImport(imports []metaImport, importPath string) (_ metaImport, err error) { func matchGoImport(imports []metaImport, importPath string) (metaImport, error) {
match := -1 match := -1
imp := strings.Split(importPath, "/") imp := strings.Split(importPath, "/")
errImportMismatch := ImportMismatchError{importPath: importPath}
for i, im := range imports { for i, im := range imports {
pre := strings.Split(im.Prefix, "/") pre := strings.Split(im.Prefix, "/")
if !splitPathHasPrefix(imp, pre) { if !splitPathHasPrefix(imp, pre) {
errImportMismatch.mismatches = append(errImportMismatch.mismatches, im.Prefix)
continue continue
} }
if match != -1 { if match != -1 {
err = fmt.Errorf("multiple meta tags match import path %q", importPath) return metaImport{}, fmt.Errorf("multiple meta tags match import path %q", importPath)
return
} }
match = i match = i
} }
if match == -1 { if match == -1 {
err = errNoMatch return metaImport{}, errImportMismatch
return
} }
return imports[match], nil return imports[match], nil
} }

View file

@ -345,6 +345,13 @@ func TestMatchGoImport(t *testing.T) {
path: "example.com", path: "example.com",
err: errors.New("pathologically short path"), err: errors.New("pathologically short path"),
}, },
{
imports: []metaImport{
{Prefix: "example.com/user/foo", VCS: "git", RepoRoot: "https://example.com/repo/target"},
},
path: "different.example.com/user/foo",
err: errors.New("meta tags do not match import path"),
},
} }
for _, test := range tests { for _, test := range tests {