Skip to content

fix: COMPOSE_REMOVE_ORPHANS resolves identically for every command carrying the flag - #14139

Merged
glours merged 3 commits into
docker:mainfrom
ndeloof:d1-remove-orphans-resolution
Nov 16, 2026
Merged

glours merged 3 commits into
docker:mainfrom
ndeloof:d1-remove-orphans-resolution

Conversation

@ndeloof

@ndeloof ndeloof commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

Epic #14074, D.1 (and G.3). COMPOSE_REMOVE_ORPHANS had three resolution mechanisms: up read it after the project's .env was injected into the process environment; down/kill read it at cobra-tree construction time — before that injection, so a value in the project's .env worked for up but not for them; create/run never read it.

All five commands now resolve it identically from PreRunE through one helper: explicit flag > process env > local .env. create also gains COMPOSE_IGNORE_ORPHANS handling (its ignoreOrphans field was sent to the API without ever being assigned), with up's conflict guard.

Behavioral notes:

  • down/kill now honor the variable from the project's .env; the shell-exported case behaves as before (the --help output no longer reflects it as the flag default, since resolution moved out of the flag definition).
  • the remote-config (OCI/Git) exclusion is kept and now documented as a product decision on setEnvWithDotEnv: COMPOSE_* variables are the local user's choice; a remote model must not steer the CLI consuming it.
  • unit tests pin the .env → process env → flag resolution order and the remote exclusion.

🤖 Generated with Claude Code

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Assessment: 🟡 NEEDS ATTENTION

Lower-confidence findings (not posted inline)

  • [medium→low] cmd/compose/compose.go:757 — Remote-config exclusion guard in setEnvWithDotEnv only works when ConfigPaths is pre-populated; silently skipped when paths are discovered dynamically (confidence: weak 52/100)
  • [medium→low] cmd/compose/create.go:100 — Asymmetric env sources: removeOrphans reads from os.Getenv (process env) while ignoreOrphans reads from project.Environment (loader-resolved); conflict guard may behave differently for remote configs (confidence: weak 52/100)

Comment thread cmd/compose/run.go Resolved
@codecov

codecov Bot commented Aug 27, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.28571% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/compose/create.go 80.00% 1 Missing ⚠️
cmd/compose/run.go 80.00% 1 Missing ⚠️
cmd/compose/up.go 80.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@ndeloof

ndeloof commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

On the lower-confidence finding about the remote-config guard in setEnvWithDotEnv (COMPOSE_FILE discovering paths dynamically): the observation is technically right — COMPOSE_FILE is resolved later by compose-go (cli.WithConfigFileEnv), so opts.ConfigPaths only carries --file and the early return doesn't fire for COMPOSE_FILE=oci://…. It has no product impact though: setEnvWithDotEnv only ever reads the local .env (cwd / --env-file) — a remote artifact's environment cannot flow through it under any path. The early return exists so NewProjectOptions isn't asked to process a remote path string; with an empty ConfigPaths that call is harmless and what gets injected is the local user's own .env, which is exactly the product decision (COMPOSE_* variables are the local user's choice). The residual inconsistency — -f oci://… skips the local-.env injection while COMPOSE_FILE=oci://… applies it — is benign and worth harmonizing separately if desired.

@ndeloof

ndeloof commented Aug 31, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up on the second lower-confidence finding (asymmetric env sources for removeOrphans/ignoreOrphans): the asymmetry is deliberate, and 0aa14bd now states it at the three read sites — the destructive variable resolves through the process environment (local .env only, a remote model cannot enable container removal), the benign one through project.Environment (remote included, worst case a suppressed warning). Future reviewers won't have to rediscover the decision.

@github-actions github-actions Bot added the stale label Nov 1, 2026
@ndeloof
ndeloof force-pushed the d1-remove-orphans-resolution branch from 0aa14bd to e6e5979 Compare November 4, 2026 17:18
@ndeloof ndeloof removed the stale label Nov 5, 2026

@glours glours left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Changes requested, no blocker. The resolution logic is right; what's missing is a test pinning the per-command wiring (inline), and the guard + rationale comment is now pasted three times (inline, one helper fixes it). One inline comment is doc accuracy on the remote exclusion.

Behaviour changes to call out in the PR description / release notes, none are mentioned today:

  • COMPOSE_REMOVE_ORPHANS=true in a local .env now takes effect for down and kill, which only honoured a shell-exported value before (the default was read at flag-registration time, before setEnvWithDotEnv). With the flag set, down also stops and removes running one-off containers (pkg/compose/down.go:72-84) and kill kills them (kill.go:51). A setting meant for up now takes down live compose run sessions.
  • run and create now honour COMPOSE_REMOVE_ORPHANS too.
  • run and create now fail on COMPOSE_IGNORE_ORPHANS=true + remove-orphans. run accepted the combination silently before, and create ignored COMPOSE_IGNORE_ORPHANS altogether.
  • COMPOSE_REMOVE_ORPHANS isn't documented anywhere under docs/ (only COMPOSE_IGNORE_ORPHANS is, docs/reference/compose.md:239); worth a line next to it.

Comment thread cmd/compose/compose.go Resolved
Comment thread cmd/compose/compose.go Resolved
Comment thread cmd/compose/create.go Resolved
Comment thread cmd/compose/create.go Resolved
Comment thread cmd/compose/run.go Resolved
Comment thread cmd/compose/up.go Resolved
Comment thread cmd/compose/orphans_env_test.go Resolved
Comment thread cmd/compose/orphans_env_test.go Resolved
@ndeloof
ndeloof force-pushed the d1-remove-orphans-resolution branch 2 times, most recently from c0690c0 to ee274a9 Compare November 14, 2026 10:41
@ndeloof
ndeloof requested review from docker-agent and removed request for docker-agent November 14, 2026 10:41

@docker-agent docker-agent left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Assessment: 🟢 APPROVE

…rrying the flag

The variable had three resolution mechanisms: up read it in PreRunE
after the project's .env was injected into the process environment;
down and kill read it at cobra-tree construction time — before that
injection, so a value declared in the project's .env worked for up but
not for them; create and run never read it at all.

All five commands now resolve it through one helper, from PreRunE:
explicit flag wins, then the process environment as completed by
setEnvWithDotEnv from the local .env. create also gains the
COMPOSE_IGNORE_ORPHANS handling (its ignoreOrphans field was sent to
the API without ever being assigned) with the same conflict guard as
up.

setEnvWithDotEnv's remote-config exclusion is now documented as a
product decision, not a technical constraint: COMPOSE_* variables
exist so a local user doesn't repeat flags — they are the local user's
choice, and a remote model must not steer the CLI consuming it.

Unit tests pin the .env → process env → flag resolution order and the
remote exclusion (epic docker#14074, D.1 and G.3).

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
run resolved both COMPOSE_REMOVE_ORPHANS and COMPOSE_IGNORE_ORPHANS
but was the only command accepting the contradictory combination
silently; it now rejects it exactly like up and create.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
COMPOSE_REMOVE_ORPHANS and COMPOSE_IGNORE_ORPHANS resolve through
different sources by design, not by accident: the destructive variable
goes through the process environment, completed from the local .env
only — a remote model cannot enable container removal — while the
benign one reads project.Environment, remote configs included, where
the worst a remote model can do is suppress a warning. Stated once, on
the resolveIgnoreOrphans helper shared by up, create and run, so future
reviewers don't have to rediscover the decision. The helper is covered
by a test, and another one runs the real up/down/kill/run/create
commands to pin that each of them resolves COMPOSE_REMOVE_ORPHANS.

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
@ndeloof
ndeloof force-pushed the d1-remove-orphans-resolution branch from ee274a9 to bb3b397 Compare November 16, 2026 13:51

@glours glours left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@glours
glours merged commit b1f8419 into docker:main Nov 16, 2026
115 of 117 checks passed
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.

3 participants