Skip to content

Commit de64016

Browse files
committed
only load env_file after services have been selected
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent 1a7343b commit de64016

File tree

5 files changed

+23
-2
lines changed

5 files changed

+23
-2
lines changed

cmd/compose/compose.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func (o *ProjectOptions) WithServices(dockerCli command.Cli, fn ProjectServicesF
170170
return Adapt(func(ctx context.Context, args []string) error {
171171
options := []cli.ProjectOptionsFn{
172172
cli.WithResolvedPaths(true),
173-
cli.WithDiscardEnvFile,
173+
cli.WithoutEnvironmentResolution,
174174
}
175175

176176
project, metrics, err := o.ToProject(ctx, dockerCli, args, options...)
@@ -180,6 +180,11 @@ func (o *ProjectOptions) WithServices(dockerCli command.Cli, fn ProjectServicesF
180180

181181
ctx = context.WithValue(ctx, tracing.MetricsKey{}, metrics)
182182

183+
project, err = project.WithServicesEnvironmentResolved(true)
184+
if err != nil {
185+
return err
186+
}
187+
183188
return fn(ctx, project, args)
184189
})
185190
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/Microsoft/go-winio v0.6.2
88
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
99
github.com/buger/goterm v1.0.4
10-
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250306081139-60e14f61e9ef
10+
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250307154133-53320326687c
1111
github.com/containerd/containerd/v2 v2.0.3
1212
github.com/containerd/platforms v1.0.0-rc.1
1313
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUo
9191
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
9292
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250306081139-60e14f61e9ef h1:1PcqLEYTxUY28/oZCxtkkF4SpleCrO+Vckw8Skpymz4=
9393
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250306081139-60e14f61e9ef/go.mod h1:6k5l/0TxCg0/2uLEhRVEsoBWBprS2uvZi32J7xub3lo=
94+
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250307154133-53320326687c/go.mod h1:6k5l/0TxCg0/2uLEhRVEsoBWBprS2uvZi32J7xub3lo=
9495
github.com/containerd/cgroups/v3 v3.0.5 h1:44na7Ud+VwyE7LIoJ8JTNQOa549a8543BmzaJHo6Bzo=
9596
github.com/containerd/cgroups/v3 v3.0.5/go.mod h1:SA5DLYnXO8pTGYiAHXz94qvLQTKfVM5GEVisn4jpins=
9697
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=

pkg/e2e/env_file_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,10 @@ func TestRawEnvFile(t *testing.T) {
3030
res := c.RunDockerComposeCmd(t, "-f", "./fixtures/dotenv/raw.yaml", "run", "test")
3131
assert.Equal(t, strings.TrimSpace(res.Stdout()), "'{\"key\": \"value\"}'")
3232
}
33+
34+
func TestUnusedMissingEnvFile(t *testing.T) {
35+
c := NewParallelCLI(t)
36+
defer c.cleanupWithDown(t, "unused_dotenv")
37+
38+
c.RunDockerComposeCmd(t, "-f", "./fixtures/env_file/compose.yaml", "up", "-d", "serviceA")
39+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
serviceA:
3+
image: nginx:latest
4+
5+
serviceB:
6+
image: nginx:latest
7+
env_file:
8+
- /doesnotexist/.env

0 commit comments

Comments
 (0)