Currently, the methods of a interface are not linkable because Package.Parser
does not collect them up.
Thus, if you put a declaration like [Reader.Read]
within the io
package, the pkgsite
is not able to link to the Read
method.
It is unclear how to best fix this. Ideally interface methods appear in Type.Methods
, but historically they have not.
Adding them in there could break tools that are not expecting their appearance.
Comment From: gopherbot
Change https://go.dev/cl/523059 mentions this issue: internal/godoc/dochtml/internal/render: fix linking of interface methods
Comment From: peczenyj
Hey guys
I just note that links to public methods in a public interface are not linked correctly via godoc if it refers to the same package. Same for public fields:
notable examples
link to public method in type interface
https://pkg.go.dev/encoding/json@go1.22.4#MarshalerError
A MarshalerError represents an error from calling a [Marshaler.MarshalJSON] or encoding.TextMarshaler.MarshalText method.
// A MarshalerError represents an error from calling a
// [Marshaler.MarshalJSON] or [encoding.TextMarshaler.MarshalText] method.
type MarshalerError struct {
The rendering:
[Marshaler.MarshalJSON]
does not work, expected https://pkg.go.dev/encoding/json#Marshaler.MarshalJSON
[encoding.TextMarshaler.MarshalText]
=> work as expected, became https://pkg.go.dev/encoding#TextMarshaler.MarshalText
Another example: https://pkg.go.dev/io@go1.22.4#WriteString
link to public field in type struct
https://pkg.go.dev/archive/tar@go1.22.4#Reader.Read
// Calling Read on special types like [TypeLink], [TypeSymlink], [TypeChar],
// [TypeBlock], [TypeDir], and [TypeFifo] returns (0, [io.EOF]) regardless of what
// the [Header.Size] claims.
func (tr *Reader) Read(b []byte) (int, error) {
Calling Read on special types like TypeLink, TypeSymlink, TypeChar, TypeBlock, TypeDir, and TypeFifo returns (0, io.EOF) regardless of what the [Header.Size] claims.
The rendering:
[Header.Size]
does not work, expected https://pkg.go.dev/archive/tar#Header.Size
I can't find an example, but I think it may work from another package, like the previous issue in the public interface
link to builtin function defined on the current package:
https://pkg.go.dev/builtin@go1.22.4#recover
Prior to Go 1.21, recover would also return nil if panic is called with a nil argument. See [panic] for details.
The rendering:
[panic]
does not work, expected https://pkg.go.dev/builtin@go1.22.4#panic
let me ask: this issue #62293 also refer to such errors? or should I open another ticket?
This is kinda annoying BTW
Comment From: AlexanderYastrebov
There is a duplicate https://github.com/golang/go/issues/63679
Comment From: AlexanderYastrebov
It is unclear how to best fix this. Ideally interface methods appear in Type.Methods, but historically they have not. Adding them in there could break tools that are not expecting their appearance.
Alternatively, to fix links it should be enough to add interface methods into p.syms like:
index 4d01ae458b..192554ec2b 100644
--- a/src/go/doc/doc.go
+++ b/src/go/doc/doc.go
@@ -167,6 +167,7 @@ func (p *Package) collectTypes(types []*Type) {
p.collectValues(t.Vars)
p.collectFuncs(t.Funcs)
p.collectFuncs(t.Methods)
+ p.collectInterfaceMethods(t)
}
}
@@ -184,6 +185,21 @@ func (p *Package) collectFuncs(funcs []*Func) {
}
}
+func (p *Package) collectInterfaceMethods(t *Type) {
+ if t.Decl != nil && t.Decl.Tok == token.TYPE {
+ for _, s := range t.Decl.Specs {
+ s := s.(*ast.TypeSpec) // token.TYPE guarantees this
+ if i, ok := s.Type.(*ast.InterfaceType); ok {
+ for _, m := range i.Methods.List {
+ if len(m.Names) > 0 { // skip embedded interfaces
+ p.syms[t.Name+"."+m.Names[0].Name] = true
+ }
+ }
+ }
+ }
+ }
+}
+
Comment From: Juneezee
Related to https://github.com/golang/go/issues/54033
Comment From: willfaught
Struct fields are missing too, e.g. [Server.Addr]
in net/http doc.