diff --git a/packages/aws-cdk/lib/init.ts b/packages/aws-cdk/lib/init.ts index f066b6c8fefb8..6166736f3e8da 100644 --- a/packages/aws-cdk/lib/init.ts +++ b/packages/aws-cdk/lib/init.ts @@ -196,7 +196,13 @@ interface ProjectInfo { function versionedTemplatesDir(): Promise { return new Promise(async resolve => { - const majorVersion = semver.major(versionNumber()); + let currentVersion = versionNumber(); + // If the CLI is invoked from source (i.e., developement), rather than from a packaged distribution, + // the version number will be '0.0.0'. We will (currently) default to the v1 templates in this case. + if (currentVersion === '0.0.0') { + currentVersion = '1.0.0'; + } + const majorVersion = semver.major(currentVersion); resolve(path.join(__dirname, 'init-templates', `v${majorVersion}`)); }); } diff --git a/packages/aws-cdk/test/init.test.ts b/packages/aws-cdk/test/init.test.ts index 4f2e8e11401f2..db042d76fc25d 100644 --- a/packages/aws-cdk/test/init.test.ts +++ b/packages/aws-cdk/test/init.test.ts @@ -93,6 +93,13 @@ describe.each(['1', '2'])('v%s tests', (majorVersion) => { }); }); +test('when no version number is present (e.g., local development), the v1 templates are chosen by default', async () => { + mockMajorVersion = '0.0.0'; + jest.resetAllMocks(); + + expect((await availableInitTemplates()).length).toBeGreaterThan(0); +}); + function cliTest(name: string, handler: (dir: string) => void | Promise): void { test(name, () => withTempDir(handler)); }