Skip to content

Allow JSONC in .buildifier.json config parsing - #1483

Open
alexeagle wants to merge 2 commits into
bazelbuild:mainfrom
alexeagle:feat/buildifier-jsonc-config
Open

Allow JSONC in .buildifier.json config parsing#1483
alexeagle wants to merge 2 commits into
bazelbuild:mainfrom
alexeagle:feat/buildifier-jsonc-config

Conversation

@alexeagle

Copy link
Copy Markdown
Contributor

Summary

  • Parse .buildifier.json as JWCC/JSONC using github.com/tailscale/hujson, so configs can include // and /* */ comments and trailing commas.
  • Add unit tests covering comment stripping edge cases and end-to-end config loading.

Justification

Editors and many tooling ecosystems treat .json config files as JSONC (VS Code, Cursor, etc.), but buildifier previously used encoding/json directly 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
  • Verify a .buildifier.json with inline comments loads via --config

Use hujson to parse JWCC/JSONC so buildifier configs can include comments and trailing commas, matching editor JSONC support.
@alexeagle
alexeagle requested a review from a team as a code owner July 17, 2026 23:15
@alexeagle
alexeagle requested review from oreflow and removed request for a team July 17, 2026 23:15

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines 165 to 167
if err := json.Unmarshal(data, c); err != nil {
return err
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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.

Suggested change
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
  1. When modifying code, ensure that changes are consistent with previous behavior, especially if the previous behavior was intentional.

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.

1 participant