]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: search for packages in the two-arg case
authorRob Pike <r@golang.org>
Mon, 28 Aug 2017 05:33:11 +0000 (15:33 +1000)
committerRob Pike <r@golang.org>
Mon, 28 Aug 2017 22:15:58 +0000 (22:15 +0000)
When given one argument, as in

go doc binary.BigEndian

doc would search for the package, but when given two, as in

go doc binary BigEndian

it would not. Fix the inconsistency.

Fixes #18697
Fixes #18664

Change-Id: Ib59dc483e8d4f91e6061c77a5ec24d0a50e115f0
Reviewed-on: https://go-review.googlesource.com/59413
Reviewed-by: Aliaksandr Valialkin <valyala@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/doc/doc_test.go
src/cmd/doc/main.go

index 89282529983d6d10fc00d9853d8f6532603ee817..ce0d77a44524955fb70798ee5f4d42447a00b592 100644 (file)
@@ -541,6 +541,36 @@ func TestMultiplePackages(t *testing.T) {
        }
 }
 
+// Test the code to look up packages when given two args. First test case is
+//     go doc binary BigEndian
+// This needs to find encoding/binary.BigEndian, which means
+// finding the package encoding/binary given only "binary".
+// Second case is
+//     go doc rand Float64
+// which again needs to find math/rand and not give up after crypto/rand,
+// which has no such function.
+func TestTwoArgLookup(t *testing.T) {
+       if testing.Short() {
+               t.Skip("scanning file system takes too long")
+       }
+       maybeSkip(t)
+       var b bytes.Buffer // We don't care about the output.
+       {
+               var flagSet flag.FlagSet
+               err := do(&b, &flagSet, []string{"binary", "BigEndian"})
+               if err != nil {
+                       t.Errorf("unexpected error %q from binary BigEndian", err)
+               }
+       }
+       {
+               var flagSet flag.FlagSet
+               err := do(&b, &flagSet, []string{"rand", "Float64"})
+               if err != nil {
+                       t.Errorf("unexpected error %q from rand Float64", err)
+               }
+       }
+}
+
 type trimTest struct {
        path   string
        prefix string
index 76c7dba2d95f57a589ec708b50a463aa2683e2f7..de275403a2189b8c05638637c8b3c29d34818695 100644 (file)
@@ -176,12 +176,12 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo
        case 1:
                // Done below.
        case 2:
-               // Package must be importable.
-               pkg, err := build.Import(args[0], "", build.ImportComment)
-               if err != nil {
-                       log.Fatalf("%s", err)
+               // Package must be findable and importable.
+               packagePath, ok := findPackage(args[0])
+               if !ok {
+                       log.Fatalf("no such package: %s", args[0])
                }
-               return pkg, args[0], args[1], false
+               return importDir(packagePath), args[0], args[1], true
        }
        // Usual case: one argument.
        arg := args[0]
@@ -230,7 +230,6 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo
                }
                // See if we have the basename or tail of a package, as in json for encoding/json
                // or ivy/value for robpike.io/ivy/value.
-               // Launch findPackage as a goroutine so it can return multiple paths if required.
                path, ok := findPackage(arg[0:period])
                if ok {
                        return importDir(path), arg[0:period], symbol, true