From 9869fbc5c461a2bc7718c4fcd9cfde707c361f9f Mon Sep 17 00:00:00 2001 From: Kristin Laemmert Date: Tue, 28 May 2019 17:27:38 -0400 Subject: [PATCH] core: don't panic in NodeAbstractResourceInstance References() (#21445) * core: don't panic in NodeAbstractResourceInstance References() It is possible for s.Current to be nil. This was hard to reproduce, so the root cause is still unknown, but we can guard against the symptom. * add log statement --- terraform/node_resource_abstract.go | 38 ++++++++++++++++++----------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/terraform/node_resource_abstract.go b/terraform/node_resource_abstract.go index 3a0570c5b..514845585 100644 --- a/terraform/node_resource_abstract.go +++ b/terraform/node_resource_abstract.go @@ -238,21 +238,31 @@ func (n *NodeAbstractResourceInstance) References() []*addrs.Reference { // need to do a little work here to massage this to the form we now // want. var result []*addrs.Reference - for _, addr := range s.Current.Dependencies { - if addr == nil { - // Should never happen; indicates a bug in the state loader - panic(fmt.Sprintf("dependencies for current object on %s contains nil address", n.ResourceInstanceAddr())) - } - // This is a little weird: we need to manufacture an addrs.Reference - // with a fake range here because the state isn't something we can - // make source references into. - result = append(result, &addrs.Reference{ - Subject: addr, - SourceRange: tfdiags.SourceRange{ - Filename: "(state file)", - }, - }) + // It is (apparently) possible for s.Current to be nil. This proved + // difficult to reproduce, so we will fix the symptom here and hope + // to find the root cause another time. + // + // https://github.com/hashicorp/terraform/issues/21407 + if s.Current == nil { + log.Printf("[WARN] no current state found for %s", n.Name()) + } else { + for _, addr := range s.Current.Dependencies { + if addr == nil { + // Should never happen; indicates a bug in the state loader + panic(fmt.Sprintf("dependencies for current object on %s contains nil address", n.ResourceInstanceAddr())) + } + + // This is a little weird: we need to manufacture an addrs.Reference + // with a fake range here because the state isn't something we can + // make source references into. + result = append(result, &addrs.Reference{ + Subject: addr, + SourceRange: tfdiags.SourceRange{ + Filename: "(state file)", + }, + }) + } } return result }