golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
123.8k stars 17.64k forks source link

runtime: Frames example does not produce documented output #70057

Open jimmyfrasche opened 4 days ago

jimmyfrasche commented 4 days ago

Go version

go version go1.23.2 linux/amd64

Output of go env in your module/workspace:

Ran on the playground and locally

What did you do?

Run the example at https://pkg.go.dev/runtime#example-Frames

What did you see happen?

It outputs only - more:true | runtime.Callers

Commenting out the check that frame.File contains "runtime/" shows the missing frames and more.

What did you expect to see?

The same output that the example shows before it is run:

- more:true | runtime.Callers
- more:true | runtime_test.ExampleFrames.func1
- more:true | runtime_test.ExampleFrames.func2
- more:true | runtime_test.ExampleFrames.func3
- more:true | runtime_test.ExampleFrames
gabyhelp commented 4 days ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

adonovan commented 4 days ago

The issue here is that the formatting done to turn the code into an example changes its behavior: when run as a test, the stack includes these functions:

runtime.Callers
runtime_test.ExampleFrames.func1
runtime_test.ExampleFrames.func2
runtime_test.ExampleFrames.func3
runtime_test.ExampleFrames
testing.runExample
testing.runExamples
testing.(*M).Run
runtime_test.TestMain
main.main

but when the documentation Example is run as shown, the stack instead has these functions:

runtime.Callers
main.main.func1
main.main.func2
main.main.func3
main.main
runtime.main
runtime.goexit

Doing a replace-all from "main.main" to "runtime_test.ExampleFrames" before printing each line of output, and breaking after "main.main" is seen, should fix it.