The standard logic for extracting "// Output:" comments in example tests is here:
https://cs.opensource.google/go/go/+/master:src/go/doc/example.go;l=113;drc=28f1bf61b7383bd4079d77090e67b3198b75be12
Observe that it has a ^
anchor so that it matches only the start of the comment text.
By contrast, pksite uses this regexp:
https://cs.opensource.google/go/x/pkgsite/+/master:internal/godoc/dochtml/internal/render/render.go;l=24;drc=7431aa5932c48bce425bf3f2dc368c5b1fed6e2c
which lacks the anchor. The consequence is that an example such as this one:
func Example() {
// A
// B This Example does not have an "// Output:" comment so it will not run.
// C
}
is considered executable by pkgsite, and its rendering of the example is abruptly cut off at line C.
Comment From: wdhongtw
This issue can be detected by adding one test case
diff --git a/internal/godoc/dochtml/internal/render/linkify_test.go b/internal/godoc/dochtml/internal/render/linkify_test.go
index d3ea8b83..7b455204 100644
--- a/internal/godoc/dochtml/internal/render/linkify_test.go
+++ b/internal/godoc/dochtml/internal/render/linkify_test.go
@@ -426,6 +426,18 @@ a := 1
// Output:
b := 1
</pre>
+`,
+ },
+ {
+ "Special \"Output\" comment must on it's own comment block",
+ `_ = true
+// To test output, use: // output: the output content
+`,
+ `
+<pre class="Documentation-exampleCode">
+_ = true
+// To test output, use: // output: the output content
+</pre>
`,
},
} {
And can be fixed quickly by ensuring there is only spaces before the //
sequence
```diff diff --git a/internal/godoc/dochtml/internal/render/render.go b/internal/godoc/dochtml/internal/render/render.go index 3875f3ba..0ad66c9e 100644 --- a/internal/godoc/dochtml/internal/render/render.go +++ b/internal/godoc/dochtml/internal/render/render.go @@ -21,7 +21,7 @@ import (
var (
// Regexp for example outputs.
- exampleOutputRx = regexp.MustCompile((?i)//[[:space:]]*(unordered )?output:
)
+ exampleOutputRx = regexp.MustCompile((?i)^[[:space:]]*//[[:space:]]*(unordered )?output:
)
)
type Renderer struct { ```
Since that there could only be space-like characters before the // Output:
block, assuming the the code is formatted.
@findleyr Would you think this issue deserve a fix? If so, maybe I can submit a PR for this. :D
Comment From: adonovan
@wdhongtw a fix would be much appreciated; thanks for offering.
Comment From: gopherbot
Change https://go.dev/cl/600415 mentions this issue: internal/godoc/dochtml: enhance output parsing for example func