Repository: docker/compose

Issue:
[BUG] `env_file` is not loaded in `docker compose` (v2.34.0) for services with profiles
### Description

In Docker Compose version 2.34.0, environment variables defined in an `env_file` are no longer loaded when running a service using `docker compose run <service name>`, specifically for services that have `profiles:`, **if** profile is not enabled explicitly via `COMPOSE_PROFILES` or `--profile`. In version 2.33.1, the variables were correctly loaded.

### Steps to reproduce

1. Create a `docker-compose.yaml` file:

   ```yaml
   services:
     service-with-profile:
       profiles: [ test-profile ]
       env_file: .env
       command: bash -c "env | grep TEST_VAR"
       image: ubuntu:24.04
   ```

2. Create a `.env` file:

   ```
   TEST_VAR_1=1
   TEST_VAR_2=2
   ```

3. Run the service using `docker compose run`:

   ```sh
   docker compose run --rm service-with-profile
   ```

**Expected Behavior (Docker Compose v2.33.1 or if `COMPOSE_PROFILES` / `--profile` is provided):**

```
TEST_VAR_2=2
TEST_VAR_1=1
```

**Actual Behavior (Docker Compose v2.34.0):**

```
(no output, environment variables are missing)
```

### System Information

```
$ docker version
Client:
 Version:           28.0.1
 API version:       1.48
 Go version:        go1.24.1
 Git commit:        068a01ea94
 Built:             Thu Mar  6 19:09:48 2025
 OS/Arch:           linux/amd64
 Context:           default

Server:
 Engine:
  Version:          28.0.1
  API version:      1.48 (minimum version 1.24)
  Go version:       go1.24.1
  Git commit:       bbd0a17ccc
  Built:            Thu Mar  6 19:09:48 2025
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          v2.0.3
  GitCommit:        06b99ca80cdbfbc6cc8bd567021738c9af2b36ce.m
 runc:
  Version:          1.2.5
  GitCommit:        
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0
```

```
$ docker compose version  # version with the issue          
Docker Compose version 2.34.0
```

```
$ docker compose version  # working version          
Docker Compose version 2.33.1
```

Hints from issue comments:
@codingjerk: @ndeloof Issue was probably introduced [here](https://github.com/docker/compose/commit/b23728941d202749fa8918cf5e8a59d1fe32513c)@lily-controlzee: thanks for reporting. I just encountered the same issue and you saved me the trouble of making a repro@Aden-Q: The issue still exists as of `v2.34.0`, compose doesn't respect `env_file` when there's a `profile` section@davidrunger: > The issue still exists as of `v2.34.0`, compose doesn't respect `env_file` when there's a `profile` section

@Aden-Q : I believe that is expected. I think `v2.34.0` is the release in which this bug was first introduced. Presumably it will be fixed in the next release.@StefanWetterActiware: Does anyone know when a release fixing this bug will be available?@aukaost: Getting a release out for this issue would be most appreciated.@davidrunger: [v2.35.0](https://github.com/docker/compose/releases/tag/v2.35.0) is now available, which includes a fix for this bug.

Solution patch:
diff --git a/cmd/compose/run.go b/cmd/compose/run.go
index 137c2526..efc5c831 100644
--- a/cmd/compose/run.go
+++ b/cmd/compose/run.go
@@ -184,7 +184,12 @@ func runCommand(p *ProjectOptions, dockerCli command.Cli, backend api.Service) *
 			return nil
 		}),
 		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 {
 				return err
 			}
diff --git a/pkg/e2e/fixtures/env_file/compose.yaml b/pkg/e2e/fixtures/env_file/compose.yaml
index 07d5dfeb..9983f573 100644
--- a/pkg/e2e/fixtures/env_file/compose.yaml
+++ b/pkg/e2e/fixtures/env_file/compose.yaml
@@ -5,4 +5,10 @@ services:
   serviceB:
     image: nginx:latest
     env_file:
-      - /doesnotexist/.env
\ No newline at end of file
+      - /doesnotexist/.env
+
+  serviceC:
+    profiles: ["test"]
+    image: alpine
+    env_file: test.env
+
diff --git a/pkg/e2e/fixtures/env_file/test.env b/pkg/e2e/fixtures/env_file/test.env
new file mode 100644
index 00000000..15c36f50
--- /dev/null
+++ b/pkg/e2e/fixtures/env_file/test.env
@@ -0,0 +1 @@
+FOO=BAR
\ No newline at end of file


Test patch:
diff --git a/pkg/e2e/env_file_test.go b/pkg/e2e/env_file_test.go
index ed7c8431..ccf02efe 100644
--- a/pkg/e2e/env_file_test.go
+++ b/pkg/e2e/env_file_test.go
@@ -21,6 +21,7 @@ import (
 	"testing"
 
 	"gotest.tools/v3/assert"
+	"gotest.tools/v3/icmd"
 )
 
 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")
 }
+
+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"})
+}