Skip to content

Commit 315aa4c

Browse files
committed
fix(deploy): pass stack selection to diff for --target filtering (#980)
1 parent ec92d3f commit 315aa4c

2 files changed

Lines changed: 49 additions & 18 deletions

File tree

src/cli/commands/deploy/__tests__/deploy.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { runCLI } from '../../../../test-utils/index.js';
2+
import { runDiff } from '../actions.js';
3+
import { StackSelectionStrategy } from '@aws-cdk/toolkit-lib';
24
import { randomUUID } from 'node:crypto';
35
import { mkdir, rm, writeFile } from 'node:fs/promises';
46
import { tmpdir } from 'node:os';
@@ -56,3 +58,20 @@ describe('deploy without agents', () => {
5658
expect(json.error.toLowerCase()).toContain('no resources defined');
5759
});
5860
});
61+
62+
describe('runDiff', () => {
63+
it('passes stack selection pattern to toolkit wrapper diff', async () => {
64+
let captured: unknown;
65+
const fakeWrapper = {
66+
diff: (opts?: unknown) => {
67+
captured = opts;
68+
},
69+
};
70+
71+
await runDiff(fakeWrapper as any, 'prod');
72+
73+
expect(captured).toEqual({
74+
stacks: { strategy: StackSelectionStrategy.PATTERN_MUST_MATCH, patterns: ['*prod*'] },
75+
});
76+
});
77+
});

src/cli/commands/deploy/actions.ts

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import { ConfigIO, SecureCredentials } from '../../../lib';
22
import type { AgentCoreMcpSpec, DeployedState } from '../../../schema';
33
import { applyTargetRegionToEnv } from '../../aws';
44
import { validateAwsCredentials } from '../../aws/account';
5-
import { createSwitchableIoHost } from '../../cdk/toolkit-lib';
5+
import { CdkToolkitWrapper, createSwitchableIoHost } from '../../cdk/toolkit-lib';
6+
import type { SwitchableIoHost } from '../../cdk/toolkit-lib';
67
import {
78
buildDeployedState,
89
getStackOutputs,
@@ -41,6 +42,7 @@ import {
4142
import { setupHttpGateways } from '../../operations/deploy/post-deploy-http-gateways';
4243
import { enableOnlineEvalConfigs } from '../../operations/deploy/post-deploy-online-evals';
4344
import type { DeployResult } from './types';
45+
import { StackSelectionStrategy } from '@aws-cdk/toolkit-lib';
4446

4547
export interface ValidatedDeployOptions {
4648
target: string;
@@ -55,6 +57,32 @@ export interface ValidatedDeployOptions {
5557
const AGENT_NEXT_STEPS = ['agentcore invoke', 'agentcore status'];
5658
const MEMORY_ONLY_NEXT_STEPS = ['agentcore add agent', 'agentcore status'];
5759

60+
export async function runDiff(
61+
toolkitWrapper: CdkToolkitWrapper,
62+
targetName: string,
63+
switchableIoHost?: SwitchableIoHost
64+
): Promise<void> {
65+
const diffIoHost = switchableIoHost ?? createSwitchableIoHost();
66+
let hasDiffContent = false;
67+
diffIoHost.setOnRawMessage((code, _level, message) => {
68+
if (!message) return;
69+
// I4002: formatted diff per stack, I4001: overall diff summary
70+
if (code === 'CDK_TOOLKIT_I4002' || code === 'CDK_TOOLKIT_I4001') {
71+
hasDiffContent = true;
72+
console.log(message);
73+
}
74+
});
75+
diffIoHost.setVerbose(true);
76+
await toolkitWrapper.diff({
77+
stacks: { strategy: StackSelectionStrategy.PATTERN_MUST_MATCH, patterns: [`*${targetName}*`] },
78+
});
79+
if (!hasDiffContent) {
80+
console.log('No stack differences detected.');
81+
}
82+
diffIoHost.setVerbose(false);
83+
diffIoHost.setOnRawMessage(null);
84+
}
85+
5886
export async function handleDeploy(options: ValidatedDeployOptions): Promise<DeployResult> {
5987
let toolkitWrapper = null;
6088
let restoreEnv: (() => void) | null = null;
@@ -296,23 +324,7 @@ export async function handleDeploy(options: ValidatedDeployOptions): Promise<Dep
296324
// Diff mode: run cdk diff and exit without deploying
297325
if (options.diff) {
298326
startStep('Run CDK diff');
299-
const diffIoHost = switchableIoHost ?? createSwitchableIoHost();
300-
let hasDiffContent = false;
301-
diffIoHost.setOnRawMessage((code, _level, message) => {
302-
if (!message) return;
303-
// I4002: formatted diff per stack, I4001: overall diff summary
304-
if (code === 'CDK_TOOLKIT_I4002' || code === 'CDK_TOOLKIT_I4001') {
305-
hasDiffContent = true;
306-
console.log(message);
307-
}
308-
});
309-
diffIoHost.setVerbose(true);
310-
await toolkitWrapper.diff();
311-
if (!hasDiffContent) {
312-
console.log('No stack differences detected.');
313-
}
314-
diffIoHost.setVerbose(false);
315-
diffIoHost.setOnRawMessage(null);
327+
await runDiff(toolkitWrapper, target.name, switchableIoHost);
316328
endStep('success');
317329

318330
logger.finalize(true);

0 commit comments

Comments
 (0)