Skip to content

Commit

Permalink
run PrePlanBlock prior to other blocks to solve panic caused by calli…
Browse files Browse the repository at this point in the history
…ng variable in for_each
  • Loading branch information
lonegunmanb committed Jun 12, 2024
1 parent 1ca06f9 commit bf7f4f0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
16 changes: 14 additions & 2 deletions dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,20 @@ func (d *Dag) addEdge(from, to string) error {
func (d *Dag) runDag(c Config, onReady func(Block) error) error {
var err error
pending := linkedlistqueue.New()
for _, n := range d.GetRoots() {
pending.Enqueue(n.(Block))
var prePlanBlocks, otherBlocks []Block
for _, v := range d.GetRoots() {
b := v.(Block)
if _, ok := b.(PrePlanBlock); ok {
prePlanBlocks = append(prePlanBlocks, b)
continue
}
otherBlocks = append(otherBlocks, b)
}
for _, b := range prePlanBlocks {
pending.Enqueue(b)
}
for _, b := range otherBlocks {
pending.Enqueue(b)
}
for !pending.Empty() {
next, _ := pending.Dequeue()
Expand Down
23 changes: 23 additions & 0 deletions for_each_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package golden

import (
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"testing"
)
Expand Down Expand Up @@ -49,6 +50,28 @@ func (s *forEachTestSuite) TestForEachBlockWithAttributeThatHasDefaultValue() {
}
}

func (s *forEachTestSuite) TestForEachBlockInvolvingVariable() {
// The order of blocks is crucial here. The block with the variable must be defined first
config := `
data "dummy" "sample" {
for_each = var.numbers
}
variable "numbers" {
type = set(number)
}
`
s.dummyFsWithFiles(map[string]string{
"test.hcl": config,
})
c, err := BuildDummyConfig("", "", []CliFlagAssignedVariables{
NewCliFlagAssignedVariable("numbers", "[1]"),
}, nil)
require.NoError(s.T(), err)
_, err = RunDummyPlan(c)
s.NoError(err)
}

func (s *forEachTestSuite) TestLocals_locals_as_for_each() {
code := `
locals {
Expand Down

0 comments on commit bf7f4f0

Please sign in to comment.