@@ -185,7 +185,8 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
185
185
tfResourcesByLabel := map [string ]map [string ]* tfjson.StateResource {}
186
186
187
187
// Map resource IDs to labels for efficient lookup when processing metadata
188
- labelByResourceID := map [string ]string {}
188
+ // Multiple resources can have the same ID, so we store a slice of labels
189
+ labelsByResourceID := map [string ][]string {}
189
190
190
191
// Extra array to preserve the order of rich parameters.
191
192
tfResourcesRichParameters := make ([]* tfjson.StateResource , 0 )
@@ -209,10 +210,10 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
209
210
}
210
211
tfResourcesByLabel [label ][resource.Address ] = resource
211
212
212
- // Build the ID to label map
213
+ // Build the ID to labels map - multiple resources can have the same ID
213
214
if idAttr , hasID := resource .AttributeValues ["id" ]; hasID {
214
215
if idStr , ok := idAttr .(string ); ok && idStr != "" {
215
- labelByResourceID [idStr ] = label
216
+ labelsByResourceID [idStr ] = append ( labelsByResourceID [ idStr ], label )
216
217
}
217
218
}
218
219
}
@@ -662,9 +663,16 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
662
663
// First, check if ResourceID is provided and try to find the resource by ID
663
664
if attrs .ResourceID != "" {
664
665
// Look for a resource with matching ID
665
- foundLabel , foundByID := labelByResourceID [attrs .ResourceID ]
666
- if foundByID {
667
- targetLabel = foundLabel
666
+ foundLabels := labelsByResourceID [attrs .ResourceID ]
667
+ if len (foundLabels ) == 1 {
668
+ // Single match - use it
669
+ targetLabel = foundLabels [0 ]
670
+ } else if len (foundLabels ) > 1 {
671
+ // Multiple resources with same ID - this creates ambiguity
672
+ logger .Warn (ctx , "multiple resources found with same resource_id, falling back to graph traversal" ,
673
+ slog .F ("resource_id" , attrs .ResourceID ),
674
+ slog .F ("metadata_address" , resource .Address ),
675
+ slog .F ("matching_labels" , foundLabels ))
668
676
} else {
669
677
// If we couldn't find by ID, fall back to graph traversal
670
678
logger .Warn (ctx , "coder_metadata resource_id not found, falling back to graph traversal" ,
0 commit comments