Skip to content

Commit 676cc1c

Browse files
authored
Fix import file generation (#24069)
Fixes #24056 When multiple existing resources had the same logical name, child resources could be written with the wrong parent alias in the generated import file. Name collision tracking was tied to created import resources, and collision repair logic did not fully preserve identity across all references. In particular, duplicate names among existing resources could overwrite alias mappings used later for parent resolution. This rewrote the name collision logic to be based around old logical names + types, regardless of if it was being imported, or just referenced. While at it we also fixed up the back-renaming logic to fix up `Provider` like it already did for `Parent`.
1 parent 76adffb commit 676cc1c

3 files changed

Lines changed: 167 additions & 20 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
component: cli/import
2+
kind: fix
3+
body: Fix import file generation when parent resources share names
4+
time: 2026-07-24T16:39:49.824025259+01:00

‎pkg/cmd/pulumi/operations/preview.go‎

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,13 @@ func buildImportFile(
8888
NameTable: map[string]resource.URN{},
8989
}
9090

91-
// A mapping of names to the index of the resourceSpec in imports.Resources that used it. We have to
92-
// fix up names _as we go_ because we're mapping over the event stream, and it would be pretty
93-
// inefficient to wait for the whole thing to finish before building the import specs.
94-
takenNames := map[string]int{}
91+
// A mapping of names to their logical name and type that used it. We have to fix up names _as we go_
92+
// because we're mapping over the event stream, and it would be pretty inefficient to wait for the
93+
// whole thing to finish before building the import specs.
94+
takenNames := map[string]struct {
95+
name string
96+
typ tokens.Type
97+
}{}
9598

9699
// We want to prefer using the urns name for the source name, but if it conflicts with other resources
97100
// we'll auto suffix it, first with the type, then with rising numbers. This function does that
@@ -119,34 +122,44 @@ func buildImportFile(
119122

120123
urn := preEvent.Metadata.URN
121124
name := urn.Name()
122-
if i, has := takenNames[name]; has {
123-
// Another resource already has this name, lets check if that was it's original name or if it was a rename
124-
importI := imports.Resources[i]
125-
if importI.LogicalName != "" {
126-
// i was renamed, so we're going to go backwards rename it again and then we can use our name for this resource.
127-
newName := uniqueName(importI.LogicalName, importI.Type)
128-
imports.Resources[i].Name = newName
129-
// Go through all the resources and fix up any parent references to use the new name.
125+
if old, has := takenNames[name]; has {
126+
// Another resource already has this name, lets check if that was it's original name or if it was a rename.
127+
if old.name != name {
128+
// old was renamed, so we're going to go backwards rename it again and then we can use our name for this resource.
129+
newName := uniqueName(old.name, old.typ)
130+
131+
// Fix up the name table to reflect the rename.
132+
if urn, has := imports.NameTable[name]; has {
133+
delete(imports.NameTable, name)
134+
imports.NameTable[newName] = urn
135+
}
136+
// Then go through all the resources and fix up any name, parent or provider references to use the new name.
130137
for j := range imports.Resources {
138+
if imports.Resources[j].Name == name {
139+
imports.Resources[j].Name = newName
140+
}
131141
if imports.Resources[j].Parent == name {
132142
imports.Resources[j].Parent = newName
133143
}
134-
}
135-
// Fix up the nametable if needed
136-
if urn, has := imports.NameTable[name]; has {
137-
delete(imports.NameTable, name)
138-
imports.NameTable[newName] = urn
144+
if imports.Resources[j].Provider == name {
145+
imports.Resources[j].Provider = newName
146+
}
139147
}
140148
// Fix up takenNames incase this is hit again
141-
takenNames[newName] = i
149+
takenNames[newName] = takenNames[name]
150+
delete(takenNames, name)
142151
} else {
143-
// i just had the same name as us, lets find a new one
152+
// old just had the same name as us, lets find a new one
144153
name = uniqueName(name, urn.Type())
145154
}
146155
}
147156

148157
// Name is unique at this point
149158
fullNameTable[urn] = name
159+
takenNames[name] = struct {
160+
name string
161+
typ tokens.Type
162+
}{urn.Name(), urn.Type()}
150163

151164
// If this is a provider we need to note we've seen it so we can build the Version and PluginDownloadURL of
152165
// any resources that use it.
@@ -257,7 +270,6 @@ func buildImportFile(
257270
logicalName = urn.Name()
258271
}
259272

260-
takenNames[name] = len(imports.Resources)
261273
imports.Resources = append(imports.Resources, importSpec{
262274
Type: new.Type,
263275
Name: name,

‎pkg/cmd/pulumi/operations/preview_test.go‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,75 @@ func TestBuildImportFile_ExistingProvider(t *testing.T) {
359359
assert.Equal(t, expected, importFile.Resources[0])
360360
}
361361

362+
// TestBuildImportFile_RenamedProviderReference tests that if a provider's generated import-file
363+
// name is renamed after a resource has already referenced it, the resource's Provider field is
364+
// updated to the provider's new name.
365+
func TestBuildImportFile_RenamedProviderReference(t *testing.T) {
366+
t.Parallel()
367+
368+
events := make(chan engine.Event)
369+
importFilePromise := buildImportFile(t.Context(), events, sdkconfig.NopEncrypter)
370+
371+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
372+
Metadata: makeRootStackMetadata(deploy.OpSame),
373+
})
374+
375+
// Occupy the provider's logical name so the provider is initially renamed from "prov" to "provPkg".
376+
existingState := makeStateMetadata(t, "prov", "other:index:Existing", true, stateOptions{})
377+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
378+
Metadata: makeMetadata(deploy.OpSame, existingState),
379+
})
380+
381+
providerState := makeStateMetadata(t, "prov", "pulumi:providers:pkg", true, stateOptions{
382+
Inputs: resource.NewPropertyMapFromMap(map[string]any{
383+
"version": "3.2.1",
384+
}),
385+
})
386+
uuid, err := uuid.NewV4()
387+
require.NoError(t, err)
388+
providerState.ID = resource.ID(uuid.String())
389+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
390+
Metadata: makeMetadata(deploy.OpSame, providerState),
391+
})
392+
393+
providerRef, err := providers.NewReference(providerState.URN, providerState.ID)
394+
require.NoError(t, err)
395+
usesProvider := makeStateMetadata(t, "usesProvider", "pkg:mod:typ", true, stateOptions{
396+
Provider: &providerRef,
397+
})
398+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
399+
Metadata: makeMetadata(deploy.OpCreate, usesProvider),
400+
})
401+
402+
// This resource takes the provider's current generated name, forcing the provider alias to
403+
// be renamed again from "provPkg" to "provPkg2".
404+
conflictingState := makeStateMetadata(t, "provPkg", "other:index:Conflict", true, stateOptions{})
405+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
406+
Metadata: makeMetadata(deploy.OpCreate, conflictingState),
407+
})
408+
409+
close(events)
410+
importFile, err := importFilePromise.Result(t.Context())
411+
require.NoError(t, err)
412+
413+
require.Len(t, importFile.NameTable, 1)
414+
assert.Equal(t, providerState.URN, importFile.NameTable["provPkg2"])
415+
416+
require.Len(t, importFile.Resources, 2)
417+
assert.Equal(t, importSpec{
418+
ID: "<PLACEHOLDER>",
419+
Type: "pkg:mod:typ",
420+
Name: "usesProvider",
421+
Provider: "provPkg2",
422+
Version: "3.2.1",
423+
}, importFile.Resources[0])
424+
assert.Equal(t, importSpec{
425+
ID: "<PLACEHOLDER>",
426+
Type: "other:index:Conflict",
427+
Name: "provPkg",
428+
}, importFile.Resources[1])
429+
}
430+
362431
// TestBuildImportFile_NewProvider tests that we can generate an import file for a resource that uses
363432
// an explicit provider that is also being created in the same deployment (#15453).
364433
func TestBuildImportFile_NewProvider(t *testing.T) {
@@ -613,3 +682,65 @@ func TestBuildImportFile_regress_15068(t *testing.T) {
613682
_, err := importFilePromise.Result(t.Context())
614683
assert.ErrorContains(t, err, "could not parse provider reference")
615684
}
685+
686+
// Regression test for https://github.com/pulumi/pulumi/issues/24056
687+
// Tests that when multiple existing component resources share the same name,
688+
// generated import resources still reference the correct distinct parents.
689+
func TestBuildImportFile_regress_24056(t *testing.T) {
690+
t.Parallel()
691+
692+
events := make(chan engine.Event)
693+
importFilePromise := buildImportFile(t.Context(), events, sdkconfig.NopEncrypter)
694+
695+
// Pretend the root stack already exists.
696+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
697+
Metadata: makeRootStackMetadata(deploy.OpSame),
698+
})
699+
700+
// Two existing components with the same logical name.
701+
parentA := makeStateMetadata(t, "bug", "example:components:ComponentResourceA", false, stateOptions{})
702+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
703+
Metadata: makeMetadata(deploy.OpSame, parentA),
704+
})
705+
706+
parentB := makeStateMetadata(t, "bug", "example:components:ComponentResourceB", false, stateOptions{})
707+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
708+
Metadata: makeMetadata(deploy.OpSame, parentB),
709+
})
710+
711+
// Each created resource has one of the existing components as its parent.
712+
childA := makeStateMetadata(t, "child-a", "example:components:ChildA", false, stateOptions{Parent: parentA.URN})
713+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
714+
Metadata: makeMetadata(deploy.OpCreate, childA),
715+
})
716+
717+
childB := makeStateMetadata(t, "child-b", "example:components:ChildB", false, stateOptions{Parent: parentB.URN})
718+
events <- engine.NewEvent(engine.ResourcePreEventPayload{
719+
Metadata: makeMetadata(deploy.OpCreate, childB),
720+
})
721+
722+
close(events)
723+
724+
importFile, err := importFilePromise.Result(t.Context())
725+
require.NoError(t, err)
726+
require.Len(t, importFile.Resources, 2)
727+
728+
parentsByType := map[tokens.Type]string{}
729+
for _, spec := range importFile.Resources {
730+
parentsByType[spec.Type] = spec.Parent
731+
}
732+
733+
parentAliasA, ok := parentsByType[childA.Type]
734+
require.True(t, ok)
735+
require.NotEmpty(t, parentAliasA)
736+
parentAliasB, ok := parentsByType[childB.Type]
737+
require.True(t, ok)
738+
require.NotEmpty(t, parentAliasB)
739+
740+
// The two distinct parents must not collapse to the same alias.
741+
assert.NotEqual(t, parentAliasA, parentAliasB)
742+
743+
// And each alias must resolve to the correct parent URN in the name table.
744+
assert.Equal(t, parentA.URN, importFile.NameTable[parentAliasA])
745+
assert.Equal(t, parentB.URN, importFile.NameTable[parentAliasB])
746+
}

0 commit comments

Comments
 (0)