Skip to content

cliconfig: defer validation of relative plugin_cache_dir (#36881) #37165

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Narita-1095305
Copy link

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

  • 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

  • This change is user-facing and I added a changelog entry.

…6881)

Relative paths are validated again after -chdir has been applied
to avoid false-positive diagnostics. Fixes hashicorp#36881.

Signed-off-by: Rikiya Narita <b2560305@planet.kanazawa-it.ac.jp>
@Narita-1095305 Narita-1095305 requested a review from a team as a code owner May 23, 2025 19:06
@crw crw added the bug label May 27, 2025
@crw
Copy link
Contributor

crw commented May 27, 2025

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

Copy link

hashicorp-cla-app bot commented Jun 3, 2025

CLA assistant check
All committers have signed the CLA.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using a plugin cache with -chdir throws a false error
2 participants