From 0d206e6704785a712cf650896cfbb717b5fd0fad Mon Sep 17 00:00:00 2001 From: Nicolas De Loof Date: Mon, 21 Sep 2026 15:00:19 +0200 Subject: [PATCH] fix(compose): move the job-target refusal into projectOrName itself The previous fix only covered create/start. projectOrName backs every other service-targeting command too (stop, kill, pause, unpause, logs, rm, down, ps, events), so they still surfaced the same two problems: a raw "no such service" error for a job name, and -- when COMPOSE_PROJECT_NAME is set -- a silent no-op, since projectOrName's label-driven fallback swallows the load error entirely. Fold the check into projectOrName so every caller is fixed at once, instead of duplicating the wiring per command. start.go's own job-handling code, now redundant, is removed; jobTargetErr and unselectedJobs (cmd/compose/run.go) are unchanged, just called from one more place. Adds unit coverage for projectOrName itself (including the COMPOSE_PROJECT_NAME path and the typo-correlation edge case) and e2e coverage for two more callers (stop, down) to confirm the fix lives in the shared helper rather than being re-implemented per command. Signed-off-by: Nicolas De Loof --- cmd/compose/compose.go | 10 +++++ cmd/compose/compose_test.go | 25 ++++++++++++ cmd/compose/start.go | 19 ---------- pkg/e2e/jobs_test.go | 38 ++++++++++++++++--- .../compose.yaml | 10 +++++ .../testdata/TestStopRefusesJob/compose.yaml | 10 +++++ 6 files changed, 87 insertions(+), 25 deletions(-) create mode 100644 pkg/e2e/testdata/TestDownRefusesJobWithProjectNameEnv/compose.yaml create mode 100644 pkg/e2e/testdata/TestStopRefusesJob/compose.yaml diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 14757f8dd..5b39a3359 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -254,6 +254,16 @@ func (o *ProjectOptions) projectOrName(ctx context.Context, dockerCli command.Cl p, _, err := o.ToProject(ctx, dockerCli, backend, services, skipUnsupportedAttributesWarning, cli.WithDiscardEnvFile, cli.WithoutEnvironmentResolution) if err != nil { + // a service name among services can genuinely be a declared job: + // every caller of projectOrName treats it as run-only and has + // nothing to act on for it, so report that clearly instead of + // either the raw "no such service" below or, worse, silently + // falling back to the label-driven project next -- a job that + // was never run left no container behind for that fallback to + // find, so it would otherwise look like a successful no-op. + if jobErr, replaced := jobTargetErr(ctx, dockerCli, o, services, err); replaced { + return nil, "", jobErr + } envProjectName := os.Getenv(ComposeProjectName) if envProjectName != "" { return nil, envProjectName, nil diff --git a/cmd/compose/compose_test.go b/cmd/compose/compose_test.go index c2492dfa2..6e3500998 100644 --- a/cmd/compose/compose_test.go +++ b/cmd/compose/compose_test.go @@ -52,3 +52,28 @@ func TestFilterServices(t *testing.T) { _, err = p.GetService("zot") assert.NilError(t, err) } + +// projectOrName backs every service-targeting command except run/create +// (down, stop, kill, pause, unpause, logs, rm, ps, events, start, ...): a +// job target must be refused the same way regardless of which of them is +// used, and the refusal must not be masked by the COMPOSE_PROJECT_NAME +// fallback below it. +func TestProjectOrNameRefusesJob(t *testing.T) { + opts := jobTargetErrFixture(t) + + t.Run("a job target is refused with a clear error", func(t *testing.T) { + _, _, err := opts.projectOrName(t.Context(), nil, "migrate") + assert.Error(t, err, `job "migrate" can only be triggered with "docker compose run"`) + }) + + t.Run("COMPOSE_PROJECT_NAME must not mask the refusal behind a silent fallback", func(t *testing.T) { + t.Setenv("COMPOSE_PROJECT_NAME", "test") + _, _, err := opts.projectOrName(t.Context(), nil, "migrate") + assert.Error(t, err, `job "migrate" can only be triggered with "docker compose run"`) + }) + + t.Run("a real typo among several targets keeps its own error, not a same-invocation job's", func(t *testing.T) { + _, _, err := opts.projectOrName(t.Context(), nil, "typo", "migrate") + assert.ErrorContains(t, err, "no such service: typo") + }) +} diff --git a/cmd/compose/start.go b/cmd/compose/start.go index 6e2e44196..f3f69cec3 100644 --- a/cmd/compose/start.go +++ b/cmd/compose/start.go @@ -18,7 +18,6 @@ package compose import ( "context" - "fmt" "time" "github.com/docker/cli/cli/command" @@ -55,9 +54,6 @@ func startCommand(p *ProjectOptions, dockerCli command.Cli, backendOptions *Back func runStart(ctx context.Context, dockerCli command.Cli, backendOptions *BackendOptions, opts startOptions, services []string) error { project, name, err := opts.projectOrName(ctx, dockerCli, services...) if err != nil { - if jobErr, replaced := jobTargetErr(ctx, dockerCli, opts.ProjectOptions, services, err); replaced { - return jobErr - } return err } if project != nil { @@ -65,21 +61,6 @@ func runStart(ctx context.Context, dockerCli command.Cli, backendOptions *Backen if err := rejectScheduledJobs(project); err != nil { return err } - } else if len(services) > 0 { - // projectOrName falls back to this label-reconstructed, file-less - // project (no error) when loading otherwise fails and - // COMPOSE_PROJECT_NAME is set — swallowing a "no such service" for - // a target that's actually a declared job along with it. Refuse it - // explicitly here too: a job that was never run has no container - // for the label-driven start below to find, so it would otherwise - // exit 0 having silently done nothing. - if jobs, ok := unselectedJobs(ctx, dockerCli, opts.ProjectOptions); ok { - for _, s := range services { - if _, isJob := jobs[s]; isJob { - return fmt.Errorf("job %q can only be triggered with \"docker compose run\"", s) - } - } - } } var timeout time.Duration diff --git a/pkg/e2e/jobs_test.go b/pkg/e2e/jobs_test.go index 20d29f2d8..b64f1700b 100644 --- a/pkg/e2e/jobs_test.go +++ b/pkg/e2e/jobs_test.go @@ -86,6 +86,9 @@ func TestRunJobDependsOnManualFalseJob(t *testing.T) { // A job is documented as run-only: create and start don't know how to // materialize one, so targeting either by a job's name must say so clearly // instead of surfacing compose-go's raw "no such service" selection error. +// The same refusal is shared by every other service-targeting command +// through projectOrName -- see TestStopRefusesJob and +// TestDownRefusesJobWithProjectNameEnv below. func TestCreateRefusesJob(t *testing.T) { NewScenario(t, "create must refuse a job by name, naming run as the right command"). Step("create fails naming the job", @@ -102,12 +105,12 @@ func TestStartRefusesJob(t *testing.T) { ServiceNotCreated("migrate")) } -// When COMPOSE_PROJECT_NAME is set, start's underlying project-resolution -// helper falls back to a label-driven, file-less project on any load -// failure -- including "no such service" for a job -- instead of -// surfacing it. Without an explicit job check on that path, start would -// exit 0 having silently done nothing: a job that was never run has no -// container for the label-driven fallback to find. +// When COMPOSE_PROJECT_NAME is set, projectOrName falls back to a +// label-driven, file-less project on any load failure -- including "no +// such service" for a job -- instead of surfacing it. Without an explicit +// job check on that path, start would exit 0 having silently done +// nothing: a job that was never run has no container for the +// label-driven fallback to find. func TestStartRefusesJobWithProjectNameEnv(t *testing.T) { s := NewScenario(t, "start must still refuse a job by name when COMPOSE_PROJECT_NAME triggers the label-driven fallback") s.Step("start fails naming the job, not silently exiting 0", @@ -115,3 +118,26 @@ func TestStartRefusesJobWithProjectNameEnv(t *testing.T) { StderrContains(`job "migrate" can only be triggered with "docker compose run"`), ServiceNotCreated("migrate")) } + +// projectOrName is shared by every service-targeting command besides +// run/create/start (stop, kill, pause/unpause, logs, rm, down, ps, +// events): the same job refusal applies to all of them. stop stands in +// for that whole family here. +func TestStopRefusesJob(t *testing.T) { + NewScenario(t, "stop must refuse a job by name, naming run as the right command"). + Step("stop fails naming the job", + ComposeCmd("stop", "migrate").MayFail(), + StderrContains(`job "migrate" can only be triggered with "docker compose run"`), + ServiceNotCreated("migrate")) +} + +// Same COMPOSE_PROJECT_NAME fallback as TestStartRefusesJobWithProjectNameEnv, +// exercised through a second projectOrName caller (down) to confirm the fix +// lives in the shared helper, not duplicated per command. +func TestDownRefusesJobWithProjectNameEnv(t *testing.T) { + s := NewScenario(t, "down must still refuse a job by name when COMPOSE_PROJECT_NAME triggers the label-driven fallback") + s.Step("down fails naming the job, not silently exiting 0", + ComposeCmd("down", "migrate").WithEnv("COMPOSE_PROJECT_NAME="+s.Project()).MayFail(), + StderrContains(`job "migrate" can only be triggered with "docker compose run"`), + ServiceNotCreated("migrate")) +} diff --git a/pkg/e2e/testdata/TestDownRefusesJobWithProjectNameEnv/compose.yaml b/pkg/e2e/testdata/TestDownRefusesJobWithProjectNameEnv/compose.yaml new file mode 100644 index 000000000..7cbebdade --- /dev/null +++ b/pkg/e2e/testdata/TestDownRefusesJobWithProjectNameEnv/compose.yaml @@ -0,0 +1,10 @@ +services: + web: + image: alpine + command: sleep infinity +jobs: + migrate: + image: alpine + command: sh -c 'echo "migration done"' + triggers: + manual: true diff --git a/pkg/e2e/testdata/TestStopRefusesJob/compose.yaml b/pkg/e2e/testdata/TestStopRefusesJob/compose.yaml new file mode 100644 index 000000000..7cbebdade --- /dev/null +++ b/pkg/e2e/testdata/TestStopRefusesJob/compose.yaml @@ -0,0 +1,10 @@ +services: + web: + image: alpine + command: sleep infinity +jobs: + migrate: + image: alpine + command: sh -c 'echo "migration done"' + triggers: + manual: true