diff --git a/config/raw_config.go b/config/raw_config.go index 1c6d7c972..1854a8b20 100644 --- a/config/raw_config.go +++ b/config/raw_config.go @@ -111,6 +111,10 @@ func (r *RawConfig) Copy() *RawConfig { r.lock.Lock() defer r.lock.Unlock() + if r.Body != nil { + return NewRawConfigHCL2(r.Body) + } + newRaw := make(map[string]interface{}) for k, v := range r.Raw { newRaw[k] = v diff --git a/config/raw_config_test.go b/config/raw_config_test.go index c6f7b496d..5884a25e4 100644 --- a/config/raw_config_test.go +++ b/config/raw_config_test.go @@ -5,6 +5,7 @@ import ( "reflect" "testing" + hcl2 "github.com/hashicorp/hcl2/hcl" "github.com/hashicorp/hil/ast" ) @@ -435,6 +436,18 @@ func TestRawConfigCopy(t *testing.T) { } } +func TestRawConfigCopyHCL2(t *testing.T) { + rc := NewRawConfigHCL2(hcl2.EmptyBody()) + rc2 := rc.Copy() + + if rc.Body == nil { + t.Errorf("RawConfig copy has a nil Body") + } + if rc2.Raw != nil { + t.Errorf("RawConfig copy got a non-nil Raw") + } +} + func TestRawConfigValue(t *testing.T) { raw := map[string]interface{}{ "foo": "${var.bar}",