Skip to content

Conversation

@tensorworkerr
Copy link

@tensorworkerr tensorworkerr commented Jan 14, 2026

When using --env-file=/~.env, the tilde was not expanded to the user's home directory. Instead, it was treated as a literal character and resolved relative to the current working directory, resulting in errors like "couldn't find env file: /current/dir/~/.env".

This adds an ExpandUser function that expands ~ to the home directory before converting relative paths to absolute paths.
Fixes #13508

What I did
-Added ExpandUser() function in internal/paths/paths.go that expands a leading ~ to the user's home directory using os.UserHomeDir()
-Updated the env file path processing in cmd/compose/compose.go to call ExpandUser() before checking if paths are absolute
-Added unit tests for the new function covering various edge cases (empty string, ~ only, ~/.env, ~/subdir/.env, absolute paths, relative paths, tilde in middle of path, ~otheruser/... syntax)

Related issue
Fixes #13508

Contribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=178221229

@tensorworkerr tensorworkerr requested a review from a team as a code owner January 14, 2026 03:31
// ExpandUser expands a leading tilde (~) in a path to the user's home directory.
// If the path doesn't start with ~, it is returned unchanged.
// If the home directory cannot be determined, the original path is returned.
func ExpandUser(path string) string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use paths.ExpandUser from compose-go

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated as you suggested.

@ndeloof
Copy link
Contributor

ndeloof commented Jan 14, 2026

A better place to fix this is GetEnvFromFile in compose-go: relativePathsResolver already manages this for usage within a compose file as file attributes, would make sense to have this logic all in one place

@tensorworkerr tensorworkerr requested a review from ndeloof January 14, 2026 23:59
@tensorworkerr tensorworkerr force-pushed the fix-env-file-tilde-expansion branch from cb64f55 to 3f2191b Compare January 15, 2026 00:02
When using --env-file=~/.env, the tilde was not expanded to the user's
home directory. Instead, it was treated as a literal character and
resolved relative to the current working directory, resulting in errors
like "couldn't find env file: /current/dir/~/.env".

This adds an ExpandUser function that expands ~ to the home directory
before converting relative paths to absolute paths.

Fixes docker#13508

Signed-off-by: tensorworker <tensorworker@proton.me>
@tensorworkerr tensorworkerr force-pushed the fix-env-file-tilde-expansion branch from 3f2191b to 3097793 Compare January 15, 2026 00:02
…on.me>

Signed-off-by: tensorworker <tensorworker@proton.me>
@tensorworkerr tensorworkerr force-pushed the fix-env-file-tilde-expansion branch from 3097793 to 9551e86 Compare January 15, 2026 00:03
@ndeloof ndeloof enabled auto-merge (rebase) January 15, 2026 07:14
@ndeloof ndeloof merged commit 02b606e into docker:main Jan 15, 2026
24 checks passed
@thaJeztah
Copy link
Member

@ndeloof I don't think this was a bug, and actually the correct behavior; expansion is handled by the shell, and does not happen if the value is prefixed by a =; you'll see the same for any tool, e.g. trying with git;

git --git-dir=~/go/src/github.com/docker/compose/.git describe
fatal: not a git repository: '~/go/src/github.com/docker/compose/.git'

git --git-dir ~/go/src/github.com/docker/compose/.git describe
v2.26.1-891-gb760afaf9

Bit concerned that this could be a sliding slope.

@ndeloof
Copy link
Contributor

ndeloof commented Jan 15, 2026

@thaJeztah yes I know shell is in charge for this, but as long as we support both space and equal flag values, need to get an homogeneous behavior. compose.yaml already offers this feature

@thaJeztah
Copy link
Member

Yeah, the issue here would be that it's not homogeneous across the CLI; other commands will not expand these, so now docker <some command> --some-flag=~/foo won't work, but docker compose --some-flag=~/foo will.

tmeijn pushed a commit to tmeijn/dotfiles that referenced this pull request Jan 22, 2026
This MR contains the following updates:

| Package | Update | Change |
|---|---|---|
| [docker/compose](https://github.com/docker/compose) | patch | `v5.0.1` → `v5.0.2` |

MR created with the help of [el-capitano/tools/renovate-bot](https://gitlab.com/el-capitano/tools/renovate-bot).

**Proposed changes to behavior should be submitted there as MRs.**

---

### Release Notes

<details>
<summary>docker/compose (docker/compose)</summary>

### [`v5.0.2`](https://github.com/docker/compose/releases/tag/v5.0.2)

[Compare Source](docker/compose@v5.0.1...v5.0.2)

#### What's Changed

##### 🐛 Fixes

- Fixed progress UI to adapt to terminal width by [@&#8203;ndeloof](https://github.com/ndeloof) in [#&#8203;13519](docker/compose#13519)
- Removed warning when no explicit build has been requested. by [@&#8203;ndeloof](https://github.com/ndeloof) in [#&#8203;13493](docker/compose#13493)
- Restored `runtime_flags` support in `models` by [@&#8203;ilopezluna](https://github.com/ilopezluna) in [#&#8203;13460](docker/compose#13460)
- Added service name completion to `down` command by [@&#8203;bmo-at](https://github.com/bmo-at) in [#&#8203;13470](docker/compose#13470)
- Fixed tilde in `--env-file` paths expanded to user home directory by [@&#8203;tensorworkerr](https://github.com/tensorworkerr) in [#&#8203;13510](docker/compose#13510)
- Handle `healthcheck.disable: true` by [@&#8203;stavros-k](https://github.com/stavros-k) in [#&#8203;13494](docker/compose#13494)
- Fixed shutdown and error handling for large file change batches in `watch` by [@&#8203;amyssnippet](https://github.com/amyssnippet) in [#&#8203;13525](docker/compose#13525)

##### 🔧  Internal

- Added unit test for upOptions.OnExit method by [@&#8203;htoyoda18](https://github.com/htoyoda18) in [#&#8203;13489](docker/compose#13489)
- clean up temporary compose files after conversion by [@&#8203;htoyoda18](https://github.com/htoyoda18) in [#&#8203;13483](docker/compose#13483)
- Fixed typo in isSwarmEnabled method name by [@&#8203;htoyoda18](https://github.com/htoyoda18) in [#&#8203;13481](docker/compose#13481)
- Fixed incorrect usage of errgroup.WithContext by [@&#8203;htoyoda18](https://github.com/htoyoda18) in [#&#8203;13480](docker/compose#13480)
- Fixed timeout initialization when waitTimeout is zero by [@&#8203;htoyoda18](https://github.com/htoyoda18) in [#&#8203;13471](docker/compose#13471)
- Extracted API version constants to dedicated file by [@&#8203;htoyoda18](https://github.com/htoyoda18) in [#&#8203;13503](docker/compose#13503)
- Replace tabbed indentation in sdk.md by [@&#8203;pkqk](https://github.com/pkqk) in [#&#8203;13505](docker/compose#13505)
- Improved attach error handling and cleanup by [@&#8203;htoyoda18](https://github.com/htoyoda18) in [#&#8203;13488](docker/compose#13488)
- Modernize tests by [@&#8203;dgageot](https://github.com/dgageot) in [#&#8203;13531](docker/compose#13531)
- set fsnotify build tag when building for OSX by [@&#8203;ndeloof](https://github.com/ndeloof) in [#&#8203;13532](docker/compose#13532)

##### ⚙️ Dependencies

- build(deps): bump github.com/klauspost/compress to v1.18.2 by [@&#8203;thaJeztah](https://github.com/thaJeztah) in [#&#8203;13499](docker/compose#13499)
- go.mod: remove exclude rules by [@&#8203;thaJeztah](https://github.com/thaJeztah) in [#&#8203;13498](docker/compose#13498)
- build(deps): bump github.com/containerd/containerd/v2 to v2.2.1 by [@&#8203;thaJeztah](https://github.com/thaJeztah) in [#&#8203;13497](docker/compose#13497)
- build(deps): bump golang.org/x/sys from 0.39.0 to 0.40.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13502](docker/compose#13502)
- build(deps): bump google.golang.org/grpc from 1.77.0 to 1.78.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13475](docker/compose#13475)
- build(deps): bump github.com/go-viper/mapstructure/v2 from 2.4.0 to 2.5.0 by [@&#8203;dependabot](https://github.com/dependabot)\[bot] in [#&#8203;13506](docker/compose#13506)
- build(deps): bump github.com/sirupsen/logrus v1.9.4 by [@&#8203;thaJeztah](https://github.com/thaJeztah) in [#&#8203;13518](docker/compose#13518)
- Bump compose to v2.10.1 by [@&#8203;ndeloof](https://github.com/ndeloof) in [#&#8203;13528](docker/compose#13528)

#### New Contributors

- [@&#8203;bmo-at](https://github.com/bmo-at) made their first contribution in [#&#8203;13470](docker/compose#13470)
- [@&#8203;pkqk](https://github.com/pkqk) made their first contribution in [#&#8203;13505](docker/compose#13505)
- [@&#8203;tensorworkerr](https://github.com/tensorworkerr) made their first contribution in [#&#8203;13510](docker/compose#13510)
- [@&#8203;stavros-k](https://github.com/stavros-k) made their first contribution in [#&#8203;13494](docker/compose#13494)
- [@&#8203;amyssnippet](https://github.com/amyssnippet) made their first contribution in [#&#8203;13525](docker/compose#13525)
- [@&#8203;dgageot](https://github.com/dgageot) made their first contribution in [#&#8203;13531](docker/compose#13531)

**Full Changelog**: <docker/compose@v5.0.1...v5.0.2>

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Enabled.

♻ **Rebasing**: Whenever MR is behind base branch, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this MR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this MR, check this box

---

This MR has been generated by [Renovate Bot](https://github.com/renovatebot/renovate).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi44Ni4xIiwidXBkYXRlZEluVmVyIjoiNDIuODYuMSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOlsiUmVub3ZhdGUgQm90IiwiYXV0b21hdGlvbjpib3QtYXV0aG9yZWQiLCJkZXBlbmRlbmN5LXR5cGU6OnBhdGNoIl19-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] docker compose down --env does not translate ~/.env to .env file in home directory

3 participants