-
Notifications
You must be signed in to change notification settings - Fork 5.7k
fix: expand tilde in --env-file paths to user home directory #13510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: expand tilde in --env-file paths to user home directory #13510
Conversation
internal/paths/paths.go
Outdated
| // 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 { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated as you suggested.
|
A better place to fix this is |
cb64f55 to
3f2191b
Compare
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>
3f2191b to
3097793
Compare
…on.me> Signed-off-by: tensorworker <tensorworker@proton.me>
3097793 to
9551e86
Compare
|
@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 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-gb760afaf9Bit concerned that this could be a sliding slope. |
|
@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 |
|
Yeah, the issue here would be that it's not homogeneous across the CLI; other commands will not expand these, so now |
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 envfile: /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