diff options
author | Rob Pike <r@golang.org> | 2014-05-27 14:37:36 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2014-05-27 14:37:36 -0700 |
commit | 7f638e90231e56c523ae51ba18dbbf084db8b86e (patch) | |
tree | 554028277a462b042df7fba6a9619ad0ef87d736 | |
parent | 0a8ce65c7a32f991e6a147165c2d0ff86b1db2f6 (diff) | |
download | go-7f638e90231e56c523ae51ba18dbbf084db8b86e.tar.gz go-7f638e90231e56c523ae51ba18dbbf084db8b86e.zip |
cmd/go: improve error message when import path contains http://
Common mistake (at least for me) because hg etc. require the prefix
while the go command forbids it.
Before:
% go get http://code.google.com/p/go.text/unicode/norm
package http:/code.google.com/p/go.text/unicode/norm: unrecognized import path "http:/code.google.com/p/go.text/unicode/norm"
After:
% go get http://code.google.com/p/go.text/unicode/norm
package http:/code.google.com/p/go.text/unicode/norm: "http://" not allowed in import path
LGTM=ruiu, rsc
R=rsc, ruiu
CC=golang-codereviews
https://golang.org/cl/97630046
-rw-r--r-- | src/cmd/go/vcs.go | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/cmd/go/vcs.go b/src/cmd/go/vcs.go index 22d5ebc244..8f0bae0b75 100644 --- a/src/cmd/go/vcs.go +++ b/src/cmd/go/vcs.go @@ -354,6 +354,8 @@ type repoRoot struct { root string } +var httpPrefixRE = regexp.MustCompile(`^https?:`) + // repoRootForImportPath analyzes importPath to determine the // version control system, and code repository to use. func repoRootForImportPath(importPath string) (*repoRoot, error) { @@ -390,8 +392,12 @@ var errUnknownSite = errors.New("dynamic lookup required to find mapping") // // If scheme is non-empty, that scheme is forced. func repoRootForImportPathStatic(importPath, scheme string) (*repoRoot, error) { - if strings.Contains(importPath, "://") { - return nil, fmt.Errorf("invalid import path %q", importPath) + // A common error is to use https://packagepath because that's what + // hg and git require. Diagnose this helpfully. + if loc := httpPrefixRE.FindStringIndex(importPath); loc != nil { + // The importPath has been cleaned, so has only one slash. The pattern + // ignores the slashes; the error message puts them back on the RHS at least. + return nil, fmt.Errorf("%q not allowed in import path", importPath[loc[0]:loc[1]]+"//") } for _, srv := range vcsPaths { if !strings.HasPrefix(importPath, srv.prefix) { |