Skip to content

config: expand tilde (~) in AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE env vars#3331

Open
LinguineCode wants to merge 1 commit intoaws:mainfrom
LinguineCode:fix-tilde-expansion-env-config-file
Open

config: expand tilde (~) in AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE env vars#3331
LinguineCode wants to merge 1 commit intoaws:mainfrom
LinguineCode:fix-tilde-expansion-env-config-file

Conversation

@LinguineCode
Copy link
Copy Markdown

Description

Fixes #3330

When AWS_CONFIG_FILE or AWS_SHARED_CREDENTIALS_FILE is set to a path starting with ~ (e.g. ~/.aws/config), the SDK passes the literal string to file open calls without expanding the tilde to the user's home directory. This is inconsistent with botocore and with the AWS SDKs and Tools Reference Guide, which specifies that ~ followed by / at the start of a file path should resolve to the home directory.

Changes

  • internal/shareddefaults/shared_config.go: Added ExpandHomePath() helper that expands a leading ~/ to UserHomeDir(). This function is placed alongside the existing UserHomeDir() it depends on.
  • config/env_config.go: Wrapped the two os.Getenv() calls for AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE with shareddefaults.ExpandHomePath().

Testing

  • Added internal/shareddefaults/expand_home_test.go with 8 test cases covering: empty string, absolute paths, relative paths, bare ~, ~/path, nested paths, tilde mid-path (no expansion), and ~username (no expansion).
  • Added test case 55 to config/env_config_test.go verifying end-to-end tilde expansion when AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE are set with ~/ prefixes.

All existing tests continue to pass.

…S_FILE env vars

The Go os package does not perform shell-style tilde expansion, so when
AWS_CONFIG_FILE or AWS_SHARED_CREDENTIALS_FILE is set to a path like
~/.aws/config, the SDK passes the literal string to file open calls,
which fails.

This is inconsistent with botocore (Python SDK), which calls
os.path.expanduser() on these paths, and with the AWS SDKs and Tools
Reference Guide which specifies that ~ followed by / at the start of a
file path should resolve to the user's home directory.

This change adds an ExpandHomePath helper to the shareddefaults package
and applies it when reading these two environment variables in
NewEnvConfig().

Reference: https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html
@LinguineCode LinguineCode requested a review from a team as a code owner February 23, 2026 03:29
@LinguineCode
Copy link
Copy Markdown
Author

LinguineCode commented Feb 23, 2026

Hey folks -- quick note of context on this PR.

I've been a systems architect for 30 years and an exclusive AWS shop for the last 15. Not "multi-cloud with a preference" -- I mean I have never taken a gig on another cloud provider. AWS is my platform. I've been a proponent and unashamed fanboy since the days when people still asked "but what if Amazon shuts it down?"

In all that time, I've been a consumer of open source -- building my career and my clients' businesses on the shoulders of work that people contributed for free. This is my first PR to an open source project, ever. And I'm glad it's to an AWS repo.

I ran into this bug the hard way: hours of debugging why direnv exec . terraform plan worked fine with the AWS CLI but silently failed with Terraform. I used Claude Code to help me hunt down the root cause -- tracing the discrepancy from my .envrc through direnv, into the Terraform provider, and finally to the two os.Getenv() calls in this SDK that don't expand ~ the way botocore does. Once we understood the problem, Claude Code helped me navigate this codebase, write the fix and tests, and put this PR together.

I've spent 30 years benefiting from open source without ever contributing back. The fact that I can now go from "this is broken and I'm frustrated" to "here's a tested fix with an issue and a PR" in a single sitting -- that genuinely moves me. The barrier to entry just got a whole lot lower, and I hope this is the first of many.

Looking forward to feedback and happy to iterate on anything.

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.

config: tilde (~) not expanded in AWS_CONFIG_FILE and AWS_SHARED_CREDENTIALS_FILE environment variables

1 participant