Repository navigation
Conversation
He-Pin
force-pushed
the
fix/cli-undefined-env-vars
branch
2 times, most recently
from
June 25, 2026 04:48
23eea1d to
eb137b6
Compare
Motivation: Undefined environment variables for CLI bindings produced confusing downstream behavior. Modification: Validate binding environment values and return explicit CLI errors. Result: Undefined env vars now fail fast with clear diagnostics.
He-Pin
force-pushed
the
fix/cli-undefined-env-vars
branch
from
June 27, 2026 02:42
5ce9040 to
ca01784
Compare
Contributor
Author
|
Superseded by combined CLI PR #1032 |
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)
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.
Motivation
--ext-str NAMEcrashed with a JavaNullPointerExceptionwhen the environment variableNAMEwas undefined.--ext-code NAMEproduced an unhelpful"Unsupported external variable kind: code"error. Reference implementations (C++ jsonnet, go-jsonnet) report a clean error message.--ext-str UNDEFINED_VARenvironment variable UNDEFINED_VAR was undefinedenvironment variable UNDEFINED_VAR was undefinedNullPointerExceptionstack traceenvironment variable UNDEFINED_VAR was undefinedModification
parseBindingsto use a singlecollection.mutable.HashMapwith while-loop iteration instead of chainedMap() ++ ... ++ ...with intermediate collections.System.getenv(s)fornullbefore invoking the value transformation, throwing aCliErrorwithfillInStackTraceoverridden to avoid JVM overhead.Map.emptyfast path when all binding lists are empty.--ext-str,--ext-code,--tla-str,--tla-code.Result
HashMapinstead of 8 intermediate collections from chained.mapand++calls.Map.emptywithout allocating.CliError.fillInStackTraceoverride avoids expensive JVM stack capture on error path.