Skip to content

Commit 3e7cb78

Browse files
aclementsgopherbot
authored andcommitted
cmd/go: re-enable build JSON from go test -json, now with GODEBUG
This re-enables the behavior of CL 536399 (by effectively reverting CL 628955), so now go test -json again includes build output and failures as JSON rather than text. However, since this behavior is clearly enough to trip up some build systems, this CL includes a GODEBUG=gotestjsonbuildtext that can be set to 1 to revert to the old behavior. Fixes #70402. Updates #62067. Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64_13,gotip-linux-amd64-longtest,gotip-windows-amd64-longtest Change-Id: I84e778cd844783dacfc83433e391b5ccb5925127 Reviewed-on: https://go-review.googlesource.com/c/go/+/629335 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Auto-Submit: Austin Clements <austin@google.com>
1 parent 5432cd9 commit 3e7cb78

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

doc/godebug.md

+8
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ For Go 1.24, it now defaults to multipathtcp="2", thus
168168
enabled by default on listerners. Using multipathtcp="0" reverts to the
169169
pre-Go 1.24 behavior.
170170

171+
Go 1.24 changed the behavior of `go test -json` to emit build errors as JSON
172+
instead of text.
173+
These new JSON events are distinguished by new `Action` values,
174+
but can still cause problems with CI systems that aren't robust to these events.
175+
This behavior can be controlled with the `gotestjsonbuildtext` setting.
176+
Using `gotestjsonbuildtext=1` restores the 1.23 behavior.
177+
This setting will be removed in a future release, Go 1.28 at the earliest.
178+
171179
### Go 1.23
172180

173181
Go 1.23 changed the channels created by package time to be unbuffered

doc/next/3-tools.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44

55
The `go build` and `go install` commands now accept a `-json` flag that reports
66
build output and failures as structured JSON output on standard output.
7-
Furthermore, passing `-json` to `go test` now reports build output and failures
8-
in addition to test results in JSON. For details of the reporting format, see
9-
`go help buildjson`.
7+
For details of the reporting format, see `go help buildjson`.
8+
9+
Furthermore, `go test -json` now reports build output and failures in JSON,
10+
interleaved with test result JSON.
11+
These are distinguished by new `Action` types, but if they cause problems in
12+
a test integration system, you can revert to the text build output by setting
13+
`GODEBUG=gotestjsonbuildtext=1`.
1014

1115
### Cgo {#cgo}
1216

src/cmd/go/internal/test/testflag.go

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ package test
66

77
import (
88
"cmd/go/internal/base"
9+
"cmd/go/internal/cfg"
910
"cmd/go/internal/cmdflag"
1011
"cmd/go/internal/work"
1112
"errors"
1213
"flag"
1314
"fmt"
15+
"internal/godebug"
1416
"os"
1517
"path/filepath"
1618
"strconv"
@@ -25,6 +27,8 @@ import (
2527
// our command line are for us, and some are for the test binary, and
2628
// some are for both.
2729

30+
var gotestjsonbuildtext = godebug.New("gotestjsonbuildtext")
31+
2832
func init() {
2933
work.AddBuildFlags(CmdTest, work.OmitVFlag|work.OmitJSONFlag)
3034

@@ -33,7 +37,6 @@ func init() {
3337
cf.StringVar(&testO, "o", "", "")
3438
work.AddCoverFlags(CmdTest, &testCoverProfile)
3539
cf.Var((*base.StringsFlag)(&work.ExecCmd), "exec", "")
36-
// TODO(austin): Make test -json imply build -json.
3740
cf.BoolVar(&testJSON, "json", false, "")
3841
cf.Var(&testVet, "vet", "")
3942

@@ -354,8 +357,11 @@ func testFlags(args []string) (packageNames, passToTest []string) {
354357
delete(addFromGOFLAGS, "v")
355358
delete(addFromGOFLAGS, "test.v")
356359

357-
// TODO(austin,#70402): Re-enable this once LUCI can handle build JSON in the test stream.
358-
//cfg.BuildJSON = true
360+
if gotestjsonbuildtext.Value() == "1" {
361+
gotestjsonbuildtext.IncNonDefault()
362+
} else {
363+
cfg.BuildJSON = true
364+
}
359365
}
360366

361367
// Inject flags from GOFLAGS before the explicit command-line arguments.

src/cmd/go/testdata/script/test_json_build.txt

+12-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
# TODO(austin,#70402)
2-
skip
3-
41
[short] skip
52

3+
env GODEBUG=gotestjsonbuildtext=0
4+
65
# There are several places where paths appear in JSON in regexps here.
76
# For the path separator we use (/|\\\\).
87
# Unfortunately, we can't just use ${/} because, while script test automatically
@@ -52,6 +51,16 @@ stdout '"Action":"output","Package":"m/veterror","Output":"FAIL\\tm/veterror \[b
5251
stdout '"Action":"fail","Package":"m/veterror","Elapsed":.*,"FailedBuild":"m/veterror \[m/veterror.test\]"'
5352
! stderr '.'
5453

54+
# Test that the GODEBUG fallback works.
55+
env GODEBUG=gotestjsonbuildtext=1
56+
! go test -json -o=$devnull ./builderror
57+
stderr '# m/builderror \[m/builderror.test\]\n'
58+
stderr 'builderror'${/}'main_test.go:3:11: undefined: y\n'
59+
stdout '"Action":"start","Package":"m/builderror"'
60+
stdout '"Action":"output","Package":"m/builderror","Output":"FAIL\\tm/builderror \[build failed\]\\n"'
61+
stdout '"Action":"fail","Package":"m/builderror","Elapsed":.*,"FailedBuild":"m/builderror \[m/builderror\.test\]"'
62+
63+
5564
-- go.mod --
5665
module m
5766
go 1.21

src/internal/godebugs/table.go

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ var All = []Info{
3030
{Name: "gocachehash", Package: "cmd/go"},
3131
{Name: "gocachetest", Package: "cmd/go"},
3232
{Name: "gocacheverify", Package: "cmd/go"},
33+
{Name: "gotestjsonbuildtext", Package: "cmd/go", Changed: 24, Old: "1"},
3334
{Name: "gotypesalias", Package: "go/types", Changed: 23, Old: "0"},
3435
{Name: "http2client", Package: "net/http"},
3536
{Name: "http2debug", Package: "net/http", Opaque: true},

src/runtime/metrics/doc.go

+5
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ Below is the full list of supported metrics, ordered lexicographically.
250250
The number of non-default behaviors executed by the cmd/go
251251
package due to a non-default GODEBUG=gocacheverify=... setting.
252252
253+
/godebug/non-default-behavior/gotestjsonbuildtext:events
254+
The number of non-default behaviors executed by the cmd/go
255+
package due to a non-default GODEBUG=gotestjsonbuildtext=...
256+
setting.
257+
253258
/godebug/non-default-behavior/gotypesalias:events
254259
The number of non-default behaviors executed by the go/types
255260
package due to a non-default GODEBUG=gotypesalias=... setting.

0 commit comments

Comments
 (0)