From 7d4de49749af75419b68919d1b084c63dbeb9d08 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Tue, 12 Sep 2023 07:51:26 +0200
Subject: [PATCH] fix(versioning): strip v prefix when bumping semver ranges
 (#24357)

---
 lib/modules/versioning/npm/index.spec.ts |  3 +++
 lib/modules/versioning/npm/range.ts      | 18 +++++++++++-------
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/lib/modules/versioning/npm/index.spec.ts b/lib/modules/versioning/npm/index.spec.ts
index 2441b4d71c..bc8c723797 100644
--- a/lib/modules/versioning/npm/index.spec.ts
+++ b/lib/modules/versioning/npm/index.spec.ts
@@ -131,6 +131,7 @@ describe('modules/versioning/npm/index', () => {
     ${'^2.3.4'}             | ${'replace'}         | ${'2.3.4'}       | ${'2.3.5'}              | ${'^2.3.4'}
     ${'~2.3.4'}             | ${'replace'}         | ${'2.3.4'}       | ${'2.3.5'}              | ${'~2.3.0'}
     ${'^0.0.1'}             | ${'replace'}         | ${'0.0.1'}       | ${'0.0.2'}              | ${'^0.0.2'}
+    ${'^0.0.1'}             | ${'replace'}         | ${'v0.0.1'}      | ${'v0.0.2'}             | ${'^0.0.2'}
     ${'^1.0.1'}             | ${'replace'}         | ${'1.0.1'}       | ${'2.0.2'}              | ${'^2.0.0'}
     ${'^1.2.3'}             | ${'replace'}         | ${'1.2.3'}       | ${'1.2.3'}              | ${'^1.2.3'}
     ${'^1.2.3'}             | ${'replace'}         | ${'1.2.3'}       | ${'1.2.2'}              | ${'^1.2.2'}
@@ -143,6 +144,7 @@ describe('modules/versioning/npm/index', () => {
     ${'1.x >2.0.0'}         | ${'widen'}           | ${'1.0.0'}       | ${'2.1.0'}              | ${null}
     ${'^1.0.0'}             | ${'bump'}            | ${'1.0.0'}       | ${'2.0.0'}              | ${'^2.0.0'}
     ${'~1.0.0'}             | ${'bump'}            | ${'1.0.0'}       | ${'2.0.0'}              | ${'~2.0.0'}
+    ${'~1.0.0'}             | ${'bump'}            | ${'v1.0.0'}      | ${'v2.0.0'}             | ${'~2.0.0'}
     ${'>1.0.0'}             | ${'bump'}            | ${'1.0.0'}       | ${'2.1.0'}              | ${null}
     ${'^1.0.0-alpha'}       | ${'replace'}         | ${'1.0.0-alpha'} | ${'1.0.0-beta'}         | ${'^1.0.0-beta'}
     ${'~1.0.0'}             | ${'replace'}         | ${'1.0.0'}       | ${'1.1.0'}              | ${'~1.1.0'}
@@ -150,6 +152,7 @@ describe('modules/versioning/npm/index', () => {
     ${'<=1.0'}              | ${'replace'}         | ${'1.0.0'}       | ${'1.2.0'}              | ${'<=1.2'}
     ${'<=1'}                | ${'replace'}         | ${'1.0.0'}       | ${'2.0.0'}              | ${'<=2'}
     ${'<= 1'}               | ${'replace'}         | ${'1.0.0'}       | ${'2.0.0'}              | ${'<= 2'}
+    ${'>=18.17.0'}          | ${'bump'}            | ${'v18.17.0'}    | ${'v18.17.1'}           | ${'>=18.17.1'}
   `(
     'getNewValue("$currentValue", "$rangeStrategy", "$currentVersion", "$newVersion") === "$expected"',
     ({ currentValue, rangeStrategy, currentVersion, newVersion, expected }) => {
diff --git a/lib/modules/versioning/npm/range.ts b/lib/modules/versioning/npm/range.ts
index d87a4ad6e7..779b3308ca 100644
--- a/lib/modules/versioning/npm/range.ts
+++ b/lib/modules/versioning/npm/range.ts
@@ -57,6 +57,10 @@ function replaceCaretValue(oldValue: string, newValue: string): string {
   return needReplace ? resultTuple.join('.') : oldValue;
 }
 
+function stripV(value: string): string {
+  return value.replace(/^v/, '');
+}
+
 // TODO: #22198
 export function getNewValue({
   currentValue,
@@ -136,18 +140,18 @@ export function getNewValue({
         });
       }
       if (element.operator === '^') {
-        return `^${newVersion}`;
+        return `^${stripV(newVersion)}`;
       }
       if (element.operator === '~') {
-        return `~${newVersion}`;
+        return `~${stripV(newVersion)}`;
       }
       if (element.operator === '=') {
-        return `=${newVersion}`;
+        return `=${stripV(newVersion)}`;
       }
       if (element.operator === '>=') {
         return currentValue.includes('>= ')
-          ? `>= ${newVersion}`
-          : `>=${newVersion}`;
+          ? `>= ${stripV(newVersion)}`
+          : `>=${stripV(newVersion)}`;
       }
       if (element.operator.startsWith('<')) {
         return currentValue;
@@ -193,7 +197,7 @@ export function getNewValue({
     return `^${replaceCaretValue(currentVersion, newVersion)}`;
   }
   if (element.operator === '=') {
-    return `=${newVersion}`;
+    return `=${stripV(newVersion)}`;
   }
   if (element.operator === '~') {
     if (suffix.length) {
@@ -204,7 +208,7 @@ export function getNewValue({
   if (element.operator === '<=') {
     let res;
     if (!!element.patch || suffix.length) {
-      res = `<=${newVersion}`;
+      res = `<=${stripV(newVersion)}`;
     } else if (element.minor) {
       res = `<=${toVersionMajor}.${toVersionMinor}`;
     } else {
-- 
GitLab