Skip to content

fix(deploy): pass stack selection to diff for --target filtering#1148

Draft
Hweinstock wants to merge 1 commit intomainfrom
fix/980-diff-target-filter
Draft

fix(deploy): pass stack selection to diff for --target filtering#1148
Hweinstock wants to merge 1 commit intomainfrom
fix/980-diff-target-filter

Conversation

@Hweinstock
Copy link
Copy Markdown
Contributor

@Hweinstock Hweinstock commented May 6, 2026

Description

When --diff or --deploy is used with --target, the CLI now passes a stack selection pattern (*${targetName}*) to the CDK toolkit so only the selected target's stack is operated on, not all stacks in the synthesized app.

Extracted runDiff and runDeploy functions for testability via dependency injection.

Related Issue

Closes #980

Type of Change

  • Bug fix

Testing

  • I ran npm run test:unit and npm run test:integ
  • I ran npm run typecheck
  • I ran npm run lint

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • My changes generate no new warnings

@github-actions github-actions Bot added size/s PR size: S agentcore-harness-reviewing AgentCore Harness review in progress labels May 6, 2026
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 6, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 43.19% 9034 / 20914
🔵 Statements 42.47% 9592 / 22584
🔵 Functions 40.02% 1558 / 3893
🔵 Branches 40% 5811 / 14527
Generated in workflow #2573 for commit 3b8979f by the Vitest Coverage Report Action

@agentcore-cli-automation
Copy link
Copy Markdown

The fix passes stackName into runDiff, but stackName is defined at line 239 as:

const stackName = stackNames[0]!;

stackNames contains all stacks synthesized by the CDK app, and bin/cdk.ts creates one stack per entry in aws-targets.json (AgentCore-${project}-${target}). stackNames[0] is therefore just the first stack in the list (effectively arbitrary ordering), not the stack that corresponds to options.target.

Reproduction of the original bug #980:

  • Targets in aws-targets.json: preprod-east2, default
  • Run agentcore deploy --target default --diff
  • stackNames = ['AgentCore-MyProject-preprod-east2', 'AgentCore-MyProject-default'] (or whatever order CDK returns)
  • stackNames[0] = 'AgentCore-MyProject-preprod-east2'
  • Diff runs against the wrong stack — the exact issue this PR is trying to fix.

Even in the current test setup this passes because the test hardcodes 'MyProject-prod' as the stack name and only asserts that the wrapper receives whatever string was passed in. It doesn't verify that handleDeploy selects the correct stack for options.target.

Suggested fixes (pick one):

  1. Construct the expected stack name from spec.name + target.name using the same sanitize logic as src/assets/cdk/bin/cdk.ts (AgentCore-${sanitize(projectName)}-${sanitize(targetName)}) and find it in stackNames, erroring if it's missing.
  2. Filter stackNames to the one that ends with -${sanitize(target.name)} (less robust, but simple).
  3. Pass target.name (or both project + target) into runDiff and let it build the pattern itself.

It's probably also worth adding a test that exercises handleDeploy with multiple synthesized stacks and asserts the diff is scoped to the stack matching options.target — otherwise a future regression here won't be caught.

As a side note, the issue description also flags that toolkitWrapper.deploy() at line 334 has the same missing stack selection. That's out of scope for this PR, but worth mentioning since both are driven by the same underlying stackNames[0] assumption.

@github-actions github-actions Bot removed the agentcore-harness-reviewing AgentCore Harness review in progress label May 6, 2026
@Hweinstock Hweinstock force-pushed the fix/980-diff-target-filter branch from 5de3cdb to c345f3d Compare May 7, 2026 00:33
@github-actions github-actions Bot added size/s PR size: S and removed size/s PR size: S labels May 7, 2026
@Hweinstock Hweinstock force-pushed the fix/980-diff-target-filter branch from c345f3d to d618196 Compare May 7, 2026 00:40
@github-actions github-actions Bot added size/s PR size: S and removed size/s PR size: S labels May 7, 2026
@Hweinstock Hweinstock force-pushed the fix/980-diff-target-filter branch from d618196 to 315aa4c Compare May 7, 2026 00:42
@github-actions github-actions Bot added size/s PR size: S and removed size/s PR size: S labels May 7, 2026
@Hweinstock Hweinstock force-pushed the fix/980-diff-target-filter branch from 315aa4c to 5036a35 Compare May 7, 2026 01:04
@github-actions github-actions Bot added size/s PR size: S and removed size/s PR size: S labels May 7, 2026
@Hweinstock Hweinstock force-pushed the fix/980-diff-target-filter branch from 5036a35 to b9843c0 Compare May 7, 2026 01:06
@github-actions github-actions Bot added size/s PR size: S and removed size/s PR size: S labels May 7, 2026
@Hweinstock
Copy link
Copy Markdown
Contributor Author

Hweinstock commented May 7, 2026

Addressed by no longer relying on arbitrary first stack name behavior.

@Hweinstock Hweinstock force-pushed the fix/980-diff-target-filter branch from b9843c0 to 3b8979f Compare May 7, 2026 01:17
@github-actions github-actions Bot added size/s PR size: S and removed size/s PR size: S labels May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/s PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(deploy): --diff flag diffs all stacks instead of filtering to the specified --target

2 participants