Skip to content

fix(terraform): match indexed references when pruning resource closure - #1

Merged
PushTheLimit merged 2 commits into
PushTheLimit:preview/target-closure-evalfrom
coder:preview/target-closure-eval-indexed-refs
Sep 2, 2026
Merged

fix(terraform): match indexed references when pruning resource closure#1
PushTheLimit merged 2 commits into
PushTheLimit:preview/target-closure-evalfrom
coder:preview/target-closure-eval-indexed-refs

Conversation

@Emyrk

@Emyrk Emyrk commented Sep 2, 2026

Copy link
Copy Markdown

Pruning runs before count/for_each expansion, so a resource block has no key. A reference like my_resource.x[0] does, and Reference.RefersTo treats that as a different block. The resource is pruned even though a target depends on it.

  • Compare block type and labels only when marking resources to keep.
  • Add a test for count + [0] and for_each + ["a"]. Both fail without the fix.

Found reviewing coder#74. Prepared with Coder Agents assistance.

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.
if b.Type() != "resource" || keep[b] {
continue
}
if ref.RefersTo(b.Reference()) {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@PushTheLimit
PushTheLimit merged commit 08c98a3 into PushTheLimit:preview/target-closure-eval Sep 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants