From 8492dfefd209dfcceb528bc30dea4be55eaa66c7 Mon Sep 17 00:00:00 2001 From: Tom Keller <1083460+kellertk@users.noreply.github.com> Date: Mon, 17 Apr 2023 16:44:11 -0700 Subject: [PATCH] chore(core): Revert #24443 add stage prefix to stack name shortening process (#25163) Reverts aws/aws-cdk#24443. Fixes aws/aws-cdk#25130. --- .../core/lib/private/unique-resource-name.ts | 13 +------------ packages/aws-cdk-lib/core/lib/stack.ts | 17 ++++++----------- packages/aws-cdk-lib/core/test/stage.test.ts | 11 ----------- 3 files changed, 7 insertions(+), 34 deletions(-) diff --git a/packages/aws-cdk-lib/core/lib/private/unique-resource-name.ts b/packages/aws-cdk-lib/core/lib/private/unique-resource-name.ts index b52eecdda0ab1..938d25eedb79c 100644 --- a/packages/aws-cdk-lib/core/lib/private/unique-resource-name.ts +++ b/packages/aws-cdk-lib/core/lib/private/unique-resource-name.ts @@ -25,13 +25,6 @@ interface MakeUniqueResourceNameOptions { * @default - none */ readonly allowedSpecialCharacters?: string; - - /** - * Prefix to be added into the stack name - * - * @default - none - */ - readonly prefix?: string; } /** @@ -56,7 +49,6 @@ const HASH_LEN = 8; export function makeUniqueResourceName(components: string[], options: MakeUniqueResourceNameOptions) { const maxLength = options.maxLength ?? 256; const separator = options.separator ?? ''; - const prefix = options.prefix ?? ''; components = components.filter(x => x !== HIDDEN_ID); if (components.length === 0) { @@ -67,7 +59,7 @@ export function makeUniqueResourceName(components: string[], options: MakeUnique // in order to support transparent migration of cloudformation templates to the CDK without the // need to rename all resources. if (components.length === 1) { - const topLevelResource = prefix + removeNonAllowedSpecialCharacters(components[0], separator, options.allowedSpecialCharacters); + const topLevelResource = removeNonAllowedSpecialCharacters(components[0], separator, options.allowedSpecialCharacters); if (topLevelResource.length <= maxLength) { return topLevelResource; @@ -76,9 +68,6 @@ export function makeUniqueResourceName(components: string[], options: MakeUnique // Calculate the hash from the full path, included unresolved tokens so the hash value is always unique const hash = pathHash(components); - if (prefix) { - components.unshift(prefix); - } const human = removeDupes(components) .filter(pathElement => pathElement !== HIDDEN_FROM_HUMAN_ID) .map(pathElement => removeNonAllowedSpecialCharacters(pathElement, separator, options.allowedSpecialCharacters)) diff --git a/packages/aws-cdk-lib/core/lib/stack.ts b/packages/aws-cdk-lib/core/lib/stack.ts index 8cdab70e5cf38..b6b153113e009 100644 --- a/packages/aws-cdk-lib/core/lib/stack.ts +++ b/packages/aws-cdk-lib/core/lib/stack.ts @@ -1433,7 +1433,7 @@ export class Stack extends Construct implements ITaggable { private generateStackName() { const assembly = Stage.of(this); const prefix = (assembly && assembly.stageName) ? `${assembly.stageName}-` : ''; - return `${this.generateStackId(assembly, prefix)}`; + return `${prefix}${this.generateStackId(assembly)}`; } /** @@ -1448,7 +1448,7 @@ export class Stack extends Construct implements ITaggable { /** * Generate an ID with respect to the given container construct. */ - private generateStackId(container: IConstruct | undefined, prefix: string='') { + private generateStackId(container: IConstruct | undefined) { const rootPath = rootPathTo(this, container); const ids = rootPath.map(c => Node.of(c).id); @@ -1458,7 +1458,7 @@ export class Stack extends Construct implements ITaggable { throw new Error('unexpected: stack id must always be defined'); } - return makeStackName(ids, prefix); + return makeStackName(ids); } private resolveExportedValue(exportedValue: any): ResolvedExport { @@ -1644,14 +1644,9 @@ export function rootPathTo(construct: IConstruct, ancestor?: IConstruct): IConst * has only one component. Otherwise we fall back to the regular "makeUniqueId" * behavior. */ -function makeStackName(components: string[], prefix: string='') { - if (components.length === 1) { - const stack_name = prefix + components[0]; - if (stack_name.length <= 128) { - return stack_name; - } - } - return makeUniqueResourceName(components, { maxLength: 128, prefix: prefix }); +function makeStackName(components: string[]) { + if (components.length === 1) { return components[0]; } + return makeUniqueResourceName(components, { maxLength: 128 }); } function getCreateExportsScope(stack: Stack) { diff --git a/packages/aws-cdk-lib/core/test/stage.test.ts b/packages/aws-cdk-lib/core/test/stage.test.ts index afb87805d70f5..80b471140a6d3 100644 --- a/packages/aws-cdk-lib/core/test/stage.test.ts +++ b/packages/aws-cdk-lib/core/test/stage.test.ts @@ -103,17 +103,6 @@ describe('stage', () => { expect(stack.stackName).toEqual('MyStage-MyStack'); }); - test('generated stack names will not exceed 128 characters', () => { - // WHEN - const app = new App(); - const stage = new Stage(app, 'ThisStageNameIsVeryLongButWillOnlyBeTooLongWhenCombinedWithTheStackName'); - const stack = new BogusStack(stage, 'ThisStackNameIsVeryLongButItWillOnlyBeTooLongWhenCombinedWithTheLongPrefix'); - - // THEN - expect(stack.stackName.length).toEqual(128); - expect(stack.stackName).toEqual('ThisStageNameIsVeryLongButWillOnlyBeTooLongWhenCombinedWithTsVeryLongButItWillOnlyBeTooLongWhenCombinedWithTheLongPrefix4CA9F65B'); - }); - test('Can not have dependencies to stacks outside the nested asm', () => { // GIVEN const app = new App();