Skip to content

fix: make --ext-str-file lazy via importstr#1035

Open
He-Pin wants to merge 3 commits into
databricks:masterfrom
He-Pin:fix/cli-ext-str-file-missing
Open

fix: make --ext-str-file lazy via importstr#1035
He-Pin wants to merge 3 commits into
databricks:masterfrom
He-Pin:fix/cli-ext-str-file-missing

Conversation

@He-Pin

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

Copy link
Copy Markdown
Contributor

Motivation

--ext-str-file eagerly read the file during CLI argument parsing, causing a crash when the file was missing — even if the external variable was never used by the Jsonnet program. Reference implementations (C++ jsonnet, go-jsonnet, jrsonnet) defer the file read until std.extVar is evaluated.

Implementation --ext-str-file missing.json (unused)
C++ jsonnet 0.22.0 exit 0
go-jsonnet 0.22.0 exit 0
jrsonnet 0.5.0-pre99 exit 0
sjsonnet master crash: NoSuchFileException
this PR exit 0

Modification

  • Store file-backed string bindings as importstr @'path' code instead of eagerly reading via os.read.
  • File read is deferred until std.extVar is evaluated, using the normal Jsonnet import mechanism.
  • Quote escaping via v.replace("'", "''") matches go-jsonnet's verbatim string literal convention.
  • Stdin (- or /dev/stdin) remains eager since stdin cannot be lazily reopened.
  • Add @unused annotation to the wd parameter in parseBindings since it's no longer used after making file reads lazy.
  • Add golden tests for used-missing-file and unused-missing-file scenarios.

Result

  • Unused missing files succeed (exit 0), matching reference implementations.
  • Used missing files fail at evaluation time with a clear Couldn't import file error and proper stack trace.
  • Net performance improvement: unused file-backed bindings pay zero I/O cost.

Motivation:
--ext-str-file should behave like Jsonnet importstr: missing files should only fail when the bound variable is evaluated. sjsonnet read the file eagerly during CLI binding parsing, so even unused bindings failed.

Modification:
Generate an importstr expression for file-backed string bindings, matching go-jsonnet's verbatim-string escaping. Keep stdin bindings eager as string literals because stdin cannot be lazily reopened. Add CLI golden tests for used missing files, unused missing files, and relative-path reads.

Result:
Used missing --ext-str-file bindings now fail through the normal import error path, while unused missing bindings are lazy and evaluation succeeds.
@He-Pin He-Pin force-pushed the fix/cli-ext-str-file-missing branch from 874ab83 to 4c353e4 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