Skip to content

fix: print errors to stderr when --json flag is used - #807

Merged
joaopapereira merged 2 commits into
carvel-dev:developfrom
Yash121l:fix/json-flag-error-output
Oct 30, 2026
Merged

joaopapereira merged 2 commits into
carvel-dev:developfrom
Yash121l:fix/json-flag-error-output

Conversation

@Yash121l

@Yash121l Yash121l commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #631

When a command fails and --json is set, imgpkg exits 1 and prints nothing. For example, imgpkg push -b localhost:1/x/y:v1 -f missing-dir --json gives empty stdout and stderr.

main called os.Exit(1) right after writing the error to the UI. os.Exit skips deferred calls, so confUI.Flush() never ran. The JSON UI buffers every line until Flush, so the error was dropped. The plain-text UI writes right away, which is why this only happens with --json.

This follows the fix for the same bug in kbld (carvel-dev/kbld#600). main now keeps the parsed options so it can check UIFlags.JSON. In JSON mode a failure writes a plain-text imgpkg: Error: ... line to stderr and leaves stdout empty, with exit code 1. Errors without --json still go through the configured UI, and successful JSON output is unchanged.

The new e2e test TestPushWithJSONFlagPrintsErrorToStderr pushes a missing directory with --json and checks the exit error, empty stdout, and the exact stderr line. It fails on develop (stderr is empty) and passes with this change. It fails before any registry call, so it needs no registry. go vet, gofmt and golangci-lint v2.14.0 are clean.

main called os.Exit(1) right after writing the error to the UI, which
skipped the deferred confUI.Flush(). The JSON UI buffers every line until
Flush, so a failing command run with --json exited 1 and printed nothing.

In JSON mode, write the error as plain text to stderr and keep stdout
empty, matching kbld (carvel-dev/kbld#600). Non-JSON errors still go
through the configured UI.

Fixes carvel-dev#631

Signed-off-by: Yash Lunawat <yash.lunawat@rtp.vc>
@Yash121l
Yash121l force-pushed the fix/json-flag-error-output branch from 9a3ddea to 0852763 Compare October 22, 2026 07:42
@Yash121l Yash121l changed the title fix: print errors when --json flag is used fix: print errors to stderr when --json flag is used Oct 22, 2026

@joaopapereira joaopapereira left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm

Windows reports a missing file as GetFileAttributesEx, not lstat, so
check the prefix and the path instead of the exact message.

Signed-off-by: Yash Lunawat <yash.lunawat@rtp.vc>
@Yash121l

Copy link
Copy Markdown
Contributor Author

Thanks @joaopapereira. The Windows failure was my test: it matched the Linux lstat ... no such file or directory text, and Windows says GetFileAttributesEx ... cannot find the file specified. 001f178 checks the imgpkg: Error: prefix and the path instead, so it passes on both and still fails without the fix.

@joaopapereira
joaopapereira merged commit 5f36a47 into carvel-dev:develop Oct 30, 2026
8 checks passed
@github-project-automation github-project-automation Bot moved this to Closed in Carvel Oct 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Closed

Development

Successfully merging this pull request may close these issues.

imgpkg with flag --json swallows error

2 participants