Skip to content

fix(cli): migrate fails with a validation error when every matching resource is already managed by a stack - #2188

Open
yeegz wants to merge 1 commit into
aws:mainfrom
yeegz:fix/235-migrate-all-resources-managed
Open

yeegz wants to merge 1 commit into
aws:mainfrom
yeegz:fix/235-migrate-all-resources-managed

Conversation

@yeegz

@yeegz yeegz commented Nov 30, 2026

Copy link
Copy Markdown

Fixes #235

cdk migrate --from-scan with filters fails with Value '[]' at 'resources' failed to satisfy constraint: Member must have length greater than or equal to 1 when every resource that matches the filters already belongs to a CloudFormation stack (for example, the resources of an Amplify stack).

In packages/aws-cdk/lib/commands/migrate.ts, listResourceScanResources throws NoResourcesFound only when the scan returns nothing. excludeManaged runs after that check, so when every match is managed by a stack the list becomes empty. getResourceScanRelatedResources gets an empty input and returns an empty list, and that goes to CreateGeneratedTemplate, which rejects it.

This change checks the list again after excludeManaged and throws a ToolkitError (NoUnmanagedResourcesFound) that says why nothing is left, as proposed in the issue:

All 2 resources matching filters tag-key=user:Application,tag-value=my-app are already managed by a CloudFormation stack. Use --from-stack to migrate an existing stack.

Without filters it reads All N scanned resources are already managed .... The MIGRATE_INTEG_TEST path is unchanged.

Tests:

  • New unit test in test/commands/migrate.test.ts: the scan returns two resources that both have ManagedByStack: true. generateTemplate now rejects with the message above, and CreateGeneratedTemplate is never called. Before the fix the test fails because generateTemplate resolves after sending an empty resource list.
  • The shared ListResourceScanResources mock in the generateTemplate tests used to return a resource marked ManagedByStack: true. Those tests were going through this same empty-list path, which the mocked CreateGeneratedTemplate accepted. The mock now returns an unmanaged copy of that resource, so those tests run the normal path. Their expected output does not change.
  • npx jest test/commands/migrate.test.ts: 37 pass. The one failure is the C# generateCdkApp test, which needs dotnet and does not run on my machine (spawn dotnet ENOENT). It is not related to this change. eslint passes on both files.

Decision to flag: the message copies the style of the existing NoResourcesFound message in the same function (capital letter, full stop) and the wording proposed in the issue. The AGENTS.md style asks for lowercase with no full stop, so I can change it if you prefer.

I used an AI coding assistant to help write this change. I reviewed and tested it myself.

Checklist

  • This change contains a major version upgrade for a dependency and I confirm all breaking changes are addressed
    • Release notes for the new version:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

…esource is already managed by a stack

`listResourceScanResources` only threw `NoResourcesFound` when the scan
returned nothing. When every matching resource belongs to an existing
CloudFormation stack, `excludeManaged` removes all of them after that
check, and the empty list reaches `CreateGeneratedTemplate`, which fails
with "Value '[]' at 'resources' failed to satisfy constraint".

Check the list again after excluding managed resources and explain why
nothing is left, pointing at `--from-stack`.

Fixes aws#235

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 73.33333% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.54%. Comparing base (f7c9dca) to head (e711093).

Files with missing lines Patch % Lines
packages/aws-cdk/lib/commands/migrate.ts 73.33% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2188      +/-   ##
==========================================
- Coverage   92.55%   92.54%   -0.02%     
==========================================
  Files          81       81              
  Lines       13224    13236      +12     
  Branches     1917     1918       +1     
==========================================
+ Hits        12240    12249       +9     
- Misses        946      949       +3     
  Partials       38       38              
Flag Coverage Δ
suite.unit 92.54% <73.33%> (-0.02%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

This branch is waiting to be deployed

1 inactive and 1 waiting deployments
integ-approval — e7110931 Waiting Nov 30, 2026 by yeegz via prepare #7819
automation — e7110931 Deployed Nov 30, 2026 by yeegz via Set AutoQueue on PR #2188 #4062
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

(cli): migrate fails when retrieving records

3 participants