From: jimmyfrasche Date: Sat, 24 Feb 2018 23:03:58 +0000 (-0800) Subject: go/build: correct value of .Doc field X-Git-Tag: go1.11beta1~1311 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=20b14b71df6aaf044d3e78920a5f56bc20dd2a49;p=gostls13.git 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í TryBot-Result: Gobot Gobot Reviewed-by: Daniel Martí Reviewed-by: Ian Lance Taylor --- 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