Standardize modifies its input slice; the doc comment does not say so
Assessment
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Newbie friendliness
- 60/100
- Issue type
- Documentation
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- go
- Domain
- api, documentation
Research direction
Start with the doc comments on Standardize, Minimize and Format in the hujson package, and with how Parse keeps slices of b that Value.Standardize writes into. Run the repro in the issue to confirm that the input buffer changes on success. Done means the doc comment says that b may be modified, and the other two functions are covered too. Whether to copy the input instead is a maintainer decision, so check with them first.
Written by the indexing model from the issue text.
Description
hujson.Standardize(b) changes the caller's buffer b. The doc comment says the result is returned and that b is returned as is on error, but it does not say that b itself is modified on success. A caller that keeps b (for example to write the original file back unchanged) loses its comments.
Version: github.com/tailscale/hujson v0.0.0-20260727124030-b80ff77dac4f, Go 1.27.
Repro:
package main
import (
"fmt"
"github.com/tailscale/hujson"
)
func main() {
in := []byte(`{"a": 1, /* c */ "b": 2,}`)
before := string(in)
out, err := hujson.Standardize(in)
fmt.Printf("err=%v\nbefore=%q\nafter =%q\nout =%q\n", err, before, string(in), string(out))
}
Output:
err=<nil>
before="{\"a\": 1, /* c */ \"b\": 2,}"
after ="{\"a\": 1, \"b\": 2,}"
out ="{\"a\": 1, \"b\": 2 }"
The comment in in is replaced with spaces (the trailing comma in in is not). This is because Parse keeps slices of b and Value.Standardize writes into them before Pack.
Suggested fix, either:
- document it: "Standardize may modify b; pass a copy if b is still needed", or
- work on a copy:
ast, err := Parse(bytes.Clone(b)).
The same likely applies to Minimize and Format.
Found in Ambit (https://github.com/craftsman-apps/ambit), which edits users' JSONC config files and must keep the original bytes; we now pass a copy.
- Dominant language
- Go
- Stars
- 876
- Forks
- 35
- PR merge metrics
- No merged PRs in 30d
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tailscale/hujson
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
Value.Patch: "add" with an out-of-range array index appends instead of failingPossibly taken @SashaMIT claimed this 4 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Value.Patch: "test" operation always fails for a null valuePossibly taken @RaphaelFakhri claimed this 6 days ago. Open
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
-
Patching with commentsPossibly taken @Shubham-Padkonde claimed this 19 days ago. Open
Difficulty 4/5 3-5 days Newbie friendliness 45/100
-
Version TagOpen
Difficulty 2/5 1-3 hours Newbie friendliness 55/100
All issues in tailscale/hujson
Similar issues
-
bug
Difficulty 2/5 Half a day Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
kind/bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
kubernetes-sigs/external-dns#6816 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
ANSI translator ignores SGR 22/24 (and other attribute-off codes): bold and underline never turn offOpen
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
Named Tuple element with a space in its name fails to parsePossibly taken @spicysharkco claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
ClickHouse/clickhouse-go#2073 ·
Maintainers usually reply within 1 day