diff --git a/bundle/deploy/terraform/import.go b/bundle/deploy/terraform/import.go index 48ad622c6c..474aaa2305 100644 --- a/bundle/deploy/terraform/import.go +++ b/bundle/deploy/terraform/import.go @@ -44,6 +44,7 @@ func (m *importResource) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn if err != nil { return diag.Errorf("terraform init: %v", err) } + defer os.RemoveAll(tmpDir) relPath, _ := b.StateFilenameTerraform(ctx) tmpState := filepath.Join(tmpDir, filepath.Base(relPath)) @@ -62,8 +63,6 @@ func (m *importResource) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn return diag.Errorf("terraform plan: %v", err) } - defer os.RemoveAll(tmpDir) - if changed && !m.opts.AutoApprove { output := buf.String() // Remove output starting from Warning until end of output, if present. @@ -103,6 +102,12 @@ func (m *importResource) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn return diag.FromErr(err) } + // A failed Close can mean a truncated state file; the deferred Close above covers error paths. + err = f.Close() + if err != nil { + return diag.FromErr(err) + } + return diags }