Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions cmd/compose/compose.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 == "" {

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.

COMPOSE_FILE is treated as -f for loading, but not for the failure policy of #14151 (hard error for an explicit file, warning only for an implicit one). With both PRs, -p x + broken COMPOSE_FILE + COMPOSE_PROJECT_NAME falls back to label mode with a warning, which is what docker-agent's medium comment describes. Suggest a shared explicitFile() helper (ConfigPaths or COMPOSE_FILE) used here and by the hard-error check in #14151.

Also, os.Getenv misses a COMPOSE_FILE set in .env, which compose-go reads (WithConfigFileEnv uses the project environment). With -p, #14535 stays unfixed in that case: handle it or document the limit.

Behavior change worth stating in the description: -p x stop now fails on a stale/invalid COMPOSE_FILE or a missing required variable, where it used to work label-only.

backend, err := compose.NewComposeService(dockerCli)
if err != nil {
return nil, "", err
Expand All @@ -299,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

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.

Both PRs rewrite this block. After #14151 this fallback can't be reached with -p set (no load, or hard error), so the name-precedence change becomes dead code, and the -p wins… / …used without -p subtests (explicit ConfigPaths + missing file) turn into hard errors. Land #14151 first and rebase, or fold the two.

nit: add a COMPOSE_FILE-based variant to the fallback tests.

if name == "" {
name = envProjectName
}
return nil, name, nil
}
Comment thread
glours marked this conversation as resolved.
Resolved
return nil, "", err
}
Expand Down
58 changes: 58 additions & 0 deletions cmd/compose/compose_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package compose

import (
"path/filepath"
"testing"

"github.com/compose-spec/compose-go/v2/types"
Expand Down Expand Up @@ -77,3 +78,60 @@ 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")
})
}

// 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")
})
}
Loading