From 1162746d35f2e9bba0036ba9974c1e4e3b3701ed Mon Sep 17 00:00:00 2001 From: Rico Hermans Date: Mon, 2 Oct 2023 22:58:48 +0200 Subject: [PATCH] chore: align-version script drops caret from dependencies it rewrites (#27376) The `align-version` script is supposed to rewrite `0.0.0` -> `2.99.0`, and also to rewrite `^0.0.0` -> `^2.99.0`. It didn't do the latter, because of a variable shadowing mistake. That mistake happened to come out correctly often enough that we didn't notice it. Fix that bug. Also add a linting rule that says that all peerDependencies should have a `^` dependency on `aws-cdk-lib`. ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license* --- .../package.json | 2 +- .../@aws-cdk/aws-appconfig-alpha/package.json | 2 +- .../custom-resource-handlers/package.json | 2 +- .../example-construct-library/package.json | 2 +- scripts/align-version.js | 8 +++---- tools/@aws-cdk/pkglint/lib/rules.ts | 22 +++++++++++++++++++ 6 files changed, 29 insertions(+), 9 deletions(-) diff --git a/packages/@aws-cdk/app-staging-synthesizer-alpha/package.json b/packages/@aws-cdk/app-staging-synthesizer-alpha/package.json index 23a49b181960e..8ea79ac838439 100644 --- a/packages/@aws-cdk/app-staging-synthesizer-alpha/package.json +++ b/packages/@aws-cdk/app-staging-synthesizer-alpha/package.json @@ -96,7 +96,7 @@ "@aws-cdk/pkglint": "0.0.0" }, "peerDependencies": { - "aws-cdk-lib": "0.0.0", + "aws-cdk-lib": "^0.0.0", "constructs": "^10.0.0" }, "publishConfig": { diff --git a/packages/@aws-cdk/aws-appconfig-alpha/package.json b/packages/@aws-cdk/aws-appconfig-alpha/package.json index 97287307667f2..86f5da9eacfe3 100644 --- a/packages/@aws-cdk/aws-appconfig-alpha/package.json +++ b/packages/@aws-cdk/aws-appconfig-alpha/package.json @@ -93,7 +93,7 @@ }, "homepage": "https://github.com/aws/aws-cdk", "peerDependencies": { - "aws-cdk-lib": "0.0.0", + "aws-cdk-lib": "^0.0.0", "constructs": "^10.0.0" }, "separate-module": false, diff --git a/packages/@aws-cdk/custom-resource-handlers/package.json b/packages/@aws-cdk/custom-resource-handlers/package.json index 20da9d6746eba..2e2d2ec0e06f8 100644 --- a/packages/@aws-cdk/custom-resource-handlers/package.json +++ b/packages/@aws-cdk/custom-resource-handlers/package.json @@ -51,7 +51,7 @@ "repository": { "url": "https://github.com/aws/aws-cdk.git", "type": "git", - "directory": "packages/custom-resource-handlers" + "directory": "packages/@aws-cdk/custom-resource-handlers" }, "keywords": [ "aws", diff --git a/packages/@aws-cdk/example-construct-library/package.json b/packages/@aws-cdk/example-construct-library/package.json index 999d39fd924fb..93511022414de 100644 --- a/packages/@aws-cdk/example-construct-library/package.json +++ b/packages/@aws-cdk/example-construct-library/package.json @@ -86,7 +86,7 @@ }, "homepage": "https://github.com/aws/aws-cdk", "peerDependencies": { - "aws-cdk-lib": "0.0.0", + "aws-cdk-lib": "^0.0.0", "constructs": "^10.0.0" }, "separate-module": false, diff --git a/scripts/align-version.js b/scripts/align-version.js index e18d3f77a1db3..bc3e5932adc71 100755 --- a/scripts/align-version.js +++ b/scripts/align-version.js @@ -26,7 +26,6 @@ for (const file of files) { } const version = packageVersionMap[pkg.name] - console.log(version); pkg.version = version; processSection(pkg.dependencies || { }, file); @@ -41,13 +40,12 @@ for (const file of files) { function processSection(section, file) { for (const [ name, version ] of Object.entries(section)) { if (version === marker || version === '^' + marker) { - const version = packageVersionMap[name]; - - if (!version) { + const newVersion = packageVersionMap[name]; + if (!newVersion) { throw new Error(`No package found ${name} within repository, which has version 0.0.0`); } - section[name] = version.replace(marker, version); + section[name] = version.replace(marker, newVersion); } } } diff --git a/tools/@aws-cdk/pkglint/lib/rules.ts b/tools/@aws-cdk/pkglint/lib/rules.ts index a6da50be7a4ac..d480d9817eaad 100644 --- a/tools/@aws-cdk/pkglint/lib/rules.ts +++ b/tools/@aws-cdk/pkglint/lib/rules.ts @@ -1533,6 +1533,28 @@ export class ConstructsDependency extends ValidationRule { } } +/** + * Peer dependencies should be a range, not a point version, to maximize compatibility + */ +export class PeerDependencyRange extends ValidationRule { + public readonly name = 'peerdependency/range'; + + public validate(pkg: PackageJson) { + const packages = ['aws-cdk-lib']; + for (const [name, version] of Object.entries(pkg.peerDependencies)) { + if (packages.includes(name) && version.match(/^[0-9]/)) { + pkg.report({ + ruleName: this.name, + message: `peerDependency on" ${name}" should be a range, not a point version: "${version}"`, + fix: () => { + pkg.addPeerDependency(name, '^' + version); + }, + }); + } + } + } +} + /** * Do not announce new versions of AWS CDK modules in awscdk.io because it is very very spammy * and actually causes the @awscdkio twitter account to be blocked.