Describe the bug
When AWS_CONFIG_FILE or AWS_SHARED_CREDENTIALS_FILE is set to a path starting with ~ (e.g. ~/.aws/config), the Go SDK does not expand the tilde to the user's home directory. The literal string ~/.aws/config is passed to file open calls, which fails because no such path exists.
This is inconsistent with:
- botocore (Python SDK), which calls
os.path.expanduser() on these paths before opening them
- The AWS SDKs and Tools Reference Guide, which specifies that
~ followed by / (or the platform path separator) at the start of a shared config/credentials file path should resolve to the user's home directory
Expected Behavior
AWS_CONFIG_FILE=~/.aws/config should resolve to $HOME/.aws/config (e.g. /home/user/.aws/config) before the SDK attempts to open the file, consistent with other AWS SDKs and the shared configuration specification.
Current Behavior
The SDK reads the raw environment variable value via os.Getenv() in config/env_config.go and stores it without any path expansion. When it later attempts to open ~/.aws/config as a literal file path, it fails.
Reproduction Steps
export AWS_CONFIG_FILE=~/.aws/config
export AWS_PROFILE=my-profile
# Any Go program using aws-sdk-go-v2 to load config will fail
This commonly affects users of direnv who set AWS_CONFIG_FILE in .envrc files, and any environment where these variables are set with ~ rather than the expanded $HOME.
Solution
The SDK's internal/shareddefaults package already has a UserHomeDir() function used to construct default paths. A small ExpandHomePath() helper can be added alongside it and applied to the two os.Getenv() calls in NewEnvConfig():
config/env_config.go line 343: cfg.SharedCredentialsFile
config/env_config.go line 344: cfg.SharedConfigFile
I have a PR ready with the fix and tests.
References
Describe the bug
When
AWS_CONFIG_FILEorAWS_SHARED_CREDENTIALS_FILEis set to a path starting with~(e.g.~/.aws/config), the Go SDK does not expand the tilde to the user's home directory. The literal string~/.aws/configis passed to file open calls, which fails because no such path exists.This is inconsistent with:
os.path.expanduser()on these paths before opening them~followed by/(or the platform path separator) at the start of a shared config/credentials file path should resolve to the user's home directoryExpected Behavior
AWS_CONFIG_FILE=~/.aws/configshould resolve to$HOME/.aws/config(e.g./home/user/.aws/config) before the SDK attempts to open the file, consistent with other AWS SDKs and the shared configuration specification.Current Behavior
The SDK reads the raw environment variable value via
os.Getenv()inconfig/env_config.goand stores it without any path expansion. When it later attempts to open~/.aws/configas a literal file path, it fails.Reproduction Steps
This commonly affects users of direnv who set
AWS_CONFIG_FILEin.envrcfiles, and any environment where these variables are set with~rather than the expanded$HOME.Solution
The SDK's
internal/shareddefaultspackage already has aUserHomeDir()function used to construct default paths. A smallExpandHomePath()helper can be added alongside it and applied to the twoos.Getenv()calls inNewEnvConfig():config/env_config.goline 343:cfg.SharedCredentialsFileconfig/env_config.goline 344:cfg.SharedConfigFileI have a PR ready with the fix and tests.
References
raw_config_parse()- callsexpanduser()before opening config files