@@ -209,7 +209,8 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
209
209
tfResourcesByLabel := map [string ]map [string ]* tfjson.StateResource {}
210
210
211
211
// Map resource IDs to labels for efficient lookup when processing metadata
212
- labelByResourceID := map [string ]string {}
212
+ // Multiple resources can have the same ID, so we store a slice of labels
213
+ labelsByResourceID := map [string ][]string {}
213
214
214
215
// Extra array to preserve the order of rich parameters.
215
216
tfResourcesRichParameters := make ([]* tfjson.StateResource , 0 )
@@ -237,10 +238,10 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
237
238
}
238
239
tfResourcesByLabel [label ][resource.Address ] = resource
239
240
240
- // Build the ID to label map
241
+ // Build the ID to labels map - multiple resources can have the same ID
241
242
if idAttr , hasID := resource .AttributeValues ["id" ]; hasID {
242
243
if idStr , ok := idAttr .(string ); ok && idStr != "" {
243
- labelByResourceID [idStr ] = label
244
+ labelsByResourceID [idStr ] = append ( labelsByResourceID [ idStr ], label )
244
245
}
245
246
}
246
247
}
@@ -700,9 +701,16 @@ func ConvertState(ctx context.Context, modules []*tfjson.StateModule, rawGraph s
700
701
// First, check if ResourceID is provided and try to find the resource by ID
701
702
if attrs .ResourceID != "" {
702
703
// Look for a resource with matching ID
703
- foundLabel , foundByID := labelByResourceID [attrs .ResourceID ]
704
- if foundByID {
705
- targetLabel = foundLabel
704
+ foundLabels := labelsByResourceID [attrs .ResourceID ]
705
+ if len (foundLabels ) == 1 {
706
+ // Single match - use it
707
+ targetLabel = foundLabels [0 ]
708
+ } else if len (foundLabels ) > 1 {
709
+ // Multiple resources with same ID - this creates ambiguity
710
+ logger .Warn (ctx , "multiple resources found with same resource_id, falling back to graph traversal" ,
711
+ slog .F ("resource_id" , attrs .ResourceID ),
712
+ slog .F ("metadata_address" , resource .Address ),
713
+ slog .F ("matching_labels" , foundLabels ))
706
714
} else {
707
715
// If we couldn't find by ID, fall back to graph traversal
708
716
logger .Warn (ctx , "coder_metadata resource_id not found, falling back to graph traversal" ,
0 commit comments