Allow JSONC in .buildifier.json config parsing - #1483
Conversation
Use hujson to parse JWCC/JSONC so buildifier configs can include comments and trailing commas, matching editor JSONC support.
There was a problem hiding this comment.
Code Review
This pull request introduces support for JSON with comments (JSONC) and trailing commas in the configuration parser by integrating the hujson library. It includes the helper implementation, unit tests, and necessary dependency updates. The reviewer suggested wrapping the error returned by json.Unmarshal in config.go to maintain consistency with other error-wrapping patterns in the same function.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| if err := json.Unmarshal(data, c); err != nil { | ||
| return err | ||
| } |
There was a problem hiding this comment.
For consistency with the other error wrapping in this function (reading config and parsing config), the error returned by json.Unmarshal should also be wrapped to provide clear context if unmarshaling fails.
| if err := json.Unmarshal(data, c); err != nil { | |
| return err | |
| } | |
| if err := json.Unmarshal(data, c); err != nil { | |
| return fmt.Errorf("unmarshaling config: %w", err) | |
| } |
References
- When modifying code, ensure that changes are consistent with previous behavior, especially if the previous behavior was intentional.
Summary
.buildifier.jsonas JWCC/JSONC usinggithub.com/tailscale/hujson, so configs can include//and/* */comments and trailing commas.Justification
Editors and many tooling ecosystems treat
.jsonconfig files as JSONC (VS Code, Cursor, etc.), but buildifier previously usedencoding/jsondirectly and rejected any comments. That mismatch forces users to choose between documented/annotated configs and a config that buildifier will actually load.Using hujson gives spec-compliant JWCC parsing (RFC 8259 JSON plus comments and trailing commas) instead of a hand-rolled comment stripper, which is safer around strings containing
//or/*.Test plan
bazel test //buildifier/config:config_test.buildifier.jsonwith inline comments loads via--config