terraform
terraform copied to clipboard
cliconfig: defer validation of relative plugin_cache_dir (#36881)
Overview
Running terraform -chdir=dir init with a relative
TF_PLUGIN_CACHE_DIR prints a spurious error even though the cache
works correctly. This PR implements a two-phase validation approach
to defer relative path validation until after -chdir is processed.
Reproduction Steps
- mkdir cache test-project && cd test-project
- echo 'terraform { required_providers { null = { source = "hashicorp/null" } } }' > main.tf
- export TF_PLUGIN_CACHE_DIR=../cache
- terraform -chdir=. init # works but prints false error
Fix
- Early validation: Skip stat() check for relative PluginCacheDir paths in cliconfig.Validate()
- Post-chdir validation: Added secondary validation in main.go after -chdir is processed
-
Comprehensive testing:
- Added unit tests for both absolute and relative path validation
- Added integration test: TestPluginCacheDirWithChdir
Benefits
- ✅ Eliminates false positive errors for valid relative cache paths
- ✅ Maintains proper error detection for invalid relative paths
- ✅ Preserves existing behavior for absolute paths
- ✅ Minimal changes with maximum compatibility
Fixes #36881
Rollback Plan
- [x] If a change needs to be reverted, we will roll out an update to the code within 7 days.
Changes to Security Controls
None.
CHANGELOG entry
- [x] This change is user-facing and I added a changelog entry.
Thanks for this submission, I will raise it in triage.