fix: inline --json-schema file paths so structured output works on CLI 2.1.160 (closes #3)

The docs recommend the file-path form (`--json-schema /path/to/schema.json`)
as primary, but the default-pinned Claude CLI 2.1.160 only accepts an inline
JSON string. Passing a path makes the CLI exit 0 with empty output, so the
action's fail-loud branch trips on every run.

Resolve the value before spawning the CLI: a file-path schema is read
in-process and passed inline (validated + compacted), while an already-inline
schema is left untouched. Because the value is handed to spawn() as an argv
element (never through a shell), a schema with `$ref`/`$defs` round-trips
intact. Unreadable files and invalid JSON now fail with a clear error instead
of the CLI's silent empty output.

Docs (README, example workflow, action.yml) are corrected to explain that the
file-path form is auto-inlined and works on the default CLI.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-03 10:45:40 +10:00
parent 13cde95d51
commit d71f4db3d7
5 changed files with 212 additions and 14 deletions
+3 -2
View File
@@ -109,9 +109,10 @@ jobs:
Notes:
- **Use the file-path form** (`--json-schema /path/to/schema.json`). An inline schema also works if single-quoted (`--json-schema '{"type":"object"}'`), but the schema must be single-quoted so the shell tokenizer treats it as one literal token and does not expand `$` (e.g. `$ref`).
- **Use the file-path form** (`--json-schema /path/to/schema.json`). The action reads the file and passes its contents to the CLI **inline**, so the file-path form works even on CLIs whose `--json-schema` only accepts an inline string (including the default-pinned `2.1.160`). The path is preferred because it has no spaces or `$` for the shell to mangle, so a schema with `$ref`/`$defs` round-trips intact. An inline schema also works if single-quoted (`--json-schema '{"type":"object"}'`) so the shell treats it as one literal token and does not expand `$` (e.g. `$ref`).
- If the `--json-schema` value is a path that can't be read, or the file isn't valid JSON, the run **fails loudly** with a clear error (rather than the CLI silently emitting empty output).
- If `--json-schema` is supplied but the model returns no structured result, the run **fails loudly** rather than emitting an empty output.
- **Claude CLI version.** `--json-schema` requires a recent CLI. This action installs `2.1.160` by default. If you pin `path_to_claude_code_executable` (e.g. a pre-baked runner image), that baked CLI must be `--json-schema`-capable — bump it independently. See [Version pins](#version-pins).
- **Claude CLI version.** The default-installed CLI (`2.1.160`) supports `--json-schema`, and the action inlines a file-path schema for you — so the file-path form is not version-sensitive. If you pin `path_to_claude_code_executable` (e.g. a pre-baked runner image), that baked CLI must still be new enough to support the `--json-schema` flag itself — bump it independently. See [Version pins](#version-pins).
### Version pins