Skip to content

Commit a9d5b84

Browse files
fix(cli): use caret version for aws-cdk-lib in init templates (#1002)
Fixes #946 - more or less. The `cdk init` command generates `package.json` files with a pinned `aws-cdk-lib` version (e.g., `"2.232.2"`), which prevents users from updating the library using `npm update`. This change adds a caret prefix (`^`) to the version, allowing semver-compatible updates while still ensuring compatibility. Additionally, this PR separates the `aws-cdk-lib` upgrade from the regular dependency upgrade workflow. This allows us to track `aws-cdk-lib` updates independently and ensures the init templates always reference a recent version. There's no guarantee that these updates are merged more often (they still need to pass integ tests), but being decoupled from other updates they are much less likely to fail. ## Why this is safe The main question is if we are still testing what we are releasing. Specifically adding the `^` caret has to consequences: - pre release versions are only matched exactly (the `^`) is ignored, we use pre-release versions in our integ tests - when a new version of `aws-cdk-lib` is released, can we ensure we have tested this combination of Toolkit and Construct Lib ### Tests in this repo We are already running all our `cdk init` tests against the latest version of `aws-cdk-lib`. This is done by finding the latest version: https://github.com/aws/aws-cdk-cli/blob/af211ad102643f752caad307b6ec1e74ba93680e/.github/workflows/integ.yml#L107 which then ends up being explicitly requested as part of `cdk init`: https://github.com/aws/aws-cdk-cli/blob/af211ad102643f752caad307b6ec1e74ba93680e/packages/@aws-cdk-testing/cli-integ/tests/init-fsharp/init-fsharp.integtest.ts#L10 There is therefore no change in testing coverage. We do however default `cdk init` to a fixed, "known good" version. This version is not actively tested, but can be assumed "good" because it would have been the latest version at the time of the upgrade PR (and therefore tested). **This assumption is actually incorrect.** Upgrade PRs are created/updated weekly, but need to be manually approved to run integ tests. In the time the PR was created and tests run, a new version could have been released. Thus we could be merging a `aws-cdk-lib` version (and consequently a `cdk init` default) that is not actually tested. It is however reasonable to declare that if a later versions works, previous versions will work as well. This leaves us with `aws-cdk-lib` releasing new versions. ### Tests in `aws/aws-cdk` As part of our release pipeline we are explicitly testing the release candidate with the full proposed version, e.g. `2.123.0`. This version is staged in a private articat repository instead of npmjs.com. Otherwise the `cdk init` tests behave exactly the same as in this repo (duh, it's the same code): The version is explicitly requested as part of the `init` command. **This means that no version of `aws-cdk-lib` is released without passing init template tests.** In turn this means that it's safe to include the `^` because any future version will only be released if it passes. ### Continuous checks ("canaries") Similar to the other tests, our continuous canary checks will explicitly install the latest available version. So in addition to the previous assertions, we also have ongoing checks that the latest version of `aws-cdk-lib` works. --- By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license --------- Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent af211ad commit a9d5b84

File tree

15 files changed

+162
-11
lines changed

15 files changed

+162
-11
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.github/workflows/upgrade-aws-cdk-lib.yml

Lines changed: 89 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.gitignore

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projen/files.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.projenrc.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,7 @@ const cli = configureProject(
11751175
'yaml@^1',
11761176
'yargs@^15',
11771177
],
1178+
excludeDepsFromUpgrade: ['aws-cdk-lib'], // this is handled separately further down
11781179
tsconfig: {
11791180
compilerOptions: {
11801181
...defaultTsOptions,
@@ -1234,6 +1235,20 @@ const cli = configureProject(
12341235
}),
12351236
);
12361237

1238+
// @ts-ignore
1239+
cli.github = repo.github;
1240+
new pj.javascript.UpgradeDependencies(cli, {
1241+
include: ['aws-cdk-lib'],
1242+
semanticCommit: 'feat',
1243+
pullRequestTitle: 'upgrade aws-cdk-lib',
1244+
target: 'minor',
1245+
taskName: 'upgrade-aws-cdk-lib',
1246+
workflow: true,
1247+
workflowOptions: {
1248+
schedule: pj.javascript.UpgradeDependenciesSchedule.WEEKDAY,
1249+
},
1250+
});
1251+
12371252
new TypecheckTests(cli);
12381253

12391254
// Eslint rules

packages/@aws-cdk/toolkit-lib/lib/util/version-range.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,10 @@ export function rangeFromSemver(ver: string, targetType: RangeType) {
3636
}
3737
}
3838
}
39+
40+
/**
41+
* Strips a leading caret ^, if present
42+
*/
43+
export function stripCaret(ver: string): string {
44+
return ver.startsWith('^') ? ver.slice(1) : ver;
45+
}

packages/aws-cdk/.projen/tasks.json

Lines changed: 29 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/aws-cdk/generate.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ HERE
1818
# Copy the current 'aws-cdk-lib' version out from the monorepo.
1919
cdk_version=$(node -p 'require("aws-cdk-lib/package.json").version')
2020
constructs_range=$(node -p 'require("aws-cdk-lib/package.json").peerDependencies.constructs')
21-
echo '{"aws-cdk-lib": "'"$cdk_version"'", "constructs": "'"$constructs_range"'"}' > lib/init-templates/.init-version.json
21+
# Use caret range for aws-cdk-lib to allow minor and patch updates being installed on cdk init
22+
echo '{"aws-cdk-lib": "^'"$cdk_version"'", "constructs": "'"$constructs_range"'"}' > lib/init-templates/.init-version.json
2223

2324
# Copy the recommended-feature-flags.json file out from aws-cdk-lib.
2425
path=$(node -p 'require.resolve("aws-cdk-lib/recommended-feature-flags.json")')
25-
cp $path lib/init-templates/.recommended-feature-flags.json
26+
cp $path lib/init-templates/.recommended-feature-flags.json

packages/aws-cdk/lib/commands/init/init.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { invokeBuiltinHooks } from './init-hooks';
77
import type { IoHelper } from '../../api-private';
88
import { cliRootDir } from '../../cli/root-dir';
99
import { versionNumber } from '../../cli/version';
10-
import { cdkHomeDir, formatErrorMessage, rangeFromSemver } from '../../util';
10+
import { cdkHomeDir, formatErrorMessage, rangeFromSemver, stripCaret } from '../../util';
1111
import type { LanguageInfo } from '../language';
1212
import { getLanguageAlias, getLanguageExtensions, SUPPORTED_LANGUAGES } from '../language';
1313
import { getPmCmdPrefix, type JsPackageManager } from './package-manager';
@@ -568,19 +568,25 @@ export class InitTemplate {
568568
}
569569

570570
export function expandPlaceholders(template: string, language: string, project: ProjectInfo, packageManager?: JsPackageManager) {
571-
const cdkVersion = project.versions['aws-cdk-lib'];
572571
const cdkCliVersion = project.versions['aws-cdk'];
572+
let cdkVersion = project.versions['aws-cdk-lib'];
573573
let constructsVersion = project.versions.constructs;
574574

575575
switch (language) {
576576
case 'java':
577577
case 'csharp':
578578
case 'fsharp':
579+
cdkVersion = rangeFromSemver(cdkVersion, 'bracket');
579580
constructsVersion = rangeFromSemver(constructsVersion, 'bracket');
580581
break;
581582
case 'python':
583+
cdkVersion = rangeFromSemver(cdkVersion, 'pep');
582584
constructsVersion = rangeFromSemver(constructsVersion, 'pep');
583585
break;
586+
case 'go':
587+
cdkVersion = stripCaret(cdkVersion);
588+
constructsVersion = stripCaret(constructsVersion);
589+
break;
584590
}
585591
return template
586592
.replace(/%name%/g, project.name)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"aws-cdk-lib": "2.232.2", "constructs": "^10.0.0"}
1+
{"aws-cdk-lib": "^2.232.2", "constructs": "^10.0.0"}

0 commit comments

Comments
 (0)