From 9d90f935ac84eddbac47eddf75c0204f2757eb0a Mon Sep 17 00:00:00 2001 From: Yoanm Date: Tue, 15 Nov 2022 20:18:02 +0100 Subject: [PATCH] go/doc: fix example generation for package prefixed with go- Trim go- prefix from package name (like it's done on runtime) before looking for related unresolved package. Fixes #56740 --- src/go/doc/example.go | 3 +++ src/go/doc/testdata/examples/issue56740.go | 17 +++++++++++++++++ src/go/doc/testdata/examples/issue56740.golden | 14 ++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 src/go/doc/testdata/examples/issue56740.go create mode 100644 src/go/doc/testdata/examples/issue56740.golden diff --git a/src/go/doc/example.go b/src/go/doc/example.go index 6d0459ec5bb..213748ea6aa 100644 --- a/src/go/doc/example.go +++ b/src/go/doc/example.go @@ -238,6 +238,9 @@ func playExample(file *ast.File, f *ast.FuncDecl) *ast.File { // We can't resolve dot imports (yet). return nil } + } else { + // Trim 'go-' prefix from package name (like done at runtime) to ensure match + n = strings.TrimPrefix(n, "go-") } if unresolved[n] { // Copy the spec and its path to avoid modifying the original. diff --git a/src/go/doc/testdata/examples/issue56740.go b/src/go/doc/testdata/examples/issue56740.go new file mode 100644 index 00000000000..d5bf11af1c3 --- /dev/null +++ b/src/go/doc/testdata/examples/issue56740.go @@ -0,0 +1,17 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package foo_test + +import ( + "fmt" + + "github.com/xxx/go-foo" +) + +func ExampleMyMethod() { + fmt.Println(foo.MyMethod()) + // Output: + // expected output +} diff --git a/src/go/doc/testdata/examples/issue56740.golden b/src/go/doc/testdata/examples/issue56740.golden new file mode 100644 index 00000000000..919d7f4a4ca --- /dev/null +++ b/src/go/doc/testdata/examples/issue56740.golden @@ -0,0 +1,14 @@ +-- MyMethod.Play -- +package main + +import ( + "fmt" + + "github.com/xxx/go-foo" +) + +func main() { + fmt.Println(foo.MyMethod()) +} +-- MyMethod.Output -- +expected output