Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
e-nikolov opened this issue Dec 26, 2021 · 0 comments

Comments

@e-nikolov
Copy link

e-nikolov commented Dec 26, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant