]> Cypherpunks repositories - gostls13.git/commitdiff
go/build: correct value of .Doc field
authorjimmyfrasche <soapboxcicero@gmail.com>
Sat, 24 Feb 2018 23:03:58 +0000 (15:03 -0800)
committerIan Lance Taylor <iant@golang.org>
Wed, 7 Mar 2018 14:35:52 +0000 (14:35 +0000)
Build could use the package comment from test files to populate the .Doc
field on *Package.

As go list uses this data and several packages in the standard library
have tests with package comments, this lead to:

$ go list -f '{{.Doc}}' flag container/heap image
These examples demonstrate more intricate uses of the flag package.
This example demonstrates an integer heap built using the heap interface.
This example demonstrates decoding a JPEG image and examining its pixels.

This change now only examines non-test files when attempting to populate
.Doc, resulting in the expected behavior:

$ gotip list -f '{{.Doc}}' flag container/heap image
Package flag implements command-line flag parsing.
Package heap provides heap operations for any type that implements heap.Interface.
Package image implements a basic 2-D image library.

Fixes #23594

Change-Id: I37171c26ec5cc573efd273556a05223c6f675968
Reviewed-on: https://go-review.googlesource.com/96976
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/go/build/build.go
src/go/build/build_test.go
src/go/build/testdata/doc/a_test.go [new file with mode: 0644]
src/go/build/testdata/doc/b_test.go [new file with mode: 0644]
src/go/build/testdata/doc/c_test.go [new file with mode: 0644]
src/go/build/testdata/doc/d_test.go [new file with mode: 0644]
src/go/build/testdata/doc/e.go [new file with mode: 0644]
src/go/build/testdata/doc/f.go [new file with mode: 0644]

index 6991e585c386fb18adc76bb6c301bba521dbc943..30b5283400490ad8c3c48a9e39c081c32087d309 100644 (file)
@@ -813,7 +813,8 @@ Found:
                        })
                        p.InvalidGoFiles = append(p.InvalidGoFiles, name)
                }
-               if pf.Doc != nil && p.Doc == "" {
+               // Grab the first package comment as docs, provided it is not from a test file.
+               if pf.Doc != nil && p.Doc == "" && !isTest && !isXTest {
                        p.Doc = doc.Synopsis(pf.Doc.Text())
                }
 
index ac5d2c3bb98d3ac8c84bd1cdae014cd130b47dfb..cb2ae3c77597c422c4895158388a53599e5d44d3 100644 (file)
@@ -395,3 +395,19 @@ func TestImportDirTarget(t *testing.T) {
                t.Errorf("p.PkgTargetRoot == %q, p.PkgObj == %q, want non-empty", p.PkgTargetRoot, p.PkgObj)
        }
 }
+
+// TestIssue23594 prevents go/build from regressing and populating Package.Doc
+// from comments in test files.
+func TestIssue23594(t *testing.T) {
+       // Package testdata/doc contains regular and external test files
+       // with comments attached to their package declarations. The names of the files
+       // ensure that we see the comments from the test files first.
+       p, err := ImportDir("testdata/doc", 0)
+       if err != nil {
+               t.Fatalf("could not import testdata: %v", err)
+       }
+
+       if p.Doc != "Correct" {
+               t.Fatalf("incorrectly set .Doc to %q", p.Doc)
+       }
+}
diff --git a/src/go/build/testdata/doc/a_test.go b/src/go/build/testdata/doc/a_test.go
new file mode 100644 (file)
index 0000000..1c07b56
--- /dev/null
@@ -0,0 +1,2 @@
+// Doc from xtests
+package doc_test
diff --git a/src/go/build/testdata/doc/b_test.go b/src/go/build/testdata/doc/b_test.go
new file mode 100644 (file)
index 0000000..0cf1605
--- /dev/null
@@ -0,0 +1 @@
+package doc_test
diff --git a/src/go/build/testdata/doc/c_test.go b/src/go/build/testdata/doc/c_test.go
new file mode 100644 (file)
index 0000000..1025707
--- /dev/null
@@ -0,0 +1 @@
+package doc
diff --git a/src/go/build/testdata/doc/d_test.go b/src/go/build/testdata/doc/d_test.go
new file mode 100644 (file)
index 0000000..ec19564
--- /dev/null
@@ -0,0 +1,2 @@
+// Doc from regular tests.
+package doc
diff --git a/src/go/build/testdata/doc/e.go b/src/go/build/testdata/doc/e.go
new file mode 100644 (file)
index 0000000..1025707
--- /dev/null
@@ -0,0 +1 @@
+package doc
diff --git a/src/go/build/testdata/doc/f.go b/src/go/build/testdata/doc/f.go
new file mode 100644 (file)
index 0000000..ab1d0bc
--- /dev/null
@@ -0,0 +1,2 @@
+// Correct
+package doc