From 598e99d5d610ca219f6b60214867e7630310217f Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Fri, 20 Nov 2026 15:44:32 +0100 Subject: [PATCH 1/2] fix(cli): load the project from COMPOSE_FILE when -p is set projectOrName only loaded the Compose model when -f was passed or when no project name was given. With -p and COMPOSE_FILE, the model was skipped, so commands like stop received no project and rebuilt it from container labels, which don't carry pre_stop hooks: the hooks were silently not run. COMPOSE_FILE is the environment equivalent of -f, so treat it the same way when deciding to load the model. Fixes #14535 Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Guillaume Lours --- cmd/compose/compose.go | 4 +++- cmd/compose/compose_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 67649b49dbb..8ff18eeb854 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -30,6 +30,7 @@ import ( "syscall" "github.com/compose-spec/compose-go/v2/cli" + "github.com/compose-spec/compose-go/v2/consts" "github.com/compose-spec/compose-go/v2/dotenv" "github.com/compose-spec/compose-go/v2/loader" composepaths "github.com/compose-spec/compose-go/v2/paths" @@ -281,7 +282,8 @@ func defaultStringArrayVar(env string) []string { func (o *ProjectOptions) projectOrName(ctx context.Context, dockerCli command.Cli, services ...string) (*types.Project, string, error) { name := o.ProjectName var project *types.Project - if len(o.ConfigPaths) > 0 || o.ProjectName == "" { + // COMPOSE_FILE is the environment equivalent of -f + if len(o.ConfigPaths) > 0 || os.Getenv(consts.ComposeFilePath) != "" || o.ProjectName == "" { backend, err := compose.NewComposeService(dockerCli) if err != nil { return nil, "", err diff --git a/cmd/compose/compose_test.go b/cmd/compose/compose_test.go index 6e3500998ed..8d61d3b0c5a 100644 --- a/cmd/compose/compose_test.go +++ b/cmd/compose/compose_test.go @@ -17,6 +17,7 @@ package compose import ( + "path/filepath" "testing" "github.com/compose-spec/compose-go/v2/types" @@ -77,3 +78,30 @@ func TestProjectOrNameRefusesJob(t *testing.T) { assert.ErrorContains(t, err, "no such service: typo") }) } + +// COMPOSE_FILE is the environment equivalent of -f: with -p, the model must be +// loaded from it too, otherwise the project is rebuilt from container labels +// and anything not stored in them (e.g. pre_stop hooks) is silently skipped. +func TestProjectOrNameLoadsComposeFileFromEnv(t *testing.T) { + dir := t.TempDir() + mustWrite(t, dir, "compose.yaml", "services:\n web:\n image: nginx\n pre_stop:\n - command: echo bye\n") + path := filepath.Join(dir, "compose.yaml") + opts := &ProjectOptions{ProjectName: "other"} + + t.Run("-p with COMPOSE_FILE loads the project", func(t *testing.T) { + t.Setenv("COMPOSE_FILE", path) + project, name, err := opts.projectOrName(t.Context(), nil) + assert.NilError(t, err) + assert.Assert(t, project != nil) + assert.Equal(t, name, "other") + assert.Equal(t, len(project.Services["web"].PreStop), 1) + }) + + t.Run("-p without COMPOSE_FILE stays label-driven", func(t *testing.T) { + unsetEnv(t, "COMPOSE_FILE") + project, name, err := opts.projectOrName(t.Context(), nil) + assert.NilError(t, err) + assert.Assert(t, project == nil) + assert.Equal(t, name, "other") + }) +} From e5a989e0de4fd81b9e5132ee7eb7275b4e3ee176 Mon Sep 17 00:00:00 2001 From: Guillaume Lours Date: Fri, 20 Nov 2026 15:44:33 +0100 Subject: [PATCH 2/2] fix(cli): let -p take precedence over COMPOSE_PROJECT_NAME on load failure When the Compose model can't be loaded, projectOrName falls back to the label-driven project and used COMPOSE_PROJECT_NAME while ignoring an explicit -p. With -p x and COMPOSE_PROJECT_NAME=y, commands such as ps, down or rm then targeted project y instead of x. Use -p first and COMPOSE_PROJECT_NAME only when -p is empty, matching the usual precedence. Co-Authored-By: Claude Sonnet 5.5 Signed-off-by: Guillaume Lours --- cmd/compose/compose.go | 9 ++++++--- cmd/compose/compose_test.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 8ff18eeb854..cfac5be3801 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -301,9 +301,12 @@ func (o *ProjectOptions) projectOrName(ctx context.Context, dockerCli command.Cl if jobErr, replaced := jobTargetErr(ctx, dockerCli, o, services, err); replaced { return nil, "", jobErr } - envProjectName := os.Getenv(ComposeProjectName) - if envProjectName != "" { - return nil, envProjectName, nil + if envProjectName := os.Getenv(ComposeProjectName); envProjectName != "" { + // -p takes precedence over COMPOSE_PROJECT_NAME + if name == "" { + name = envProjectName + } + return nil, name, nil } return nil, "", err } diff --git a/cmd/compose/compose_test.go b/cmd/compose/compose_test.go index 8d61d3b0c5a..ed2ccf05dd4 100644 --- a/cmd/compose/compose_test.go +++ b/cmd/compose/compose_test.go @@ -105,3 +105,33 @@ func TestProjectOrNameLoadsComposeFileFromEnv(t *testing.T) { assert.Equal(t, name, "other") }) } + +// When the model can't be loaded, the label-driven fallback must honor the +// usual precedence: -p over COMPOSE_PROJECT_NAME. +func TestProjectOrNameFallbackNamePrecedence(t *testing.T) { + stale := filepath.Join(t.TempDir(), "missing.yaml") + t.Setenv("COMPOSE_PROJECT_NAME", "from-env") + + t.Run("-p wins over COMPOSE_PROJECT_NAME", func(t *testing.T) { + opts := &ProjectOptions{ProjectName: "from-flag", ConfigPaths: []string{stale}} + project, name, err := opts.projectOrName(t.Context(), nil) + assert.NilError(t, err) + assert.Assert(t, project == nil) + assert.Equal(t, name, "from-flag") + }) + + t.Run("a load error is still reported when COMPOSE_PROJECT_NAME is not set", func(t *testing.T) { + unsetEnv(t, "COMPOSE_PROJECT_NAME") + opts := &ProjectOptions{ProjectName: "from-flag", ConfigPaths: []string{stale}} + _, _, err := opts.projectOrName(t.Context(), nil) + assert.ErrorContains(t, err, "missing.yaml") + }) + + t.Run("COMPOSE_PROJECT_NAME is used without -p", func(t *testing.T) { + opts := &ProjectOptions{ConfigPaths: []string{stale}} + project, name, err := opts.projectOrName(t.Context(), nil) + assert.NilError(t, err) + assert.Assert(t, project == nil) + assert.Equal(t, name, "from-env") + }) +}