From 19a77f890197d5e109070314cfa6ed56e2d4c022 Mon Sep 17 00:00:00 2001
From: Rhys Arkins <rhys@arkins.net>
Date: Sat, 22 Sep 2018 18:17:49 +0200
Subject: [PATCH] fix(npm): force package-lock.json update when widening
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Removes dependency record from package-lock.json before running “npm install” to ensure the latest version is installed.

Closes #2480
---
 lib/config/definitions.js                     |  1 -
 lib/manager/npm/post-update/index.js          | 26 ++++++++++++++++++-
 .../__snapshots__/flatten.spec.js.snap        |  8 ++++++
 3 files changed, 33 insertions(+), 2 deletions(-)

diff --git a/lib/config/definitions.js b/lib/config/definitions.js
index e9e263a3da..a76b6fbec3 100644
--- a/lib/config/definitions.js
+++ b/lib/config/definitions.js
@@ -523,7 +523,6 @@ const options = [
   {
     name: 'rangeStrategy',
     description: 'Policy for how to modify/update existing ranges.',
-    stage: 'package',
     type: 'string',
     default: 'replace',
     allowedValues: ['auto', 'pin', 'bump', 'replace', 'widen'],
diff --git a/lib/manager/npm/post-update/index.js b/lib/manager/npm/post-update/index.js
index 90f96ae4ed..1517a26e19 100644
--- a/lib/manager/npm/post-update/index.js
+++ b/lib/manager/npm/post-update/index.js
@@ -191,7 +191,31 @@ async function writeExistingFiles(config, packageFiles) {
         await fs.remove(npmLockPath);
       } else {
         logger.debug(`Writing ${npmLock}`);
-        const existingNpmLock = await platform.getFile(npmLock);
+        let existingNpmLock = await platform.getFile(npmLock);
+        const widens = [];
+        for (const upgrade of config.upgrades) {
+          if (
+            upgrade.rangeStrategy === 'widen' &&
+            upgrade.npmLock === npmLock
+          ) {
+            widens.push(upgrade.depName);
+          }
+        }
+        if (widens.length) {
+          logger.info(`Removing ${widens} from ${npmLock} to force an update`);
+          try {
+            const npmLockParsed = JSON.parse(existingNpmLock);
+            widens.forEach(depName => {
+              delete npmLockParsed.dependencies[depName];
+            });
+            existingNpmLock = JSON.stringify(npmLockParsed, null, 2);
+          } catch (err) {
+            logger.warn(
+              { npmLock },
+              'Error massing package-lock.json for widen'
+            );
+          }
+        }
         await fs.outputFile(npmLockPath, existingNpmLock);
       }
     }
diff --git a/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap b/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
index ae58066b0c..cfde3adb65 100644
--- a/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
+++ b/test/workers/repository/updates/__snapshots__/flatten.spec.js.snap
@@ -62,6 +62,7 @@ Array [
     "prNotPendingHours": 25,
     "prTitle": null,
     "raiseDeprecationWarnings": true,
+    "rangeStrategy": "replace",
     "rebaseLabel": "rebase",
     "rebaseStalePrs": null,
     "recreateClosed": false,
@@ -149,6 +150,7 @@ Array [
     "prNotPendingHours": 25,
     "prTitle": null,
     "raiseDeprecationWarnings": true,
+    "rangeStrategy": "replace",
     "rebaseLabel": "rebase",
     "rebaseStalePrs": null,
     "recreateClosed": false,
@@ -233,6 +235,7 @@ Array [
     "prNotPendingHours": 25,
     "prTitle": null,
     "raiseDeprecationWarnings": true,
+    "rangeStrategy": "replace",
     "rebaseLabel": "rebase",
     "rebaseStalePrs": true,
     "recreateClosed": true,
@@ -323,6 +326,7 @@ Array [
     "prNotPendingHours": 25,
     "prTitle": null,
     "raiseDeprecationWarnings": true,
+    "rangeStrategy": "replace",
     "rebaseLabel": "rebase",
     "rebaseStalePrs": null,
     "recreateClosed": false,
@@ -407,6 +411,7 @@ Array [
     "prNotPendingHours": 25,
     "prTitle": null,
     "raiseDeprecationWarnings": true,
+    "rangeStrategy": "replace",
     "rebaseLabel": "rebase",
     "rebaseStalePrs": true,
     "recreateClosed": true,
@@ -497,6 +502,7 @@ Array [
     "prNotPendingHours": 25,
     "prTitle": null,
     "raiseDeprecationWarnings": true,
+    "rangeStrategy": "replace",
     "rebaseLabel": "rebase",
     "rebaseStalePrs": null,
     "recreateClosed": false,
@@ -584,6 +590,7 @@ Array [
     "prNotPendingHours": 25,
     "prTitle": null,
     "raiseDeprecationWarnings": true,
+    "rangeStrategy": "replace",
     "rebaseLabel": "rebase",
     "rebaseStalePrs": null,
     "recreateClosed": false,
@@ -671,6 +678,7 @@ Array [
     "prNotPendingHours": 25,
     "prTitle": null,
     "raiseDeprecationWarnings": true,
+    "rangeStrategy": "replace",
     "rebaseLabel": "rebase",
     "rebaseStalePrs": null,
     "recreateClosed": false,
-- 
GitLab