diff --git a/addrs/module_instance.go b/addrs/module_instance.go index 49cbf7863..75c69254a 100644 --- a/addrs/module_instance.go +++ b/addrs/module_instance.go @@ -414,9 +414,26 @@ func (m ModuleInstance) TargetContains(other Targetable) bool { } for i, ourStep := range m { otherStep := to[i] - if ourStep != otherStep { + + if ourStep.Name != otherStep.Name { return false } + + // if this is our last step, because all targets are parsed as + // instances, this may be a ModuleInstance intended to be used as a + // Module. + if i == len(m)-1 { + if ourStep.InstanceKey == NoKey { + // If the other step is a keyed instance, then we contain that + // step, and if it isn't it's a match, which is true either way + return true + } + } + + if ourStep.InstanceKey != otherStep.InstanceKey { + return false + } + } return true diff --git a/addrs/target_test.go b/addrs/target_test.go index be54f9427..69792c974 100644 --- a/addrs/target_test.go +++ b/addrs/target_test.go @@ -20,13 +20,6 @@ func TestTargetContains(t *testing.T) { mustParseTarget("module.foo"), true, }, - { - // module.foo is an unkeyed module instance here, so it cannot - // contain another instance - mustParseTarget("module.foo"), - mustParseTarget("module.foo[0]"), - false, - }, { RootModuleInstance, mustParseTarget("module.foo"), @@ -99,6 +92,11 @@ func TestTargetContains(t *testing.T) { mustParseTarget("module.bar.test_resource.foo[0]"), true, }, + { + mustParseTarget("module.bax"), + mustParseTarget("module.bax[0].test_resource.foo[0]"), + true, + }, // Config paths, while never returned from parsing a target, must still // be targetable