From 2027795f82e62a7b7565e2bbc7ecba66bb68c57e Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Wed, 9 Oct 2024 05:22:32 +0000 Subject: [PATCH 1/2] chore(constraints): allow exceptions for dependency-range consistency --- yarn.config.cjs | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) diff --git a/yarn.config.cjs b/yarn.config.cjs index a2e0428f09..64a11b3faf 100644 --- a/yarn.config.cjs +++ b/yarn.config.cjs @@ -714,6 +714,35 @@ function expectControllerDependenciesListedAsPeerDependencies( } } +/** + * Filter out dependency ranges which are not to be considered in `expectConsistentDependenciesAndDevDependencies`. + * + * @param {string} dependencyIdent - The dependency being filtered for + * @param {Map} dependenciesByRange - Dependencies by range + * @returns {Map} The resulting map. + */ +function getInconsistentDependenciesAndDevDependencies( + dependencyIdent, + dependenciesByRange, +) { + const ALLOWED_INCONSISTENT_DEPENDENCIES = Object.entries({ + // '@metamask/foo': ['^1.0.0'], + }); + for (const [ + allowedPackage, + ignoredRange, + ] of ALLOWED_INCONSISTENT_DEPENDENCIES) { + if (allowedPackage === dependencyIdent) { + return new Map( + Object.entries(dependenciesByRange).filter( + ([range]) => !ignoredRange.includes(range), + ), + ); + } + } + return dependenciesByRange; +} + /** * Expect that all version ranges in `dependencies` and `devDependencies` for * the same dependency across the entire monorepo are the same. As it is @@ -732,9 +761,14 @@ function expectConsistentDependenciesAndDevDependencies(Yarn) { dependencyIdent, dependenciesByRange, ] of nonPeerDependenciesByIdent.entries()) { - const dependencyRanges = [...dependenciesByRange.keys()].sort(); + const dependenciesToConsider = + getInconsistentDependenciesAndDevDependencies( + dependencyIdent, + dependenciesByRange, + ); + const dependencyRanges = [...dependenciesToConsider.keys()].sort(); if (dependenciesByRange.size > 1) { - for (const dependencies of dependenciesByRange.values()) { + for (const dependencies of dependenciesToConsider.values()) { for (const dependency of dependencies) { dependency.error( `Expected version range for ${dependencyIdent} (in ${ From f23be0cd1616536f150974260d0be66361f346f8 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Wed, 9 Oct 2024 05:28:59 +0000 Subject: [PATCH 2/2] chore(constraints): break loop early; simplify logic --- yarn.config.cjs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/yarn.config.cjs b/yarn.config.cjs index 64a11b3faf..1352ee2e0f 100644 --- a/yarn.config.cjs +++ b/yarn.config.cjs @@ -761,23 +761,24 @@ function expectConsistentDependenciesAndDevDependencies(Yarn) { dependencyIdent, dependenciesByRange, ] of nonPeerDependenciesByIdent.entries()) { + if (dependenciesByRange.size <= 1) { + continue; + } const dependenciesToConsider = getInconsistentDependenciesAndDevDependencies( dependencyIdent, dependenciesByRange, ); const dependencyRanges = [...dependenciesToConsider.keys()].sort(); - if (dependenciesByRange.size > 1) { - for (const dependencies of dependenciesToConsider.values()) { - for (const dependency of dependencies) { - dependency.error( - `Expected version range for ${dependencyIdent} (in ${ - dependency.type - }) to be consistent across monorepo. Pick one: ${inspect( - dependencyRanges, - )}`, - ); - } + for (const dependencies of dependenciesToConsider.values()) { + for (const dependency of dependencies) { + dependency.error( + `Expected version range for ${dependencyIdent} (in ${ + dependency.type + }) to be consistent across monorepo. Pick one: ${inspect( + dependencyRanges, + )}`, + ); } } }