From 226d66bac8d47b02ddc69d5cb7b25b07f0c6a2fc Mon Sep 17 00:00:00 2001 From: Martin Atkins Date: Mon, 30 Apr 2018 17:12:43 -0700 Subject: [PATCH] core: include "root" element when constructing legacy ModuleState path We've not yet adjusted any of the state structs to reflect our new address types because they are used with encoding/json to produce our state file format, but the shimming here previously was incorrect because it failed to include the special "root" string that's always required at element zero of a module path in the state. --- terraform/state.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/terraform/state.go b/terraform/state.go index 70702730d..eddbcffac 100644 --- a/terraform/state.go +++ b/terraform/state.go @@ -175,7 +175,13 @@ func (s *State) addModule(path addrs.ModuleInstance) *ModuleState { // This requires that none of the steps have instance keys, which is // true for all addresses at the time of implementing this because // "count" and "for_each" are not yet implemented for modules. - legacyPath := make([]string, len(path)) + // For the purposes of state, the legacy address format also includes + // a redundant extra prefix element "root". It is important to include + // this because the "prune" method will remove any module that has a + // path length less than one, and other parts of the state code will + // trim off the first element indiscriminately. + legacyPath := make([]string, len(path)+1) + legacyPath[0] = "root" for i, step := range path { if step.InstanceKey != addrs.NoKey { // FIXME: Once the rest of Terraform is ready to use count and @@ -184,7 +190,7 @@ func (s *State) addModule(path addrs.ModuleInstance) *ModuleState { panic("state cannot represent modules with count or for_each keys") } - legacyPath[i] = step.Name + legacyPath[i+1] = step.Name } m = &ModuleState{Path: legacyPath}