From 68ee4e0480d4618f3bfe12e77d4f00517443f718 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 16 Mar 2017 20:14:20 -0700 Subject: [PATCH] config/module: don't panic when referencing undefined module Fixes #12788 We would panic when referencing an output from an undefined module. The panic above this is correct but in this case Load will not catch interpolated variables that _reference_ an unloaded/undefined module. Test included. --- .../test-fixtures/validate-module-unknown/main.tf | 3 +++ config/module/tree.go | 6 ++++-- config/module/tree_test.go | 12 ++++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 config/module/test-fixtures/validate-module-unknown/main.tf diff --git a/config/module/test-fixtures/validate-module-unknown/main.tf b/config/module/test-fixtures/validate-module-unknown/main.tf new file mode 100644 index 000000000..29b3c01bc --- /dev/null +++ b/config/module/test-fixtures/validate-module-unknown/main.tf @@ -0,0 +1,3 @@ +resource "null_resource" "var" { + key = "${module.unknown.value}" +} diff --git a/config/module/tree.go b/config/module/tree.go index 8ce24a321..b6f90fd93 100644 --- a/config/module/tree.go +++ b/config/module/tree.go @@ -354,8 +354,10 @@ func (t *Tree) Validate() error { tree, ok := children[mv.Name] if !ok { - // This should never happen because Load watches us - panic("module not found in children: " + mv.Name) + newErr.Add(fmt.Errorf( + "%s: undefined module referenced %s", + source, mv.Name)) + continue } found := false diff --git a/config/module/tree_test.go b/config/module/tree_test.go index 475ce7a7c..87bf1df67 100644 --- a/config/module/tree_test.go +++ b/config/module/tree_test.go @@ -424,6 +424,18 @@ func TestTreeValidate_requiredChildVar(t *testing.T) { } } +func TestTreeValidate_unknownModule(t *testing.T) { + tree := NewTree("", testConfig(t, "validate-module-unknown")) + + if err := tree.Load(testStorage(t), GetModeNone); err != nil { + t.Fatalf("err: %s", err) + } + + if err := tree.Validate(); err == nil { + t.Fatal("should error") + } +} + const treeLoadStr = ` root foo (path: foo)