Skip to content

fix(docker): assimilate APE binary for scratch image, expand test cov…#11

Merged
SteveParson merged 1 commit intomainfrom
fix/docker-scratch-assimilate
Feb 21, 2026
Merged

fix(docker): assimilate APE binary for scratch image, expand test cov…#11
SteveParson merged 1 commit intomainfrom
fix/docker-scratch-assimilate

Conversation

@SteveParson
Copy link
Copy Markdown
Owner

Problem

The Docker image used FROM scratch as its final stage, but the binary produced by cosmocc is an APE (Actually Portable Executable) — a polyglot format that relies on /bin/sh to bootstrap itself on Linux. With no shell in a scratch container, the kernel cannot exec the binary and the container exits immediately with exec format error.

Solution

After make, invoke cosmocc's bundled assimilate tool (.cosmocc/current/bin/assimilate) to rewrite the APE binary in-place as a native Linux ELF. The resulting static binary runs directly in a scratch container with no dependencies. The current symlink (created by the download script) is used rather than a glob to avoid the shell expanding it to multiple paths and passing unintended arguments to assimilate.

Testing

Built the image locally with docker build -t king:local . and confirmed the container starts and produces DISCORD_TOKEN is required rather than exec format error. Additionally, extracted the binary from the image and verified the magic bytes changed from MZ (APE polyglot) to \x7fELF (native ELF). Unit test coverage was also expanded from 72 to 84 passing cases, covering previously untested JSON escape sequences (\/, \b, \f), whitespace around colons in json_find_key, negative integers in json_get_int_in_object, additional url_encode characters (#, ?, =, &, /, +), and http_status_code edge cases.

…erage

- Call cosmocc's native `assimilate` tool after `make` so the APE
  polyglot is converted to a native ELF before being copied into the
  `FROM scratch` image (fixes exec format error at runtime)
- Add `.env.sample` with all supported environment variables
- Gitignore `.env` to prevent accidental token commits
- Add Docker usage section to README including `--env-file` example
- Expand test_king.c: JSON escape sequences (\/, \b, \f), whitespace
  around colons, negative nested integers, url_encode special chars
  (#, ?, =, &, /, +), and http_status_code edge cases (84 tests total)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@SteveParson SteveParson merged commit 3e2f40c into main Feb 21, 2026
@SteveParson SteveParson deleted the fix/docker-scratch-assimilate branch February 21, 2026 23:01
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