all: fix JSON serialization under Go 1.27's finalized encoding/json/v2

Go 1.27 enables GOEXPERIMENT=jsonv2 by default: encoding/json is now
backed by the json/v2 machinery, and github.com/go-json-experiment/json
compiles as a thin alias of the standard library's encoding/json/v2.
Several tag options and behaviors we relied on did not make the cut for
the final Go 1.27 API, breaking tailscaled at runtime and four packages'
tests. This change adapts to the final API while keeping the wire format
byte-for-byte identical on all Go versions.

First, the `format` tag option was demoted to experimental. Its mere
presence in a struct tag now makes marshaling and unmarshaling fail at
runtime. tailcfg.SSHAction.SessionDuration had `format:nano` (added in
a2dc517d7 to pin the v1 representation), so on Go 1.27 any netmap
containing an SSH policy failed to decode, breaking every PollNetMap.
Remove the option here and in net/speedtest; time.Duration still
marshals as int64 nanoseconds under encoding/json on all Go versions
(Go 1.27's v1 mode sets FormatDurationAsNano by default), so old
clients and servers are unaffected. Add a regression test locking in
the exact wire format.

Consequently, invert the cmd/vet jsontags rule: it previously required
an explicit `format` tag on time.Duration fields, which is now exactly
wrong. It now rejects any `format` tag option, which would have caught
this bug in CI.

Second, the `inline` tag option was renamed to `embed`. The standard
library silently ignores `inline`, while the pinned go-json-experiment
module (used on Go 1.26) only knows `inline`. Specify both options in
types/prefs and logtail; each implementation ignores the option it does
not know, producing identical output. Drop `inline` once we require
Go 1.27.

Third, encoding/json (v1) now dispatches to MarshalJSONTo and
UnmarshalJSONFrom methods and its v1 options flow into nested
jsonv2.MarshalEncode calls. Types whose v1 methods deliberately
routed through jsonv2 for v2 semantics (types/opt.Value, the
types/prefs preference types) would silently change wire format
(e.g. nil slices becoming null). Pin jsonv2.DefaultOptionsV2 in
their jsonv2 methods so the representation is the same regardless
of the entry point.

Finally, json.Marshal costs one more allocation under Go 1.27,
tripping the types/logger.AsJSON alloc test. Switch its fmt.Formatter
to jsonv2.MarshalWrite with explicit v1 options, which writes directly
to the fmt.State: one allocation on both toolchains with unchanged
output. Depaware files pick up the go-json-experiment/json/v1 options
shim as a new dependency of types/logger.

With this change, go test ./... passes with both Go 1.26.5 and
go1.27rc2.

Updates #20220
Fixes #20528
Fixes #20254

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I694c7d57fd81e55a579c579e9be10032bca569d4
This commit is contained in:
Brad Fitzpatrick
2026-07-19 09:58:50 -07:00
committed by Brad Fitzpatrick
parent ece1b12ebf
commit 82cfea90ca
19 changed files with 100 additions and 46 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ tailscale.com/cmd/derper dependencies: (generated by github.com/tailscale/depawa
github.com/go-json-experiment/json/internal/jsonopts from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/internal/jsonwire from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/jsontext from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/v1 from tailscale.com/net/routecheck
github.com/go-json-experiment/json/v1 from tailscale.com/net/routecheck+
💣 github.com/go4org/hashtriemap from tailscale.com/derp/derpserver
github.com/golang/groupcache/lru from tailscale.com/net/dnscache
github.com/hdevalence/ed25519consensus from tailscale.com/tka
+1 -1
View File
@@ -41,7 +41,7 @@ tailscale.com/cmd/k8s-operator dependencies: (generated by github.com/tailscale/
github.com/go-json-experiment/json/internal/jsonopts from github.com/go-json-experiment/json/jsontext+
github.com/go-json-experiment/json/internal/jsonwire from github.com/go-json-experiment/json/jsontext+
github.com/go-json-experiment/json/jsontext from tailscale.com/logtail+
github.com/go-json-experiment/json/v1 from tailscale.com/net/routecheck
github.com/go-json-experiment/json/v1 from tailscale.com/net/routecheck+
github.com/go-logr/logr from github.com/go-logr/logr/slogr+
github.com/go-logr/logr/slogr from github.com/go-logr/zapr
github.com/go-logr/zapr from sigs.k8s.io/controller-runtime/pkg/log/zap+
+1
View File
@@ -9,6 +9,7 @@ tailscale.com/cmd/stund dependencies: (generated by github.com/tailscale/depawar
github.com/go-json-experiment/json/internal/jsonopts from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/internal/jsonwire from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/jsontext from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/v1 from tailscale.com/types/logger
github.com/munnerz/goautoneg from github.com/prometheus/common/expfmt
💣 github.com/prometheus/client_golang/prometheus from tailscale.com/tsweb/promvarz
github.com/prometheus/client_golang/prometheus/internal from github.com/prometheus/client_golang/prometheus
+1 -1
View File
@@ -125,7 +125,7 @@ tailscale.com/cmd/tailscale dependencies: (generated by github.com/tailscale/dep
github.com/go-json-experiment/json/internal/jsonopts from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/internal/jsonwire from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/jsontext from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/v1 from tailscale.com/net/routecheck
github.com/go-json-experiment/json/v1 from tailscale.com/net/routecheck+
L 💣 github.com/godbus/dbus/v5 from fyne.io/systray+
L github.com/godbus/dbus/v5/introspect from fyne.io/systray+
L github.com/godbus/dbus/v5/prop from fyne.io/systray
+1
View File
@@ -15,6 +15,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
github.com/go-json-experiment/json/internal/jsonopts from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/internal/jsonwire from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/jsontext from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/v1 from tailscale.com/types/logger
github.com/golang/groupcache/lru from tailscale.com/net/dnscache
💣 github.com/jsimonetti/rtnetlink from tailscale.com/net/netmon
github.com/jsimonetti/rtnetlink/internal/unix from github.com/jsimonetti/rtnetlink
+1
View File
@@ -15,6 +15,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
github.com/go-json-experiment/json/internal/jsonopts from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/internal/jsonwire from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/jsontext from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/v1 from tailscale.com/types/logger
github.com/golang/groupcache/lru from tailscale.com/net/dnscache
💣 github.com/jsimonetti/rtnetlink from tailscale.com/net/netmon
github.com/jsimonetti/rtnetlink/internal/unix from github.com/jsimonetti/rtnetlink
+1 -1
View File
@@ -30,7 +30,7 @@ tailscale.com/cmd/tsidp dependencies: (generated by github.com/tailscale/depawar
github.com/go-json-experiment/json/internal/jsonopts from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/internal/jsonwire from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/jsontext from github.com/go-json-experiment/json+
github.com/go-json-experiment/json/v1 from tailscale.com/net/routecheck
github.com/go-json-experiment/json/v1 from tailscale.com/net/routecheck+
L 💣 github.com/godbus/dbus/v5 from tailscale.com/net/dns
github.com/golang/groupcache/lru from tailscale.com/net/dnscache
github.com/google/btree from gvisor.dev/gvisor/pkg/tcpip/transport/tcp
+11 -26
View File
@@ -38,7 +38,7 @@ func run(pass *analysis.Pass) (any, error) {
if tag == "" {
continue
}
var seenName, hasFormat bool
var seenName bool
for opt := range strings.SplitSeq(tag, ",") {
if !seenName {
seenName = true
@@ -116,13 +116,18 @@ func run(pass *analysis.Pass) (any, error) {
report(pass, structType, fieldVar, StringOnNonNumericKind)
}
default:
key, _, ok := strings.Cut(opt, ":")
hasFormat = key == "format" && ok
// The `format` tag option did not make the cut for
// Go 1.27's encoding/json/v2. Its mere presence in a
// struct tag makes both v1 and v2 marshaling fail at
// runtime unless the caller passes the
// jsonv2.ExperimentalSupportFormatTag option.
// See https://go.dev/issue/71631 and
// https://github.com/tailscale/tailscale/issues/20528.
if key, _, ok := strings.Cut(opt, ":"); ok && key == "format" {
report(pass, structType, fieldVar, FormatUnsupported)
}
}
}
if !hasFormat && isTimeDuration(mayPointerElem(fieldVar.Type())) {
report(pass, structType, fieldVar, FormatMissingOnTimeDuration)
}
}
})
return nil, nil
@@ -152,26 +157,6 @@ func isString(t types.Type) bool {
return ok && basic.Kind() == types.String
}
// isTimeDuration reports whether t is a time.Duration type.
func isTimeDuration(t types.Type) bool {
return isNamed(t, "time", "Duration")
}
// mayPointerElem returns the pointed-at type if t is a pointer,
// otherwise it returns t as-is.
func mayPointerElem(t types.Type) types.Type {
if pointer, ok := t.(*types.Pointer); ok {
return pointer.Elem()
}
return t
}
// isNamed reports t is a named typed of the given path and name.
func isNamed(t types.Type, path, name string) bool {
gotPath, gotName := typeName(t)
return gotPath == path && gotName == name
}
// typeName reports the pkgPath and name of the type.
// It recursively follows type aliases to get the underlying named type.
func typeName(t types.Type) (pkgPath, name string) {
+3 -3
View File
@@ -74,7 +74,7 @@ const (
OmitEmptyShouldBeOmitZero ReportKind = "OmitEmptyShouldBeOmitZero"
OmitEmptyShouldBeOmitZeroButHasIsZero ReportKind = "OmitEmptyShouldBeOmitZeroButHasIsZero"
StringOnNonNumericKind ReportKind = "StringOnNonNumericKind"
FormatMissingOnTimeDuration ReportKind = "FormatMissingOnTimeDuration"
FormatUnsupported ReportKind = "FormatUnsupported"
)
func (k ReportKind) message() string {
@@ -89,8 +89,8 @@ func (k ReportKind) message() string {
return "should probably use `omitzero` instead of `omitempty`"
case StringOnNonNumericKind:
return "must not use `string` on non-numeric types"
case FormatMissingOnTimeDuration:
return "must use an explicit `format` tag (e.g., `format:nano`) on a time.Duration type; see https://go.dev/issue/71631"
case FormatUnsupported:
return "must not use the `format` tag option; Go 1.27's encoding/json rejects it at runtime (see tailscale/tailscale#20528)"
default:
return string(k)
}