Skip to content

fix(encoding/json): block encoding/json/v2 at compile time - #529

Merged
paralin merged 2 commits into
s4wave:masterfrom
cogentcore:fix/block-json-v2
Oct 24, 2026
Merged

paralin merged 2 commits into
s4wave:masterfrom
cogentcore:fix/block-json-v2

Conversation

@rcoreilly

Copy link
Copy Markdown
Contributor

encoding/json/v2 is compiled from Go source, but it uses the internals of encoding/json/jsontext, whose override implements only Value and the formatting options. jsontext.Internal is null there, so any program that imports encoding/json/v2 panics with "invalid memory address or nil pointer dereference" while the package initializes (var export = jsontext.Internal.Export(...) in arshal.go), before main runs. Programs can reach it without importing it themselves, through generated symbol tables such as yaegi's standard library symbols for Go 1.27; that is how a Cogent Core web app hit it.

I first tried dropping the jsontext override and compiling jsontext from source (listed as a nativeDependency of encoding/json, so RawMessage keeps working). With #526, #527 and #528, json/v2 then works and matches native Go. But every program that uses encoding/json would compile jsontext and its internal packages: for a small Marshal/Unmarshal program the compiled output grew from 1.26 MB to 1.91 MB, and because jsontext.pointerSuffixError embeds error, its promoted Error method is async, which makes fmt calls with interface arguments async as well. That seemed too high a price for programs that never use json/v2.

So this adds an override for encoding/json/v2 whose strict parity ledger blocks every export. A program that uses one now fails to compile with parity-blocked-use, naming the symbol, instead of failing at run time. TestOverrideParityVerifierBlocksJSONV2 compiles a real json/v2 program and checks for that diagnostic (it fails without this change). Two compliance outputs gain an erased type-only import of encoding/json/v2, because encoding/json.Unmarshaler is an alias of jsonv2.Unmarshaler since Go 1.27; they still pass.

encoding/json/v2 is compiled from Go source, but it uses the internals of
encoding/json/jsontext, whose override implements only Value and the
formatting options: jsontext.Internal is null there, so a program that
imports encoding/json/v2 panicked with a nil pointer dereference while
the package initialized, before main ran. Programs reach it through
generated symbol tables too, such as those of an interpreter.

Compiling jsontext from source instead makes json/v2 work, but every
program that uses encoding/json would then compile jsontext and its
internal packages, and its error types make fmt calls async.

Add an override for encoding/json/v2 whose parity ledger blocks every
export, so a program that uses one fails to compile with
parity-blocked-use instead of failing at run time. Two compliance
outputs gain an erased type-only import, since encoding/json.Unmarshaler
is an alias into encoding/json/v2 since Go 1.27.

Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
encoding/json.Marshaler and encoding/json.Unmarshaler are aliases of the
encoding/json/v2 types since Go 1.27, so packages compiled from source
that use them, such as html/template, refer to v2.Marshaler and import
encoding/json/v2. The blocked override exported no types, and it was not
copied into the output unless a program imported it, so such programs
failed type checking and bundling ("Could not resolve
@goscript/encoding/json/v2/index.js").

Export Marshaler and Unmarshaler from the encoding/json/v2 override, with
the methods of the encoding/json ones, classify them as real, and list
encoding/json/v2 as a dependency of the encoding/json override so that
it is always copied. Add a compliance test using html/template.

Signed-off-by: Randall C. O'Reilly <oreilly@ucdavis.edu>
@rcoreilly

Copy link
Copy Markdown
Contributor Author

Pushed a follow-up commit: encoding/json.Marshaler and Unmarshaler are aliases of the v2 types since Go 1.27, so packages compiled from source that use them (html/template, for one) refer to v2.Marshaler and import encoding/json/v2. With every export blocked and the package not copied into the output, such programs failed type checking and bundling. The v2 override now exports those two interfaces (classified real), encoding/json lists encoding/json/v2 as a dependency so it is always copied, and a compliance test (package_import_json_marshaler_alias) uses html/template.

@paralin
paralin merged commit ebdd983 into s4wave:master Oct 24, 2026
7 checks passed
@rcoreilly
rcoreilly deleted the fix/block-json-v2 branch October 24, 2026 22:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants