Skip to content

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

@e-nikolov

Description

@e-nikolov
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions