diff --git a/terraform/shadow_eval_context.go b/terraform/shadow_eval_context.go index e39fc14db..0c962c433 100644 --- a/terraform/shadow_eval_context.go +++ b/terraform/shadow_eval_context.go @@ -229,7 +229,12 @@ func (c *shadowEvalContextShadow) Provider(n string) ResourceProvider { func (c *shadowEvalContextShadow) CloseProvider(n string) error { // Wait for the provider value - raw := c.Shared.Providers.Value(n) + raw, ok := c.Shared.Providers.ValueOk(n) + if !ok { + c.err(fmt.Errorf( + "CloseProvider called for uninitialized provider %q", n)) + return nil + } if raw == nil { c.err(fmt.Errorf( "Unknown 'CloseProvider' call for %q", n)) diff --git a/terraform/shadow_eval_context_test.go b/terraform/shadow_eval_context_test.go index 2ef5e2c09..08db93a9f 100644 --- a/terraform/shadow_eval_context_test.go +++ b/terraform/shadow_eval_context_test.go @@ -234,3 +234,24 @@ func TestShadowEvalContextCloseProvider_noInitClose(t *testing.T) { t.Fatal("should error") } } + +func TestShadowEvalContextCloseProvider_noCreate(t *testing.T) { + mock := new(MockEvalContext) + _, shadow := NewShadowEvalContext(mock) + + // Args, results + name := "foo" + mockResult := new(MockResourceProvider) + + // Configure the mock + mock.InitProviderProvider = mockResult + + // Close the provider + if err := shadow.CloseProvider(name); err != nil { + t.Fatalf("err: %s", err) + } + + if err := shadow.CloseShadow(); err == nil { + t.Fatal("should error") + } +}