From 0d620018fe01edba30eb233cc2c75f757757ab53 Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Tue, 19 May 2020 15:32:36 -0400 Subject: [PATCH] provider cache: log errors and validate dir exists (#24993) * providercache: add logging for errors from getproviders.SearchLocalDirectory providercache.fillMetaCache() was silently swallowing errors when searching the cache directory. This commit logs the error without changing the behavior otherwise. * command/cliconfig: validate plugin cache dir exists The plugin cache directory must exist for terraform to use it, so we will add a check at the begining. --- command/cliconfig/cliconfig.go | 9 +++++++++ command/cliconfig/cliconfig_test.go | 6 ++++++ internal/providercache/dir.go | 1 + 3 files changed, 16 insertions(+) diff --git a/command/cliconfig/cliconfig.go b/command/cliconfig/cliconfig.go index 2aecfb9d12..7ac6eeb0be 100644 --- a/command/cliconfig/cliconfig.go +++ b/command/cliconfig/cliconfig.go @@ -273,6 +273,15 @@ 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), + ) + } + } + return diags } diff --git a/command/cliconfig/cliconfig_test.go b/command/cliconfig/cliconfig_test.go index a84e825b3b..d09ffff7b7 100644 --- a/command/cliconfig/cliconfig_test.go +++ b/command/cliconfig/cliconfig_test.go @@ -193,6 +193,12 @@ func TestConfigValidate(t *testing.T) { }, 1, // no more than one provider_installation block allowed }, + "plugin_cache_dir does not exist": { + &Config{ + PluginCacheDir: "fake", + }, + 1, // The specified plugin cache dir %s cannot be opened + }, } for name, test := range tests { diff --git a/internal/providercache/dir.go b/internal/providercache/dir.go index 1c1ade7b73..72688f92ec 100644 --- a/internal/providercache/dir.go +++ b/internal/providercache/dir.go @@ -136,6 +136,7 @@ func (d *Dir) fillMetaCache() error { allData, err := getproviders.SearchLocalDirectory(d.baseDir) if err != nil { + log.Printf("[TRACE] providercache.fillMetaCache: error while scanning directory %s: %s", d.baseDir, err) return err }