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 <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2026-09-21 15:00:19 +02:00 • committed by Nicolas De loof
parent 039413cc77
commit 0d206e6704
6 changed files with 87 additions and 25 deletions

View file

@ -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

View file

@ -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")
})
}

View file

@ -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

View file

@ -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"))
}

View file

@ -0,0 +1,10 @@
services:
web:
image: alpine
command: sleep infinity
jobs:
migrate:
image: alpine
command: sh -c 'echo "migration done"'
triggers:
manual: true

View file

@ -0,0 +1,10 @@
services:
web:
image: alpine
command: sleep infinity
jobs:
migrate:
image: alpine
command: sh -c 'echo "migration done"'
triggers:
manual: true