1
0
mirror of https://github.com/golang/go synced 2024-11-22 19:14:53 -07:00

cmd/go/internal/modget: clarify error for 'go get' without arguments

If the current directory doesn't contain a package, 'go get' will say
that without additional detail.

If there were no arguments, errors will start with "go get:" instead
of "go get .:".

Fixes #39080

Change-Id: I47366f2a27bce17bd8b79344ad15b8b934a888c9
Reviewed-on: https://go-review.googlesource.com/c/go/+/234681
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Trust: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Jay Conrod 2020-05-20 13:51:53 -04:00
parent 3e56bad13b
commit 0968d2d599
3 changed files with 19 additions and 2 deletions

View File

@ -505,6 +505,12 @@ func parseArgs(ctx context.Context, rawArgs []string) []*query {
continue
}
// If there were no arguments, CleanPatterns returns ".". Set the raw
// string back to "" for better errors.
if len(rawArgs) == 0 {
q.raw = ""
}
// Guard against 'go get x.go', a common mistake.
// Note that package and module paths may end with '.go', so only print an error
// if the argument has no version and either has no slash or refers to an existing file.
@ -820,6 +826,9 @@ func (r *resolver) performLocalQueries(ctx context.Context) {
}
if len(match.Pkgs) == 0 {
if q.raw == "" || q.raw == "." {
return errSet(fmt.Errorf("no package in current directory"))
}
if !q.isWildcard() {
return errSet(fmt.Errorf("%s%s is not a package in module rooted at %s", q.pattern, absDetail, modload.ModRoot()))
}

View File

@ -295,7 +295,11 @@ func reportError(q *query, err error) {
}
}
base.Errorf("go get %s: %s", q, errStr)
if qs := q.String(); qs != "" {
base.Errorf("go get %s: %s", qs, errStr)
} else {
base.Errorf("go get: %s", errStr)
}
}
func reportConflict(pq *query, m module.Version, conflict versionReason) {

View File

@ -4,8 +4,12 @@ env GO111MODULE=on
# in an empty directory should refer to the path '.' and should not attempt
# to resolve an external module.
cd dir
! go get
stderr '^go get: no package in current directory$'
! go get .
stderr 'go get: \. \(.*[/\\]dir\) is not a package in module rooted at .*[/\\]dir$'
stderr '^go get \.: no package in current directory$'
! go get ./subdir
stderr '^go get: \.[/\\]subdir \('$WORK'[/\\]gopath[/\\]src[/\\]dir[/\\]subdir\) is not a package in module rooted at '$WORK'[/\\]gopath[/\\]src[/\\]dir$'
! go list
! stderr 'cannot find module providing package'
stderr '^no Go files in '$WORK'[/\\]gopath[/\\]src[/\\]dir$'