fix: --yaml-stream produces correct output matching C++ jsonnet/go-jsonnet#1026
Merged
Merged
Conversation
stephenamar-db
approved these changes
Jun 24, 2026
Collaborator
|
rebase |
0ea50d5 to
8274626
Compare
…onnet
The --yaml-stream (-y) CLI flag had four bugs:
1. Missing initial `---` document start marker
2. Missing `...` document end marker
3. Rendered each document as YAML instead of JSON
4. Did not error on non-array input (silently fell through to normal rendering)
Cross-implementation comparison for input `[{"a":1},{"b":2}]`:
| Feature | C++ jsonnet | go-jsonnet | sjsonnet (before) | sjsonnet (after) |
|----------------------|----------------|----------------|-------------------|------------------|
| Initial `---` | Yes | Yes | No | Yes |
| Trailing `...` | Yes | Yes | No | Yes |
| Document format | JSON | JSON | YAML | JSON |
| Non-array error | Yes | Yes | No (silent) | Yes |
| Empty array output | (empty) | (empty) | `\n` | (empty) |
Output is now byte-identical to C++ jsonnet for all tested cases.
Root cause: The YAML stream code in SjsonnetMainBase.scala used
`config.copy(yamlOut = true)` which forced YAML rendering instead of JSON,
had incorrect `---` prefix logic (only for scalars or non-first elements),
and fell through to `renderNormal` for non-array values instead of erroring.
Fix: Rewrite the YAML stream handler to:
- Write `---\n` before each document
- Render each document as JSON using the standard Renderer
- Write `...\n` after all documents
- Produce an error with the value's type name for non-array input
- Write directly to stdout stream to avoid double-newline from println
- Handle --output-file correctly for the YAML stream case
8274626 to
3053b17
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR fixes
--yaml-stream(-y) output to match C++ jsonnet and go-jsonnet stream behavior.Before this PR, sjsonnet rendered the top-level array using YAML-style output without the JSON document stream markers expected by C++ jsonnet/go-jsonnet.
Behavior comparison
Local reference versions used:
v0.22.0v0.22.00.5.0-pre99[1, 2]with-y---\n1\n---\n2\n...\n[]with-y\n...\n{a: 1}with-yThe intentional compatibility target is C++ jsonnet/go-jsonnet. jrsonnet differs for empty arrays and error wording.
Changes
SjsonnetMainBase.scala: render YAML streams as JSON documents separated by---and terminated by..., with empty arrays producing no outputSjsonnetMainBase.scala: reject non-array top-level values in stream mode with the C++/go-jsonnet style errorMainTests.scala: update expected output and add non-array / empty-array regression coverageTests
rtk ./mill 'sjsonnet.jvm[3.3.7]'.test.testOnly sjsonnet.MainTests rtk git diff --check upstream/master...HEADBoth passed locally after rebasing onto
upstream/master.