fix(terraform): match indexed references when pruning resource closure - #1
Merged
PushTheLimit merged 2 commits intoSep 2, 2026
Conversation
A resource referenced only as my_resource.x[0] or my_resource.x["k"] is currently pruned because Reference.RefersTo treats the reference key as significant while the unexpanded resource block has none. This test fails on the PR head and documents the expected behavior.
Pruning runs before count/for_each expansion, so resource blocks carry no key while references like my_resource.x[0] do. Reference.RefersTo treats that mismatch as a different block, pruning resources the target closure depends on. Compare block type and labels only.
Emyrk
commented
Sep 2, 2026
| if b.Type() != "resource" || keep[b] { | ||
| continue | ||
| } | ||
| if ref.RefersTo(b.Reference()) { |
Author
There was a problem hiding this comment.
ref.RefersTo has this comparison:
if (r.Key() != "" || other.Key() != "") && r.Key() != other.Key() {
return false
}At prune time, the blocks are not expanded.
So we are comparing against an expanded key (empty string, "")
This change ignores any key elements, erroring on the side of keeping entire blocks if a single element of the block expansion is referenced
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pruning runs before count/for_each expansion, so a resource block has no key. A reference like
my_resource.x[0]does, andReference.RefersTotreats that as a different block. The resource is pruned even though a target depends on it.count+[0]andfor_each+["a"]. Both fail without the fix.Found reviewing coder#74. Prepared with Coder Agents assistance.