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

proposal: cmd/test2json: provide stderr flag for output #61856

Closed
goto1134 opened this issue Aug 8, 2023 · 5 comments
Closed

proposal: cmd/test2json: provide stderr flag for output #61856

goto1134 opened this issue Aug 8, 2023 · 5 comments
Labels
Milestone

Comments

@goto1134
Copy link

goto1134 commented Aug 8, 2023

Running the following test

package main

import (
	"testing"
)

func Test(t *testing.T) {
	panic(1)
}

either with go test -json or with go test -c && go tool test2json pkg.test -test.v produces json lines with the panic stacktrace. It is unclear thought whether the output is stdout or stderr from the json:

{"Time":"2023-08-08T20:00:03.64831+02:00","Action":"start"}
{"Time":"2023-08-08T20:00:03.727661+02:00","Action":"output","Test":"Test","Output":"--- FAIL: Test (0.00s)\n"}
{"Time":"2023-08-08T20:00:03.730158+02:00","Action":"output","Test":"Test","Output":"panic: 1 [recovered]\n"}
{"Time":"2023-08-08T20:00:03.730162+02:00","Action":"output","Test":"Test","Output":"\tpanic: 1\n"}
{"Time":"2023-08-08T20:00:03.730163+02:00","Action":"output","Test":"Test","Output":"\n"}
{"Time":"2023-08-08T20:00:03.730165+02:00","Action":"output","Test":"Test","Output":"goroutine 6 [running]:\n"}
{"Time":"2023-08-08T20:00:03.730166+02:00","Action":"output","Test":"Test","Output":"testing.tRunner.func1.2({0x100c58e80, 0x100c48598})\n"}
{"Time":"2023-08-08T20:00:03.730172+02:00","Action":"output","Test":"Test","Output":"\t/opt/homebrew/opt/go/libexec/src/testing/testing.go:1526 +0x1c8\n"}
{"Time":"2023-08-08T20:00:03.730176+02:00","Action":"output","Test":"Test","Output":"testing.tRunner.func1()\n"}
{"Time":"2023-08-08T20:00:03.730178+02:00","Action":"output","Test":"Test","Output":"\t/opt/homebrew/opt/go/libexec/src/testing/testing.go:1529 +0x384\n"}
{"Time":"2023-08-08T20:00:03.740272+02:00","Action":"output","Test":"Test","Output":"panic({0x100c58e80, 0x100c48598})\n"}
{"Time":"2023-08-08T20:00:03.740282+02:00","Action":"output","Test":"Test","Output":"\t/opt/homebrew/opt/go/libexec/src/runtime/panic.go:884 +0x204\n"}
{"Time":"2023-08-08T20:00:03.740295+02:00","Action":"output","Test":"Test","Output":"awesomeProject3.Test(0x0?)\n"}
{"Time":"2023-08-08T20:00:03.740297+02:00","Action":"output","Test":"Test","Output":"\t/Users/Andrei.Efanov/Projects/chore/awesomeProject3/sample_test.go:8 +0x2c\n"}
{"Time":"2023-08-08T20:00:03.740305+02:00","Action":"output","Test":"Test","Output":"testing.tRunner(0x14000003a00, 0x100c7b9d8)\n"}
{"Time":"2023-08-08T20:00:03.740306+02:00","Action":"output","Test":"Test","Output":"\t/opt/homebrew/opt/go/libexec/src/testing/testing.go:1576 +0x10c\n"}
{"Time":"2023-08-08T20:00:03.740308+02:00","Action":"output","Test":"Test","Output":"created by testing.(*T).Run\n"}
{"Time":"2023-08-08T20:00:03.740845+02:00","Action":"output","Test":"Test","Output":"\t/opt/homebrew/opt/go/libexec/src/testing/testing.go:1629 +0x368\n"}
{"Time":"2023-08-08T20:00:03.740883+02:00","Action":"fail","Test":"Test","Elapsed":0.093}

Writing tools to present the output, I would like to distinguish stderr to paint it in a different color in the user console. Please consider adding a flag to the output json event.

@gopherbot gopherbot added this to the Proposal milestone Aug 8, 2023
@goto1134 goto1134 changed the title proposal: cmd/internal/test2json: provide stderr flag for output proposal: cmd/test2json: provide stderr flag for output Aug 8, 2023
@rsc
Copy link
Contributor

rsc commented Aug 9, 2023

This is not feasible in general. The problem is that you can have either (A) stdout and stderr merged into one, preserving the relative ordering of writes to stdout and writes to stderr, or (B) separate stdout and stderr, losing information about the relative ordering of individual messages. Go has chosen (A), because often programs are not terribly consistent about stdout vs stderr and often you care a lot about not reordering the relative outputs.

There is not stdout and stderr; there is just "Output".

@goto1134
Copy link
Author

goto1134 commented Aug 9, 2023

Makes sense, thank you.

At first I was confused with the current behaviour, since despite having a single output channel, the channel is different for different tools. For example, running a go program or a compiled test, everything printed with println or panic gets into stderr by default. But for the tools like test2json, the output channel is stdout. And since I never tried to output anything meaningful with println, I saw panics in stderr and thought go uses both stderr and stdout.

What is the reason for having stderr by default and stdout for test2json?

@rsc
Copy link
Contributor

rsc commented Aug 9, 2023

This proposal has been added to the active column of the proposals project
and will now be reviewed at the weekly proposal review meetings.
— rsc for the proposal review group

@rsc
Copy link
Contributor

rsc commented Aug 16, 2023

What is the reason for having stderr by default and stdout for test2json?

Mostly to separate the other non-JSON error output that the go command can print.

@rsc
Copy link
Contributor

rsc commented Aug 16, 2023

This proposal has been declined as infeasible.
— rsc for the proposal review group

@rsc rsc closed this as completed Aug 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Declined
Development

No branches or pull requests

3 participants