diff --git a/lib/util/package-rules.ts b/lib/util/package-rules.ts index 7a4974e9d6ceb40787d615926c77ed9d4731c1e9..5184d5f1f5e593343f760e1c0522870a2a45d4ab 100644 --- a/lib/util/package-rules.ts +++ b/lib/util/package-rules.ts @@ -194,15 +194,35 @@ function matchesRule(inputConfig: Config, packageRule: PackageRule): boolean { currentValue && version.isVersion(currentValue) ? currentValue // it's a version so we can match against it : lockedVersion || fromVersion; // need to match against this fromVersion, if available - if (compareVersion && version.isVersion(compareVersion)) { - const isMatch = version.matches(compareVersion, matchCurrentVersion); - // istanbul ignore if - if (!isMatch) { + if (compareVersion) { + // istanbul ignore next + if (version.isVersion(compareVersion)) { + const isMatch = version.matches(compareVersion, matchCurrentVersion); + // istanbul ignore if + if (!isMatch) { + return false; + } + positiveMatch = true; + } else { return false; } - positiveMatch = true; } else { - return false; + // Handle the case where the match version is exact and current value is a range + try { + const matchCurrentVersionStr = matchCurrentVersion.toString(); + if (version.isVersion(matchCurrentVersionStr)) { + const isMatch = version.matches(matchCurrentVersionStr, currentValue); + if (!isMatch) { + return false; + } + positiveMatch = true; + } else { + return false; + } + } catch (err) /* istanbul ignore next */ { + logger.warn({ err }, 'Error trying reverse compare'); + return false; + } } } return positiveMatch; diff --git a/test/util/package-rules.spec.ts b/test/util/package-rules.spec.ts index 793778c2d62a425625366d0e16d8d74f91855321..cbee06e98cf4916462da7a16e2eb95f666970e19 100644 --- a/test/util/package-rules.spec.ts +++ b/test/util/package-rules.spec.ts @@ -474,6 +474,33 @@ describe('applyPackageRules()', () => { }); expect(res1.x).toBeDefined(); }); + it('checks if matchCurrentVersion selector is a version and matches if currentValue is a range', () => { + const config: TestConfig = { + packageRules: [ + { + packageNames: ['test'], + matchCurrentVersion: '2.1.0', + x: 1, + }, + ], + }; + const res1 = applyPackageRules({ + ...config, + ...{ + depName: 'test', + currentValue: '^2.0.0', + }, + }); + expect(res1.x).toBeDefined(); + const res2 = applyPackageRules({ + ...config, + ...{ + depName: 'test', + currentValue: '~2.0.0', + }, + }); + expect(res2.x).toBeUndefined(); + }); it('checks if matchCurrentVersion selector works with static values', () => { const config: TestConfig = { packageRules: [