Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/cli/tui/hooks/useCdkPreflight.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ConfigIO, SecureCredentials } from '../../../lib';
import { AwsCredentialsError } from '../../../lib/errors/types';
import type { DeployedState } from '../../../schema';
import type { AwsDeploymentTarget, DeployedState } from '../../../schema';
import { applyTargetRegionToEnv } from '../../aws';
import { validateAwsCredentials } from '../../aws/account';
import { type CdkToolkitWrapper, type SwitchableIoHost, createSwitchableIoHost } from '../../cdk/toolkit-lib';
Expand Down Expand Up @@ -52,6 +52,14 @@ export interface PreflightOptions {
isInteractive?: boolean;
/** Skip identity provider check (for plan command which only synthesizes) */
skipIdentityCheck?: boolean;
/**
* Subset of targets the user picked in the TUI target selector. When provided,
* the preflight context's awsTargets is filtered to only these targets, so
* downstream deploy steps and the displayed Target header reflect what was
* actually selected. When omitted, all configured targets are used (CLI mode
* already filters via --target before reaching here).
*/
selectedTargets?: AwsDeploymentTarget[];
}

export interface PreflightResult {
Expand Down Expand Up @@ -113,7 +121,7 @@ const IDENTITY_STEP: Step = { label: LABEL_API_KEY, status: 'pending' };
const BOOTSTRAP_STEP: Step = { label: 'Bootstrap AWS environment', status: 'pending' };

export function useCdkPreflight(options: PreflightOptions): PreflightResult {
const { logger, isInteractive = false, skipIdentityCheck = false } = options;
const { logger, isInteractive = false, skipIdentityCheck = false, selectedTargets } = options;

// Create switchable ioHost - starts silent, can be flipped to verbose for deploy
const switchableIoHost = useMemo(() => createSwitchableIoHost(), []);
Expand Down Expand Up @@ -301,6 +309,17 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
let preflightContext: PreflightContext;
try {
preflightContext = await validateProject();
// Filter to only the targets the user picked in the TUI selector,
// if provided. Without this, the deploy header shows every target
// in aws-targets.json and downstream `awsTargets[0]` reads can
// resolve to a target the user did not select. See issue #1267.
if (selectedTargets && selectedTargets.length > 0) {
const selectedNames = new Set(selectedTargets.map(t => t.name));
preflightContext = {
...preflightContext,
awsTargets: preflightContext.awsTargets.filter(t => selectedNames.has(t.name)),
};
}
setContext(preflightContext);
// Make aws-targets.json region authoritative for downstream SDK / CDK
// toolkit-lib clients that bypass explicit region options. Restored on
Expand Down Expand Up @@ -530,7 +549,16 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
return () => {
process.off('unhandledRejection', handleUnhandledRejection);
};
}, [phase, logger, switchableIoHost, isInteractive, skipIdentityCheck, teardownConfirmed, restoreRegionEnv]);
}, [
phase,
logger,
switchableIoHost,
isInteractive,
skipIdentityCheck,
teardownConfirmed,
restoreRegionEnv,
selectedTargets,
]);

// Handle identity-setup phase (after user provides credentials)
useEffect(() => {
Expand Down
15 changes: 14 additions & 1 deletion src/cli/tui/screens/deploy/DeployScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,19 @@ export function DeployScreen({
// Load MCP spec for ResourceGraph
const configIO = useMemo(() => new ConfigIO(), []);

// Resolve which targets the user picked in the AWS target selector. When the
// project has a single target, the selector skips the picker phase and pre-
// selects index 0; with multiple targets the user explicitly selects one (or
// several) and we pass that filter down so the deploy actually targets the
// right environments. See issue #1267.
const selectedTargets = useMemo(
() =>
awsConfig.selectedTargetIndices
.map(i => awsConfig.availableTargets[i])
.filter((t): t is NonNullable<typeof t> => t !== undefined),
[awsConfig.selectedTargetIndices, awsConfig.availableTargets]
);

const {
phase,
steps,
Expand Down Expand Up @@ -97,7 +110,7 @@ export function DeployScreen({
useEnvLocalCredentials,
useManualCredentials,
skipCredentials,
} = useDeployFlow({ preSynthesized, isInteractive, diffMode });
} = useDeployFlow({ preSynthesized, isInteractive, diffMode, selectedTargets });
const allSuccess = !hasError && isComplete;
const skipPreflight = !!preSynthesized;

Expand Down
11 changes: 9 additions & 2 deletions src/cli/tui/screens/deploy/useDeployFlow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigIO } from '../../../../lib';
import type { AwsDeploymentTarget } from '../../../../schema';
import type { CdkToolkitWrapper, DeployMessage, SwitchableIoHost } from '../../../cdk/toolkit-lib';
import {
buildDeployedState,
Expand Down Expand Up @@ -65,6 +66,12 @@ interface DeployFlowOptions {
isInteractive?: boolean;
/** Run CDK diff instead of deploy */
diffMode?: boolean;
/**
* Subset of targets the user picked in the TUI target selector. When set,
* the preflight context's awsTargets is filtered to only these targets.
* See issue #1267.
*/
selectedTargets?: AwsDeploymentTarget[];
}

interface DeployFlowState {
Expand Down Expand Up @@ -118,14 +125,14 @@ interface DeployFlowState {
}

export function useDeployFlow(options: DeployFlowOptions = {}): DeployFlowState {
const { preSynthesized, isInteractive = false, diffMode = false } = options;
const { preSynthesized, isInteractive = false, diffMode = false, selectedTargets } = options;
const skipPreflight = !!preSynthesized;

// Create logger once for the entire deploy flow
const [logger] = useState(() => new ExecLogger({ command: 'deploy' }));

// Always call the hook (React rules), but we won't use it when preSynthesized is provided
const preflight = useCdkPreflight({ logger, isInteractive });
const preflight = useCdkPreflight({ logger, isInteractive, selectedTargets });

// Use pre-synthesized values when provided, otherwise use preflight values
const cdkToolkitWrapper = preSynthesized?.cdkToolkitWrapper ?? preflight.cdkToolkitWrapper;
Expand Down
Loading