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
This commit is contained in:
Kristin Laemmert 2019-05-28 17:27:38 -04:00 committed by GitHub
parent d3506b774f
commit 9869fbc5c4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 24 additions and 14 deletions

View File

@ -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
}