run only loads required service env_file and ignores others

Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2025-03-18 09:10:13 +01:00 committed by Guillaume Lours
parent 9129abe516
commit 6a8c0988cf
4 changed files with 23 additions and 2 deletions

View File

@ -184,7 +184,12 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
return nil return nil
}), }),
RunE: Adapt(func(ctx context.Context, args []string) error { RunE: Adapt(func(ctx context.Context, args []string) error {
project, _, err := p.ToProject(ctx, dockerCli, []string{options.Service}, cgo.WithResolvedPaths(true), cgo.WithDiscardEnvFile) project, _, err := p.ToProject(ctx, dockerCli, []string{options.Service}, cgo.WithResolvedPaths(true), cgo.WithoutEnvironmentResolution)
if err != nil {
return err
}
project, err = project.WithServicesEnvironmentResolved(true)
if err != nil { if err != nil {
return err return err
} }

View File

@ -21,6 +21,7 @@ import (
"testing" "testing"
"gotest.tools/v3/assert" "gotest.tools/v3/assert"
"gotest.tools/v3/icmd"
) )
func TestRawEnvFile(t *testing.T) { func TestRawEnvFile(t *testing.T) {
@ -37,3 +38,11 @@ func TestUnusedMissingEnvFile(t *testing.T) {
c.RunDockerComposeCmd(t, "-f", "./fixtures/env_file/compose.yaml", "up", "-d", "serviceA") c.RunDockerComposeCmd(t, "-f", "./fixtures/env_file/compose.yaml", "up", "-d", "serviceA")
} }
func TestRunEnvFile(t *testing.T) {
c := NewParallelCLI(t)
defer c.cleanupWithDown(t, "run_dotenv")
res := c.RunDockerComposeCmd(t, "--project-directory", "./fixtures/env_file", "run", "serviceC", "env")
res.Assert(t, icmd.Expected{Out: "FOO=BAR"})
}

View File

@ -5,4 +5,10 @@ services:
serviceB: serviceB:
image: nginx:latest image: nginx:latest
env_file: env_file:
- /doesnotexist/.env - /doesnotexist/.env
serviceC:
profiles: ["test"]
image: alpine
env_file: test.env

View File

@ -0,0 +1 @@
FOO=BAR