diff options
author | jimmyfrasche <soapboxcicero@gmail.com> | 2018-02-24 15:03:58 -0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2018-03-07 14:35:52 +0000 |
commit | 20b14b71df6aaf044d3e78920a5f56bc20dd2a49 (patch) | |
tree | 767d2499e609fa6abba458fe156ece119b2e4cc2 | |
parent | ee465831eccef9d8380a0cbfbb526684399d35eb (diff) | |
download | go-git-20b14b71df6aaf044d3e78920a5f56bc20dd2a49.tar.gz |
go/build: correct value of .Doc field
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>
-rw-r--r-- | src/go/build/build.go | 3 | ||||
-rw-r--r-- | src/go/build/build_test.go | 16 | ||||
-rw-r--r-- | src/go/build/testdata/doc/a_test.go | 2 | ||||
-rw-r--r-- | src/go/build/testdata/doc/b_test.go | 1 | ||||
-rw-r--r-- | src/go/build/testdata/doc/c_test.go | 1 | ||||
-rw-r--r-- | src/go/build/testdata/doc/d_test.go | 2 | ||||
-rw-r--r-- | src/go/build/testdata/doc/e.go | 1 | ||||
-rw-r--r-- | src/go/build/testdata/doc/f.go | 2 |
8 files changed, 27 insertions, 1 deletions
diff --git a/src/go/build/build.go b/src/go/build/build.go index 6991e585c3..30b5283400 100644 --- a/src/go/build/build.go +++ b/src/go/build/build.go @@ -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()) } diff --git a/src/go/build/build_test.go b/src/go/build/build_test.go index ac5d2c3bb9..cb2ae3c775 100644 --- a/src/go/build/build_test.go +++ b/src/go/build/build_test.go @@ -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 index 0000000000..1c07b56360 --- /dev/null +++ b/src/go/build/testdata/doc/a_test.go @@ -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 index 0000000000..0cf1605ef3 --- /dev/null +++ b/src/go/build/testdata/doc/b_test.go @@ -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 index 0000000000..1025707079 --- /dev/null +++ b/src/go/build/testdata/doc/c_test.go @@ -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 index 0000000000..ec19564eb3 --- /dev/null +++ b/src/go/build/testdata/doc/d_test.go @@ -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 index 0000000000..1025707079 --- /dev/null +++ b/src/go/build/testdata/doc/e.go @@ -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 index 0000000000..ab1d0bc935 --- /dev/null +++ b/src/go/build/testdata/doc/f.go @@ -0,0 +1,2 @@ +// Correct +package doc |