command: Fix stale lock after running add

This commit is contained in:
Alisdair McDiarmid 2021-08-20 13:22:11 -04:00
parent 498dc36635
commit 70a4f7a6b6
2 changed files with 12 additions and 0 deletions

View File

@ -106,6 +106,14 @@ func (c *AddCommand) Run(rawArgs []string) int {
return 1
}
// Successfully creating the context can result in a lock, so ensure we release it
defer func() {
diags := opReq.StateLocker.Unlock()
if diags.HasErrors() {
c.showDiagnostics(diags)
}
}()
// load the configuration to verify that the resource address doesn't
// already exist in the config.
var module *configs.Module

View File

@ -3,6 +3,7 @@ package command
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
@ -592,4 +593,7 @@ resource "test_instance" "new" {
t.Fatalf("wrong output:\n%s", cmp.Diff(expected, output.Stdout()))
}
if _, err := os.Stat(filepath.Join(td, ".terraform.tfstate.lock.info")); !os.IsNotExist(err) {
t.Fatal("state left locked after add")
}
}