From d42d83572b6362b934ab2850e783474671b7d2a0 Mon Sep 17 00:00:00 2001 From: Alisdair McDiarmid Date: Mon, 25 Oct 2021 11:28:02 -0400 Subject: [PATCH] cli: Fix backend init failure with deleted cache When an explicit backend is configured with a configuration which has not yet been initialized, running `terraform init` performs a state migration to fetch the remotely stored state in order to operate on it. Like the previous bug introduced by the recent provider diagnostics change, this code path was not correctly configured to enable init mode for the backend, which resulted in a fatal error during init when the cache dir is deleted. Setting the `Init` backend option allows this code path to continue without error when first initializing the backend for state migration. The new e2e test fails without this change. --- .../command/e2etest/providers_tamper_test.go | 44 +++++++++++++++++++ internal/command/meta_backend.go | 4 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/internal/command/e2etest/providers_tamper_test.go b/internal/command/e2etest/providers_tamper_test.go index 5335c66f9..03026354a 100644 --- a/internal/command/e2etest/providers_tamper_test.go +++ b/internal/command/e2etest/providers_tamper_test.go @@ -78,6 +78,42 @@ func TestProviderTampering(t *testing.T) { t.Fatalf("unexpected plan error: %s\nstderr:\n%s", err, stderr) } }) + t.Run("cache dir totally gone, explicit backend", func(t *testing.T) { + tf := e2e.NewBinary(terraformBin, seedDir) + defer tf.Close() + workDir := tf.WorkDir() + + err := ioutil.WriteFile(filepath.Join(workDir, "backend.tf"), []byte(localBackendConfig), 0600) + if err != nil { + t.Fatal(err) + } + + err = os.RemoveAll(filepath.Join(workDir, ".terraform")) + if err != nil { + t.Fatal(err) + } + + stdout, stderr, err := tf.Run("plan") + if err == nil { + t.Fatalf("unexpected plan success\nstdout:\n%s", stdout) + } + if want := `Initial configuration of the requested backend "local"`; !strings.Contains(stderr, want) { + t.Errorf("missing expected error message\nwant substring: %s\ngot:\n%s", want, stderr) + } + if want := `terraform init`; !strings.Contains(stderr, want) { + t.Errorf("missing expected error message\nwant substring: %s\ngot:\n%s", want, stderr) + } + + // Running init as suggested resolves the problem + _, stderr, err = tf.Run("init") + if err != nil { + t.Fatalf("unexpected init error: %s\nstderr:\n%s", err, stderr) + } + _, stderr, err = tf.Run("plan") + if err != nil { + t.Fatalf("unexpected plan error: %s\nstderr:\n%s", err, stderr) + } + }) t.Run("null plugin package modified before plan", func(t *testing.T) { tf := e2e.NewBinary(terraformBin, seedDir) defer tf.Close() @@ -229,3 +265,11 @@ func TestProviderTampering(t *testing.T) { } }) } + +const localBackendConfig = ` +terraform { + backend "local" { + path = "terraform.tfstate" + } +} +` diff --git a/internal/command/meta_backend.go b/internal/command/meta_backend.go index 2524ccb28..71bf6035c 100644 --- a/internal/command/meta_backend.go +++ b/internal/command/meta_backend.go @@ -745,7 +745,7 @@ func (m *Meta) backend_c_r_S(c *configs.Backend, cHash int, sMgr *clistate.Local m.Ui.Output(fmt.Sprintf(strings.TrimSpace(outputBackendMigrateLocal), s.Backend.Type)) // Grab a purely local backend to get the local state if it exists - localB, diags := m.Backend(&BackendOpts{ForceLocal: true}) + localB, diags := m.Backend(&BackendOpts{ForceLocal: true, Init: true}) if diags.HasErrors() { return nil, diags } @@ -799,7 +799,7 @@ func (m *Meta) backend_C_r_s(c *configs.Backend, cHash int, sMgr *clistate.Local } // Grab a purely local backend to get the local state if it exists - localB, localBDiags := m.Backend(&BackendOpts{ForceLocal: true}) + localB, localBDiags := m.Backend(&BackendOpts{ForceLocal: true, Init: true}) if localBDiags.HasErrors() { diags = diags.Append(localBDiags) return nil, diags