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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changes/v1.13/BUG FIXES-20250524-035653.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: BUG FIXES
body: suppress false-positive plugin_cache_dir error when -chdir is used
time: 2025-05-24T03:56:53.347808+09:00
custom:
Issue: "36881"
16 changes: 11 additions & 5 deletions internal/command/cliconfig/cliconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,11 +325,17 @@ func (c *Config) Validate() tfdiags.Diagnostics {
}

if c.PluginCacheDir != "" {
_, err := os.Stat(c.PluginCacheDir)
if err != nil {
diags = diags.Append(
fmt.Errorf("The specified plugin cache dir %s cannot be opened: %s", c.PluginCacheDir, err),
)
// Skip validation for relative paths here, as they may be intended
// to be resolved relative to a directory specified by -chdir, which
// is processed after config loading. Relative paths will be
// re-validated later in main.go after any -chdir option is processed.
if filepath.IsAbs(c.PluginCacheDir) {
_, err := os.Stat(c.PluginCacheDir)
if err != nil {
diags = diags.Append(
fmt.Errorf("The specified plugin cache dir %s cannot be opened: %s", c.PluginCacheDir, err),
)
}
}
}

Expand Down
10 changes: 8 additions & 2 deletions internal/command/cliconfig/cliconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,18 @@ func TestConfigValidate(t *testing.T) {
},
1, // no more than one provider_installation block allowed
},
"plugin_cache_dir does not exist": {
"plugin_cache_dir absolute path does not exist": {
&Config{
PluginCacheDir: "fake",
PluginCacheDir: "/absolute/fake/path",
},
1, // The specified plugin cache dir %s cannot be opened
},
"plugin_cache_dir relative path": {
&Config{
PluginCacheDir: "../relative/fake/path",
},
0, // Relative paths are not validated in early validation phase
},
}

for name, test := range tests {
Expand Down
22 changes: 22 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/hashicorp/terraform/internal/httpclient"
"github.com/hashicorp/terraform/internal/logging"
"github.com/hashicorp/terraform/internal/terminal"
"github.com/hashicorp/terraform/internal/tfdiags"
"github.com/hashicorp/terraform/version"
"github.com/mattn/go-shellwords"
"github.com/mitchellh/colorstring"
Expand Down Expand Up @@ -238,6 +239,27 @@ func realMain() int {
Ui.Error(fmt.Sprintf("Error handling -chdir option: %s", err))
return 1
}

// After changing the working directory with -chdir, we need to re-validate
// any relative plugin cache directory paths, since they are resolved relative
// to the current working directory.
if pluginCacheDir := config.PluginCacheDir; pluginCacheDir != "" && !filepath.IsAbs(pluginCacheDir) {
if _, err := os.Stat(pluginCacheDir); err != nil {
Ui.Error("There are some problems with the CLI configuration:")
earlyColor := &colorstring.Colorize{
Colors: colorstring.DefaultColors,
Disable: true,
Reset: true,
}
diag := tfdiags.Sourceless(
tfdiags.Error,
"Invalid plugin cache directory",
fmt.Sprintf("The specified plugin cache dir %s cannot be opened: %s", pluginCacheDir, err),
)
Ui.Error(format.Diagnostic(diag, nil, earlyColor, 78))
Ui.Error("As a result of the above problems, Terraform may not behave as intended.\n\n")
}
}
}

// In tests, Commands may already be set to provide mock commands
Expand Down
57 changes: 57 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ package main
import (
"fmt"
"os"
"path/filepath"
"reflect"
"testing"

"github.com/hashicorp/cli"
"github.com/hashicorp/terraform/internal/command/cliconfig"
)

func TestMain_cliArgsFromEnv(t *testing.T) {
Expand Down Expand Up @@ -329,3 +331,58 @@ func TestWarnOutput(t *testing.T) {
t.Fatalf("unexpected stdout: %q\n", stdout)
}
}

func TestPluginCacheDirWithChdir(t *testing.T) {
// Create a temporary directory structure for testing
tmpDir := t.TempDir()
subDir := filepath.Join(tmpDir, "subdir")
cacheDir := filepath.Join(tmpDir, "cache")

if err := os.MkdirAll(subDir, 0755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(cacheDir, 0755); err != nil {
t.Fatal(err)
}

// Save original working directory
originalWd, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(originalWd)

// Change to tmpDir
if err := os.Chdir(tmpDir); err != nil {
t.Fatal(err)
}

// Test case 1: Relative path that exists
config := &cliconfig.Config{
PluginCacheDir: "../cache",
}

// Change to subdir (simulating -chdir)
if err := os.Chdir(subDir); err != nil {
t.Fatal(err)
}

// Validate relative path
if pluginCacheDir := config.PluginCacheDir; pluginCacheDir != "" && !filepath.IsAbs(pluginCacheDir) {
if _, err := os.Stat(pluginCacheDir); err != nil {
t.Errorf("Expected relative plugin cache dir to be accessible after chdir, but got error: %v", err)
}
}

// Test case 2: Relative path that doesn't exist
config2 := &cliconfig.Config{
PluginCacheDir: "../nonexistent",
}

// Validate non-existent relative path
if pluginCacheDir := config2.PluginCacheDir; pluginCacheDir != "" && !filepath.IsAbs(pluginCacheDir) {
if _, err := os.Stat(pluginCacheDir); err == nil {
t.Error("Expected non-existent relative plugin cache dir to fail validation")
}
}
}
Loading