container icon indicating copy to clipboard operation
container copied to clipboard

Route `container image save` reference list to stderr in stdout mode

Open costajohnt opened this issue 5 months ago • 2 comments

Type of Change

  • [x] Bug fix
  • [ ] New feature
  • [ ] Breaking change
  • [ ] Documentation update

Motivation and Context

Fixes #1801.

When container image save runs without --output, stdout carries the OCI tar archive. The command writes the archive bytes to stdout and then print(reference)s each saved image reference to stdout afterward, appending non-archive text after the tar EOF marker. That corrupts the stream for strict tar/OCI consumers, so a command like:

container image save alpine:latest > alpine.tar

produces an archive with trailing non-archive bytes. The existing round-trip test passes only because container image load tolerates the trailing data, while other tooling rejects or warns about it.

This routes the saved-reference list to stderr in the no---output branch, so stdout contains only archive bytes. When saving to a file via --output, stdout is free, so the references continue to print to stdout exactly as before.

I used a raw FileHandle.standardError.write(...) to match the existing stderr writes in this codebase (e.g. Application.swift) and to keep the reference list as plain text rather than formatted log lines. Happy to route it through the logger instead if you'd prefer.

Testing

  • [ ] Tested locally
  • [x] Added/updated tests
  • [ ] Added/updated docs

Added testImageSaveToStdoutProducesCleanArchive: it saves an image to stdout and asserts that the captured stdout ends at the tar EOF marker (1024 zero bytes), with the saved reference surfaced on stderr. With the bug, the trailing bytes are reference text and the assertion fails.

swift build and swift format are clean. I was not able to run the CLI integration suite locally (it needs the running container runtime), so I left "Tested locally" unchecked. The new test sits alongside the existing image save/load integration tests and is written to run in CI.

costajohnt avatar Jun 24 '26 19:06 costajohnt

@costajohnt The fix looks good now, but one more thing - we need you to submit signed commits to merge the change. See https://github.com/apple/containerization/blob/main/CONTRIBUTING.md#pull-requests for details.

jglogan avatar Jun 25 '26 21:06 jglogan

Code Coverage

Tier Line Coverage
Unit 33.56%
Integration 23.05%
Combined 55.77%

github-actions[bot] avatar Jun 26 '26 05:06 github-actions[bot]

@costajohnt The point of using the logger is that by default it's like a print(stderr, msg) - no special log formatting, just the message. But if you use --debug you'll get log-formatted messages with timestamps.

jglogan avatar Jun 26 '26 17:06 jglogan