Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: variables evaluated correctly when module expands based on another module #2606

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions internal/hcl/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ func (blocks Blocks) Matching(pattern BlockMatcher) *Block {
search = blocks.OfType(pattern.Type)
}

if pattern.Label == "" && len(search) > 0 {
return search[0]
}

for _, block := range search {
label := block.Label()
if pattern.StripCount {
Expand All @@ -171,10 +175,6 @@ func (blocks Blocks) Matching(pattern BlockMatcher) *Block {
}
}

if len(search) > 0 {
return search[0]
}

return nil
}

Expand Down Expand Up @@ -571,7 +571,7 @@ func (b *Block) IsForEachReferencedExpanded(moduleBlocks Blocks) bool {

label := r.String()
if blockType == "module" {
label = r.typeLabel
label = r.nameLabel
}

referenced := moduleBlocks.Matching(BlockMatcher{
Expand Down
17 changes: 15 additions & 2 deletions internal/hcl/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,21 @@ func (e *Evaluator) evaluateModules() {
if v := moduleCall.Module.Key(); v != nil {
e.ctx.Set(outputs, "module", stripCount(moduleCall.Name), *v)
} else if v := moduleCall.Module.Index(); v != nil {
e.ctx.Set(outputs, "module", stripCount(moduleCall.Name), fmt.Sprintf("%d", *v))
existing := e.ctx.Get("module", stripCount(moduleCall.Name))

var list []cty.Value
if isList(existing) {
list = existing.AsValueSlice()
}

// Pad the list if the index is greater than the current length.
for int64(len(list)) <= *v {
list = append(list, cty.DynamicVal)
}

list[*v] = outputs

e.ctx.Set(cty.TupleVal(list), "module", stripCount(moduleCall.Name))
} else {
e.ctx.Set(outputs, "module", moduleCall.Name)
}
Expand Down Expand Up @@ -1048,5 +1062,4 @@ func ExpFunctions(baseDir string, logger *logrus.Entry) map[string]function.Func
"yamlencode": yaml.YAMLEncodeFunc,
"zipmap": stdlib.ZipmapFunc,
}

}