Skip to content

fix: report output file write failures cleanly - #1038

Closed
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/cli-output-file-errors
Closed

He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/cli-output-file-errors

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 24, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

Output-file creation failures could leak raw Java exceptions instead of clean CLI diagnostics. The original handleWriteFile[T](f: => T) also interpolated the by-name write expression while formatting errors, which could re-evaluate a failing write path.

Scenario sjsonnet master this PR
Missing output parent dir Java NoSuchFileException stack trace open path/out.json: no such file or directory
Malformed output path Java path exception detail open <path>: <message>
--multi --output-file file-list write failure raw write failure clean exit code 1 + stderr

Modification

  • Change handleWriteFile to take an eager path: String label and a by-name write thunk separately.
  • Route normal output-file writes, byte-renderer writes, YAML stream writes, and multi-mode file-list writes through handleWriteFile.
  • Normalize missing-file, malformed-path, and generic I/O failures into concise diagnostics.
  • Add JVM CLI regression tests for missing parents, malformed output paths, and multi-mode output-file failures.

Result

  • Output-file write failures return exit code 1 instead of throwing.
  • Error messages report the user path and do not leak Java exception class names in covered cases.
  • Successful stdout, file, YAML stream, and multi-output behavior is unchanged.

Verification

  • ./mill --no-daemon 'sjsonnet.jvm[3.3.8].test' sjsonnet.MainTests
  • ./mill --no-daemon 'sjsonnet.jvm[3.3.8].checkFormat'

@He-Pin
He-Pin force-pushed the fix/cli-output-file-errors branch from 4b622f2 to 244001d Compare June 25, 2026 04:48
@He-Pin
He-Pin marked this pull request as draft June 25, 2026 17:57
@He-Pin
He-Pin marked this pull request as ready for review June 25, 2026 18:04
@He-Pin
He-Pin force-pushed the fix/cli-output-file-errors branch from e88687d to 5bc45e8 Compare June 25, 2026 19:04
Motivation: Output file write failures could leak exceptions or unclear filesystem errors.

Modification: Centralize output write error handling and update the CLI tests and goldens.

Result: File write failures now produce clean CLI diagnostics.
@He-Pin

He-Pin commented Jun 28, 2026

Copy link
Copy Markdown
Contributor Author

Superseded by combined CLI PR #1032

@He-Pin He-Pin closed this Jun 28, 2026
stephenamar-db pushed a commit that referenced this pull request Jun 28, 2026
…y bindings (#1032)

## Motivation

Several CLI error paths leaked Java exception names
(`NoSuchFileException`, `InvalidPathException`, `upickle.core.Abort`) or
stack traces to end users. The `-V` short flag was ambiguously bound to
`--ext-str`, `--ext-code`, and `--tla-code`. The `--string` flag
produced internal errors for non-string results instead of a clean type
mismatch message. Binding file paths with invalid characters (e.g. null
bytes) were silently accepted. Reference implementations (C++ jsonnet,
go-jsonnet, jrsonnet) reserve `-V` exclusively for `--ext-str`.

This PR combines the following fixes into a single cohesive CLI
hardening change:

| Original PR | Fix |
| --- | --- |
| #1032 | Reserve `-V` exclusively for `--ext-str` |
| #1034 | Report undefined CLI binding env vars with clean error |
| #1035 | Make `--ext-str-file`/`--ext-code-file` lazy via
importstr/import |
| #1036 | Report missing input files without stack traces |
| #1038 | Report output file write failures cleanly |
| #1039 | Fix `--string` non-string error reporting |

## Modification

### Config / CLI parsing
- Remove `short = 'V'` from `--ext-code` and `--tla-code` `@arg`
annotations in `Config.scala`
- Keep `short = 'V'` on `--ext-str` only
- Update README help snapshot

### Error handling (`SjsonnetMainBase.scala`)
- Add `CliError` class with no stack trace for clean CLI error messages
- Add `handleRenderError` to wrap Jsonnet evaluation errors
- Add `handleWriteFile` to wrap file I/O errors with
C++/go-jsonnet-style messages
- Rewrite `parseBindings` to use `HashMap` with eager
`InvalidPathException` validation
- Make `--ext-str-file` and `--ext-code-file` lazy
(`importstr`/`import`) while still validating path syntax eagerly
- Add clean type error for `--string` with non-string results (matching
C++/go-jsonnet format)
- Wrap output file write and multi/yaml-stream render paths with error
handling

### Tests
- 18 new CLI integration tests covering all error paths
- `ConfigTests` verifies `-V` binds only to `--ext-str`
- 24 new test resource files (jsonnet inputs + golden outputs)

## Result

- All CLI errors produce clean messages matching C++/go-jsonnet
conventions
- `-V` binds only to `--ext-str`, matching reference implementations
- `--string` rejects non-string types with clear `expected string
result, got: <type>` error
- Malformed binding file paths fail eagerly with clean error
- Unused `--ext-str-file` bindings are lazy (file not read unless
referenced)
- 45/45 MainTests pass, 0 regressions in full test suite

## CLI Superset Status

sjsonnet is now a superset of go-jsonnet and C++ jsonnet CLI features,
with these extras:
- `--yaml-out`, `--yaml-debug`, `--debug-importer`, `--debug-stats`
- `--profile`, `--strict`, `--preserve-order`, `--indent`
- `--fatal-warnings`, `--throw-error-for-invalid-sets`
- `--max-stack`, `--max-parser-recursion-depth`
- `--reverse-jpaths-priority`, `--no-trailing-newline`
- `--broken-assertion-logic` (backwards compat)
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