go-chi/docgen

Go compiler optimizations are affecting docgen.GetFuncInfo() in some cases

e-nikolov opened this issue · 0 comments

package chai_test

import (
	"fmt"

	"github.com/go-chi/docgen"
)

//Comment
func Simple() string {
	return "hello"
}

func ExampleGetFuncInfo() {
	fmt.Println(docgen.GetFuncInfo(Simple).Comment)
	// Output:
	// Comment
}

https://github.com/go-chai/chai/blob/main/openapi2/funcinfo_test.go

This example fails if I run it normally via

go test ./ -run ExampleGetFuncInfo

--- FAIL: ExampleGetFuncInfo (0.00s)
got:

want:
Comment
FAIL
FAIL    github.com/go-chai/chai 0.003s
FAIL

But if I disable the go compiler optimizations by passing -gcflags '-N' to the go run command, it succeeds:

go test -gcflags '-N' ./ -run ExampleGetFuncInfo

ok      github.com/go-chai/chai 0.003s

I noticed that the line reported by GetFuncInfo is the line of the return rather than the line of the function signature. I suspect that it happens because of the function getting inlined or some other optimization that has to do with escape analysis, which in turn causes issues for the caller frames that GetFuncInfo relies on.