Skip to content

Commit

Permalink
fix: don't use existing context when evaluating resources, data or pr…
Browse files Browse the repository at this point in the history
…ovider blocks

The SetByDot function handles the merging anyway, so we don't need to pull out the existing values. I've left the old functionality in the `evaluateResource` and `evaluatorProvider` functions because the legacy evaluator needs it.

This also fixes an issue where the data blocks were using `e.ctx.Get(b.TypeLabel())` which was getting the wrong context. The correct functionality would have been to use `e.ctx.Get("data")`. Otherwise, this caused issues where the type of a data block matched the name of a resource, e.g. `data.aws_lb.anything` and `aws_lb.aws_lb`. In this case it would pull the context of the resource, which could cause a panic if the resource had been expanded, because it would be an array instead of the expected map.
  • Loading branch information
aliscott committed May 23, 2024
1 parent f2485a4 commit 4938f49
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 28 deletions.
11 changes: 2 additions & 9 deletions internal/hcl/graph_vertex_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,8 @@ func (v *VertexData) evaluate(e *Evaluator, b *Block) error {
return fmt.Errorf("data block %s has no label", v.ID())
}

var existingVals map[string]cty.Value
existingCtx := e.ctx.Get(b.TypeLabel())
if !existingCtx.IsNull() {
existingVals = existingCtx.AsValueMap()
} else {
existingVals = make(map[string]cty.Value)
}

val := e.evaluateResource(b, existingVals)
// We don't care about the existing values, this is only needed by the legacy evaluator
val := e.evaluateResource(b, map[string]cty.Value{})

v.logger.Debug().Msgf("adding data %s to the evaluation context", v.ID())
e.ctx.SetByDot(val, fmt.Sprintf("data.%s", b.TypeLabel()))
Expand Down
13 changes: 3 additions & 10 deletions internal/hcl/graph_vertex_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,11 @@ func (v *VertexProvider) Visit(mutex *sync.Mutex) error {
return fmt.Errorf("could not find block %q in module %q", v.ID(), moduleInstance.name)
}

var existingVals map[string]cty.Value
existingCtx := e.ctx.Get(blockInstance.TypeLabel())
if !existingCtx.IsNull() {
existingVals = existingCtx.AsValueMap()
} else {
existingVals = make(map[string]cty.Value)
}

val := e.evaluateProvider(blockInstance, existingVals)
// We don't care about the existing values, this is only needed by the legacy evaluator
val := e.evaluateProvider(blockInstance, map[string]cty.Value{})

v.logger.Debug().Msgf("adding %s to the evaluation context", v.ID())
e.ctx.Set(val, blockInstance.Label())
e.ctx.SetByDot(val, blockInstance.Label())

e.AddFilteredBlocks(blockInstance)
}
Expand Down
11 changes: 2 additions & 9 deletions internal/hcl/graph_vertex_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,8 @@ func (v *VertexResource) evaluate(e *Evaluator, b *Block) error {
return fmt.Errorf("resource block %s has no label", v.ID())
}

var existingVals map[string]cty.Value
existingCtx := e.ctx.Get(b.TypeLabel())
if !existingCtx.IsNull() {
existingVals = existingCtx.AsValueMap()
} else {
existingVals = make(map[string]cty.Value)
}

val := e.evaluateResource(b, existingVals)
// We don't care about the existing values, this is only needed by the legacy evaluator
val := e.evaluateResource(b, map[string]cty.Value{})

v.logger.Debug().Msgf("adding resource %s to the evaluation context", v.ID())
e.ctx.SetByDot(val, b.TypeLabel())
Expand Down

0 comments on commit 4938f49

Please sign in to comment.