go-chi / go-chi/docgen

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

Open
#18 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
127
Forks
27
PR merge metrics
No merged PRs in 30d

Description

```Go
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.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with openapi2/funcinfo_test.go and reproduce ExampleGetFuncInfo with and without `-gcflags '-N'`. Trace how docgen.GetFuncInfo determines the reported line under compiler optimizations; done means the example passes normally and identifies the function signature line rather than the return line.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.