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.
This commit is contained in:
Mitchell Hashimoto 2017-03-16 20:14:20 -07:00
parent 31c0f33431
commit 68ee4e0480
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 19 additions and 2 deletions

View File

@ -0,0 +1,3 @@
resource "null_resource" "var" {
key = "${module.unknown.value}"
}

View File

@ -354,8 +354,10 @@ func (t *Tree) Validate() error {
tree, ok := children[mv.Name] tree, ok := children[mv.Name]
if !ok { if !ok {
// This should never happen because Load watches us newErr.Add(fmt.Errorf(
panic("module not found in children: " + mv.Name) "%s: undefined module referenced %s",
source, mv.Name))
continue
} }
found := false found := false

View File

@ -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 = ` const treeLoadStr = `
root root
foo (path: foo) foo (path: foo)