Skip to content

Commit 0546ec2

Browse files
authored
chore(cli): remove unreachable v1 compat in stack selection (#32620)
### Reason for this change Removing dead code. The version number of the `CLI is never < 2` in release and thus this code cannot be reached. This allowed to remove the env variable hack that enabled test scenarios to use v2 behavior. ### Description of changes Remove the unreachable if statement and then optimize code. Also includes some minor reformatting of method parameters. ### Describe any new or updated permissions being added n/a ### Description of how you validated changes Test pipeline. ### Checklist - [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md) ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent c4136bd commit 0546ec2

File tree

3 files changed

+17
-29
lines changed

3 files changed

+17
-29
lines changed

packages/aws-cdk/lib/api/cxapp/cloud-assembly.ts

+17-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { minimatch } from 'minimatch';
44
import * as semver from 'semver';
55
import { error, print, warning } from '../../logging';
66
import { flatten } from '../../util';
7-
import { versionNumber } from '../../version';
87

98
export enum DefaultSelection {
109
/**
@@ -122,43 +121,35 @@ export class CloudAssembly {
122121
}
123122
}
124123

125-
private selectTopLevelStacks(stacks: cxapi.CloudFormationStackArtifact[],
124+
private selectTopLevelStacks(
125+
stacks: cxapi.CloudFormationStackArtifact[],
126126
topLevelStacks: cxapi.CloudFormationStackArtifact[],
127-
extend: ExtendedStackSelection = ExtendedStackSelection.None): StackCollection {
127+
extend: ExtendedStackSelection = ExtendedStackSelection.None,
128+
): StackCollection {
128129
if (topLevelStacks.length > 0) {
129130
return this.extendStacks(topLevelStacks, stacks, extend);
130131
} else {
131132
throw new Error('No stack found in the main cloud assembly. Use "list" to print manifest');
132133
}
133134
}
134135

135-
private selectMatchingStacks(stacks: cxapi.CloudFormationStackArtifact[],
136+
private selectMatchingStacks(
137+
stacks: cxapi.CloudFormationStackArtifact[],
136138
patterns: string[],
137-
extend: ExtendedStackSelection = ExtendedStackSelection.None): StackCollection {
138-
139-
// cli tests use this to ensure tests do not depend on legacy behavior
140-
// (otherwise they will fail in v2)
141-
const disableLegacy = process.env.CXAPI_DISABLE_SELECT_BY_ID === '1';
142-
143-
const matchingPattern = (pattern: string) => (stack: cxapi.CloudFormationStackArtifact) => {
144-
if (minimatch(stack.hierarchicalId, pattern)) {
145-
return true;
146-
} else if (!disableLegacy && stack.id === pattern && semver.major(versionNumber()) < 2) {
147-
warning('Selecting stack by identifier "%s". This identifier is deprecated and will be removed in v2. Please use "%s" instead.', chalk.bold(stack.id), chalk.bold(stack.hierarchicalId));
148-
warning('Run "cdk ls" to see a list of all stack identifiers');
149-
return true;
150-
}
151-
return false;
152-
};
139+
extend: ExtendedStackSelection = ExtendedStackSelection.None,
140+
): StackCollection {
153141

142+
const matchingPattern = (pattern: string) => (stack: cxapi.CloudFormationStackArtifact) => minimatch(stack.hierarchicalId, pattern);
154143
const matchedStacks = flatten(patterns.map(pattern => stacks.filter(matchingPattern(pattern))));
155144

156145
return this.extendStacks(matchedStacks, stacks, extend);
157146
}
158147

159-
private selectDefaultStacks(stacks: cxapi.CloudFormationStackArtifact[],
148+
private selectDefaultStacks(
149+
stacks: cxapi.CloudFormationStackArtifact[],
160150
topLevelStacks: cxapi.CloudFormationStackArtifact[],
161-
defaultSelection: DefaultSelection) {
151+
defaultSelection: DefaultSelection,
152+
) {
162153
switch (defaultSelection) {
163154
case DefaultSelection.MainAssembly:
164155
return new StackCollection(this, topLevelStacks);
@@ -178,9 +169,11 @@ export class CloudAssembly {
178169
}
179170
}
180171

181-
private extendStacks(matched: cxapi.CloudFormationStackArtifact[],
172+
private extendStacks(
173+
matched: cxapi.CloudFormationStackArtifact[],
182174
all: cxapi.CloudFormationStackArtifact[],
183-
extend: ExtendedStackSelection = ExtendedStackSelection.None) {
175+
extend: ExtendedStackSelection = ExtendedStackSelection.None,
176+
) {
184177
const allStacks = new Map<string, cxapi.CloudFormationStackArtifact>();
185178
for (const stack of all) {
186179
allStacks.set(stack.hierarchicalId, stack);

packages/aws-cdk/test/api/cloud-assembly.test.ts

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import { DefaultSelection } from '../../lib/api/cxapp/cloud-assembly';
44
import { MockCloudExecutable } from '../util';
55
import { cliAssemblyWithForcedVersion } from './assembly-versions';
66

7-
// behave like v2
8-
process.env.CXAPI_DISABLE_SELECT_BY_ID = '1';
9-
107
test('do not throw when selecting stack without errors', async () => {
118
// GIVEN
129
const cxasm = await testCloudAssembly();

packages/aws-cdk/test/cdk-toolkit.test.ts

-2
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,6 @@ import { flatten } from '../lib/util';
9595

9696
markTesting();
9797

98-
process.env.CXAPI_DISABLE_SELECT_BY_ID = '1';
99-
10098
const defaultBootstrapSource: BootstrapSource = { source: 'default' };
10199
const bootstrapEnvironmentMock = jest.spyOn(Bootstrapper.prototype, 'bootstrapEnvironment');
102100
let cloudExecutable: MockCloudExecutable;

0 commit comments

Comments
 (0)