terraform icon indicating copy to clipboard operation
terraform copied to clipboard

cliconfig: defer validation of relative plugin_cache_dir (#36881)

Open Narita-1095305 opened this issue 7 months ago • 2 comments

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

  1. mkdir cache test-project && cd test-project
  2. echo 'terraform { required_providers { null = { source = "hashicorp/null" } } }' > main.tf
  3. export TF_PLUGIN_CACHE_DIR=../cache
  4. 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.

Narita-1095305 avatar May 23 '25 19:05 Narita-1095305

Thanks for this submission, I will raise it in triage.

crw avatar May 27 '25 21:05 crw

CLA assistant check
All committers have signed the CLA.

hashicorp-cla-app[bot] avatar Jun 03 '25 08:06 hashicorp-cla-app[bot]