Skip to content

fix: report undefined CLI binding env vars#1034

Open
He-Pin wants to merge 3 commits into
databricks:masterfrom
He-Pin:fix/cli-undefined-env-vars
Open

fix: report undefined CLI binding env vars#1034
He-Pin wants to merge 3 commits into
databricks:masterfrom
He-Pin:fix/cli-undefined-env-vars

Conversation

@He-Pin

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

Copy link
Copy Markdown
Contributor

Motivation

--ext-str NAME crashed with a Java NullPointerException when the environment variable NAME was undefined. --ext-code NAME produced an unhelpful "Unsupported external variable kind: code" error. Reference implementations (C++ jsonnet, go-jsonnet) report a clean error message.

Implementation --ext-str UNDEFINED_VAR
C++ jsonnet 0.22.0 environment variable UNDEFINED_VAR was undefined
go-jsonnet 0.22.0 environment variable UNDEFINED_VAR was undefined
sjsonnet master NullPointerException stack trace
this PR environment variable UNDEFINED_VAR was undefined

Modification

  • Rewrite parseBindings to use a single collection.mutable.HashMap with while-loop iteration instead of chained Map() ++ ... ++ ... with intermediate collections.
  • Check System.getenv(s) for null before invoking the value transformation, throwing a CliError with fillInStackTrace overridden to avoid JVM overhead.
  • Add Map.empty fast path when all binding lists are empty.
  • Add golden tests for all four binding types: --ext-str, --ext-code, --tla-str, --tla-code.

Result

  • Undefined environment variables produce a clean CLI error matching reference implementations.
  • Reduced allocations: single HashMap instead of 8 intermediate collections from chained .map and ++ calls.
  • Empty-input fast path returns Map.empty without allocating.
  • CliError.fillInStackTrace override avoids expensive JVM stack capture on error path.

@He-Pin He-Pin force-pushed the fix/cli-undefined-env-vars branch from 2023dc4 to 23eea1d Compare June 24, 2026 19:10
Motivation:
--ext-str NAME and --ext-code NAME read NAME from the environment when no = value is provided. If NAME was unset, sjsonnet either crashed with a NullPointerException (--ext-str) or produced a misleading extVar kind error (--ext-code), and --ext-code could even succeed when the binding was unused.

Modification:
Detect missing environment variables while parsing CLI bindings and return the same clean CLI-level error used by go-jsonnet. Keep the success path allocation-light with an empty-binding fast path, a single mutable map for non-empty bindings, and a stackless CLI exception only on the error path. Add jsonnet-resource + golden CLI regression tests for --ext-str and --ext-code.

Result:
Missing binding environment variables now fail eagerly with ERROR: environment variable NAME was undefined instead of leaking Java/internal errors or succeeding inconsistently.
@He-Pin He-Pin force-pushed the fix/cli-undefined-env-vars branch from 23eea1d to eb137b6 Compare June 25, 2026 04:48
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