]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go/internal/modget: clarify error for 'go get' without arguments
authorJay Conrod <jayconrod@google.com>
Wed, 20 May 2020 17:51:53 +0000 (13:51 -0400)
committerJay Conrod <jayconrod@google.com>
Tue, 17 Nov 2020 15:10:45 +0000 (15:10 +0000)
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>

src/cmd/go/internal/modget/get.go
src/cmd/go/internal/modget/query.go
src/cmd/go/testdata/script/mod_dot.txt

index 5b8eebf7cb20d894a928f6f676b59ff225f21f6e..0b7f6bf1d587dcc071b513389a02d8f107011f7a 100644 (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()))
                                }
index 53b60cc71a0ec4c9528344a1dc9ee34af4b6c92f..20eb0b6364ee0715af197d9862ca5944a9fcd89f 100644 (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) {
index 72be61279999b61a2fdc472cdaae893ecadeabda..ca8d5c6cc2d62d9ebd1a64f72efdfaae15d22cf4 100644 (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$'