Skip to content

Commit 2b3ba3c

Browse files
authored
fix(gen2-migration): skip imported auth resources in refactor step (#14722)
fix(cli-internal): skip imported auth resources in refactor step Imported (reference) auth resources have no nested CloudFormation stack — they reference externally-managed Cognito resources. The refactor step was unconditionally creating an AuthCognitoForwardRefactorer for every auth:Cognito resource, which then failed with "unable to find source stack" because there is no stack to find. Now checks serviceType === 'imported' in amplify-meta.json before creating the refactorer, matching the existing pattern in the generate step. Added tests for both forward and rollback paths. --- Prompt: In the gen2-migration tool, I am getting this error when trying to refactor an app with an imported auth resource - "[AuthCognitoForwardRefactorer] unable to find source stack"
1 parent c86f4e5 commit 2b3ba3c

2 files changed

Lines changed: 63 additions & 21 deletions

File tree

packages/amplify-cli/src/__tests__/commands/gen2-migration/refactor/refactor.test.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ function findGen2RootStackName(app: MigrationApp) {
128128
throw new Error(`Unable to find Gen2 root stack name for app: ${app.name}`);
129129
}
130130

131-
function mockDiscover(resources: DiscoveredResource[]): jest.SpyInstance {
131+
function mockDiscover(resources: DiscoveredResource[], metaOverrides?: Record<string, unknown>): jest.SpyInstance {
132132
return jest.spyOn(Gen1App, 'create').mockResolvedValue({
133133
discover: () => resources,
134-
meta: () => undefined,
134+
meta: (category: string) => metaOverrides?.[category] as Record<string, unknown> | undefined,
135135
} as unknown as Gen1App);
136136
}
137137

@@ -232,6 +232,17 @@ describe('AmplifyMigrationRefactorStep', () => {
232232
const plan = await step.forward();
233233
await plan.describe();
234234
});
235+
236+
it('skips imported auth resources', async () => {
237+
infraSpy = mockCreateInfrastructure();
238+
createSpy = mockDiscover([{ category: 'auth', resourceName: 'myImportedPool', service: 'Cognito', key: 'auth:Cognito' }], {
239+
auth: { myImportedPool: { service: 'Cognito', serviceType: 'imported' } },
240+
});
241+
242+
const step = createStep();
243+
const plan = await step.forward();
244+
await plan.describe();
245+
});
235246
});
236247

237248
describe('rollback()', () => {
@@ -254,5 +265,16 @@ describe('AmplifyMigrationRefactorStep', () => {
254265
const plan = await step.rollback();
255266
await plan.describe();
256267
});
268+
269+
it('skips imported auth resources', async () => {
270+
infraSpy = mockCreateInfrastructure();
271+
createSpy = mockDiscover([{ category: 'auth', resourceName: 'myImportedPool', service: 'Cognito', key: 'auth:Cognito' }], {
272+
auth: { myImportedPool: { service: 'Cognito', serviceType: 'imported' } },
273+
});
274+
275+
const step = createStep();
276+
const plan = await step.rollback();
277+
await plan.describe();
278+
});
257279
});
258280
});

packages/amplify-cli/src/commands/gen2-migration/refactor/refactor.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,32 @@ export class AmplifyMigrationRefactorStep extends AmplifyMigrationStep {
6767

6868
for (const resource of discovered) {
6969
switch (resource.key) {
70-
case 'auth:Cognito':
71-
refactorers.push(
72-
new AuthCognitoForwardRefactorer(
73-
gen1Env,
74-
gen2Branch,
75-
clients,
76-
this.region,
77-
accountId,
78-
this.logger,
79-
this.appId,
80-
this.currentEnvName,
81-
resource,
82-
cfn,
83-
),
84-
);
70+
case 'auth:Cognito': {
71+
// Imported auth resources have no CloudFormation stack to move — skip.
72+
const isReferenceAuth = discovered
73+
.filter((r) => r.category === 'auth')
74+
.some((r) => {
75+
const meta = (gen1App.meta('auth') ?? {})[r.resourceName] as Record<string, unknown> | undefined;
76+
return meta?.serviceType === 'imported';
77+
});
78+
if (!isReferenceAuth) {
79+
refactorers.push(
80+
new AuthCognitoForwardRefactorer(
81+
gen1Env,
82+
gen2Branch,
83+
clients,
84+
this.region,
85+
accountId,
86+
this.logger,
87+
this.appId,
88+
this.currentEnvName,
89+
resource,
90+
cfn,
91+
),
92+
);
93+
}
8594
break;
95+
}
8696
case 'auth:Cognito-UserPool-Groups':
8797
refactorers.push(
8898
new AuthUserPoolGroupsForwardRefactorer(gen1Env, gen2Branch, clients, this.region, accountId, this.logger, resource, cfn),
@@ -143,11 +153,21 @@ export class AmplifyMigrationRefactorStep extends AmplifyMigrationStep {
143153

144154
for (const resource of discovered) {
145155
switch (resource.key) {
146-
case 'auth:Cognito':
147-
refactorers.push(
148-
new AuthCognitoRollbackRefactorer(gen1Env, gen2Branch, clients, this.region, accountId, this.logger, resource, cfn),
149-
);
156+
case 'auth:Cognito': {
157+
// Imported auth resources have no CloudFormation stack to move — skip.
158+
const isReferenceAuth = discovered
159+
.filter((r) => r.category === 'auth')
160+
.some((r) => {
161+
const meta = (gen1App.meta('auth') ?? {})[r.resourceName] as Record<string, unknown> | undefined;
162+
return meta?.serviceType === 'imported';
163+
});
164+
if (!isReferenceAuth) {
165+
refactorers.push(
166+
new AuthCognitoRollbackRefactorer(gen1Env, gen2Branch, clients, this.region, accountId, this.logger, resource, cfn),
167+
);
168+
}
150169
break;
170+
}
151171
case 'auth:Cognito-UserPool-Groups':
152172
refactorers.push(
153173
new AuthUserPoolGroupsRollbackRefactorer(gen1Env, gen2Branch, clients, this.region, accountId, this.logger, resource, cfn),

0 commit comments

Comments
 (0)