Skip to content

Commit e5bfdac

Browse files
committed
fix: shrinkwrap setting incorrect lockfileVersion
Fix: #3962 When created from a hidden lockfile, shrinkwrap was always setting the lockfileVersion to 3. The shrinkwrap command also needed to be updated to log the correct message based on the lockfileVersion config instead of the static lockfileVersion. With these fixes, it was possible to log a better message whenever we are changing the lockfileVersion, either from a config or any existing lockfile. Lastly, the tests for shrinkwrap were completely refactored to use the real npm and test all conceivable scenarios, as well as removing usage of `util.promisify` in favor of `fs.promises` now that we've dropped support for Node 10. PR-URL: #3978 Credit: @lukekarrys Close: #3978 Reviewed-by: @wraithgar
1 parent 8ffeb71 commit e5bfdac

File tree

3 files changed

+613
-323
lines changed

3 files changed

+613
-323
lines changed

lib/commands/shrinkwrap.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
const { resolve, basename } = require('path')
2-
const util = require('util')
3-
const fs = require('fs')
4-
const { unlink } = fs.promises || { unlink: util.promisify(fs.unlink) }
2+
const { unlink } = require('fs').promises
53
const Arborist = require('@npmcli/arborist')
64
const log = require('npmlog')
75

@@ -21,7 +19,6 @@ class Shrinkwrap extends BaseCommand {
2119
// if has a npm-shrinkwrap.json, nothing to do
2220
// if has a package-lock.json, rename to npm-shrinkwrap.json
2321
// if has neither, load the actual tree and save that as npm-shrinkwrap.json
24-
// in all cases, re-cast to current lockfile version
2522
//
2623
// loadVirtual, fall back to loadActual
2724
// rename shrinkwrap file type, and tree.meta.save()
@@ -40,17 +37,37 @@ class Shrinkwrap extends BaseCommand {
4037
const oldFilename = meta.filename
4138
const notSW = !newFile && basename(oldFilename) !== 'npm-shrinkwrap.json'
4239

40+
// The computed lockfile version of a hidden lockfile is always 3
41+
// even if the actual value of the property is a different.
42+
// When shrinkwrap is run with only a hidden lockfile we want to
43+
// set the shrinkwrap lockfile version as whatever was explicitly
44+
// requested with a fallback to the actual value from the hidden
45+
// lockfile.
46+
if (meta.hiddenLockfile) {
47+
meta.lockfileVersion = arb.options.lockfileVersion ||
48+
meta.originalLockfileVersion
49+
}
4350
meta.hiddenLockfile = false
4451
meta.filename = sw
4552
await meta.save()
4653

47-
if (newFile)
48-
log.notice('', 'created a lockfile as npm-shrinkwrap.json')
49-
else if (notSW) {
54+
const updatedVersion = meta.originalLockfileVersion !== meta.lockfileVersion
55+
? meta.lockfileVersion
56+
: null
57+
58+
if (newFile) {
59+
let message = 'created a lockfile as npm-shrinkwrap.json'
60+
if (updatedVersion)
61+
message += ` with version ${updatedVersion}`
62+
log.notice('', message)
63+
} else if (notSW) {
5064
await unlink(oldFilename)
51-
log.notice('', 'package-lock.json has been renamed to npm-shrinkwrap.json')
52-
} else if (meta.originalLockfileVersion !== this.npm.lockfileVersion)
53-
log.notice('', `npm-shrinkwrap.json updated to version ${this.npm.lockfileVersion}`)
65+
let message = 'package-lock.json has been renamed to npm-shrinkwrap.json'
66+
if (updatedVersion)
67+
message += ` and updated to version ${updatedVersion}`
68+
log.notice('', message)
69+
} else if (updatedVersion)
70+
log.notice('', `npm-shrinkwrap.json updated to version ${updatedVersion}`)
5471
else
5572
log.notice('', 'npm-shrinkwrap.json up to date')
5673
}

0 commit comments

Comments
 (0)