Skip to content

Commit b237289

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

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004 h1:lkAMpLVBDaj17e
8989
github.com/cloudflare/cfssl v0.0.0-20180223231731-4e2dcbde5004/go.mod h1:yMWuSON2oQp+43nFtAV/uvKQIFpSPerB57DCt9t8sSA=
9090
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
9191
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
92-
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250306081139-60e14f61e9ef h1:1PcqLEYTxUY28/oZCxtkkF4SpleCrO+Vckw8Skpymz4=
93-
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250306081139-60e14f61e9ef/go.mod h1:6k5l/0TxCg0/2uLEhRVEsoBWBprS2uvZi32J7xub3lo=
92+
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250307154133-53320326687c h1:2KFMBiATRqXYmm/6U1Y3WUxorVBERkdwM1ax7vJI2OQ=
93+
github.com/compose-spec/compose-go/v2 v2.4.9-0.20250307154133-53320326687c/go.mod h1:6k5l/0TxCg0/2uLEhRVEsoBWBprS2uvZi32J7xub3lo=
9494
github.com/containerd/cgroups/v3 v3.0.5 h1:44na7Ud+VwyE7LIoJ8JTNQOa549a8543BmzaJHo6Bzo=
9595
github.com/containerd/cgroups/v3 v3.0.5/go.mod h1:SA5DLYnXO8pTGYiAHXz94qvLQTKfVM5GEVisn4jpins=
9696
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)