From d310bd3290c3a81e8285ceeb6eda9c9b5aa867d7 Mon Sep 17 00:00:00 2001 From: Luke Karrys Date: Thu, 21 Oct 2021 10:06:52 -0700 Subject: [PATCH] fix: always set originalLockfileVersion when doing shrinkwrap reset Fix: npm/cli#3899 A shrinkwrap reset without specifying a lockfile version was triggering an old lockfile warning due to the originalLockfileVersion not being set prior to checking whether the lockfile was old. --- lib/shrinkwrap.js | 5 +++-- test/arborist/build-ideal-tree.js | 27 +++++++++++++++++++++++++++ test/shrinkwrap.js | 1 + 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/shrinkwrap.js b/lib/shrinkwrap.js index 93e1cb1a4..00e5b8d34 100644 --- a/lib/shrinkwrap.js +++ b/lib/shrinkwrap.js @@ -380,9 +380,10 @@ class Shrinkwrap { reset () { this.tree = null this[_awaitingUpdate] = new Map() - this.originalLockfileVersion = this.lockfileVersion + const lockfileVersion = this.lockfileVersion || defaultLockfileVersion + this.originalLockfileVersion = lockfileVersion this.data = { - lockfileVersion: this.lockfileVersion || defaultLockfileVersion, + lockfileVersion, requires: true, packages: {}, dependencies: {}, diff --git a/test/arborist/build-ideal-tree.js b/test/arborist/build-ideal-tree.js index 1e3147405..8df0cc7a6 100644 --- a/test/arborist/build-ideal-tree.js +++ b/test/arborist/build-ideal-tree.js @@ -611,6 +611,33 @@ t.test('force a new mkdirp (but not semver major)', async t => { t.equal(arb.idealTree.children.get('minimist').package.version, '1.2.5') }) +t.test('empty update should not trigger old lockfile', async t => { + const path = t.testdir({ + 'package.json': JSON.stringify({ + name: 'empty-update', + version: '1.0.0', + }), + 'package-lock.json': JSON.stringify({ + name: 'empty-update', + version: '1.0.0', + lockfileVersion: 2, + requires: true, + packages: { + '': { + name: 'empty-update', + version: '1.0.0', + }, + }, + }), + }) + const checkLogs = warningTracker() + + const arb = newArb(path) + await arb.reify({ update: true }) + + t.strictSame(checkLogs(), []) +}) + t.test('no fix available', async t => { const path = resolve(fixtures, 'audit-mkdirp/mkdirp-unfixable') const checkLogs = warningTracker() diff --git a/test/shrinkwrap.js b/test/shrinkwrap.js index 97e0dc91e..86120bb03 100644 --- a/test/shrinkwrap.js +++ b/test/shrinkwrap.js @@ -75,6 +75,7 @@ t.test('starting out with a reset lockfile is an empty lockfile', t => dependencies: {}, packages: {}, }) + t.equal(sw.originalLockfileVersion, 2) t.equal(sw.loadedFromDisk, true) t.equal(sw.filename, resolve(fixture, 'package-lock.json')) }))