Skip to content

Commit 72a7a59

Browse files
wraithgarfritzy
authored andcommitted
fix: only save package-lock when truly finished
When we update the trees, the edges are refreshed to correct their specs. If we have written the package-lock before this point, it will be incorrect. No new tests are needed as existing tests were snapshotted w/ the broken behavior, and are fixed now showing this bugfix in action.
1 parent 5ec35b7 commit 72a7a59

File tree

2 files changed

+40
-52
lines changed

2 files changed

+40
-52
lines changed

workspaces/arborist/lib/arborist/reify.js

+37-49
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ const _rollbackRetireShallowNodes = Symbol.for('rollbackRetireShallowNodes')
8282
const _rollbackCreateSparseTree = Symbol.for('rollbackCreateSparseTree')
8383
const _rollbackMoveBackRetiredUnchanged = Symbol.for('rollbackMoveBackRetiredUnchanged')
8484
const _saveIdealTree = Symbol.for('saveIdealTree')
85-
const _saveLockFile = Symbol('saveLockFile')
8685
const _copyIdealToActual = Symbol('copyIdealToActual')
8786
const _addOmitsToTrashList = Symbol('addOmitsToTrashList')
8887
const _packageLockOnly = Symbol('packageLockOnly')
@@ -1404,64 +1403,53 @@ module.exports = cls => class Reifier extends cls {
14041403
}
14051404
}
14061405

1407-
// preserve indentation, if possible
1408-
const {
1409-
[Symbol.for('indent')]: indent,
1410-
} = this.idealTree.package
1411-
const format = indent === undefined ? ' ' : indent
1412-
1413-
const saveOpt = {
1414-
format: (this[_formatPackageLock] && format) ? format
1415-
: this[_formatPackageLock],
1416-
}
1417-
1418-
const promises = [this[_saveLockFile](saveOpt)]
1419-
1420-
const updatePackageJson = async (tree) => {
1421-
const pkgJson = await PackageJson.load(tree.path)
1422-
.catch(() => new PackageJson(tree.path))
1423-
const {
1424-
dependencies = {},
1425-
devDependencies = {},
1426-
optionalDependencies = {},
1427-
peerDependencies = {},
1428-
// bundleDependencies is not required by PackageJson like the other fields here
1429-
// PackageJson also doesn't omit an empty array for this field so defaulting this
1430-
// to an empty array would add that field to every package.json file.
1431-
bundleDependencies,
1432-
} = tree.package
1433-
1434-
pkgJson.update({
1435-
dependencies,
1436-
devDependencies,
1437-
optionalDependencies,
1438-
peerDependencies,
1439-
bundleDependencies,
1440-
})
1441-
await pkgJson.save()
1442-
}
1443-
14441406
if (save) {
14451407
for (const tree of updatedTrees) {
14461408
// refresh the edges so they have the correct specs
14471409
tree.package = tree.package
1448-
promises.push(updatePackageJson(tree))
1410+
const pkgJson = await PackageJson.load(tree.path)
1411+
.catch(() => new PackageJson(tree.path))
1412+
const {
1413+
dependencies = {},
1414+
devDependencies = {},
1415+
optionalDependencies = {},
1416+
peerDependencies = {},
1417+
// bundleDependencies is not required by PackageJson like the other
1418+
// fields here PackageJson also doesn't omit an empty array for this
1419+
// field so defaulting this to an empty array would add that field to
1420+
// every package.json file.
1421+
bundleDependencies,
1422+
} = tree.package
1423+
1424+
pkgJson.update({
1425+
dependencies,
1426+
devDependencies,
1427+
optionalDependencies,
1428+
peerDependencies,
1429+
bundleDependencies,
1430+
})
1431+
await pkgJson.save()
14491432
}
14501433
}
14511434

1452-
await Promise.all(promises)
1453-
process.emit('timeEnd', 'reify:save')
1454-
return true
1455-
}
1435+
// before now edge specs could be changing, affecting the `requires` field
1436+
// in the package lock, so we hold off saving to the very last action
1437+
if (this[_usePackageLock]) {
1438+
// preserve indentation, if possible
1439+
let format = this.idealTree.package[Symbol.for('indent')]
1440+
if (format === undefined) {
1441+
format = ' '
1442+
}
14561443

1457-
async [_saveLockFile] (saveOpt) {
1458-
if (!this[_usePackageLock]) {
1459-
return
1444+
// TODO this ignores options.save
1445+
await this.idealTree.meta.save({
1446+
format: (this[_formatPackageLock] && format) ? format
1447+
: this[_formatPackageLock],
1448+
})
14601449
}
14611450

1462-
const { meta } = this.idealTree
1463-
1464-
return meta.save(saveOpt)
1451+
process.emit('timeEnd', 'reify:save')
1452+
return true
14651453
}
14661454

14671455
async [_copyIdealToActual] () {

workspaces/arborist/tap-snapshots/test/arborist/reify.js.test.cjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -32675,7 +32675,7 @@ exports[`test/arborist/reify.js TAP save package.json on update should update na
3267532675
"a": {
3267632676
"version": "file:a",
3267732677
"requires": {
32678-
"abbrev": "^1.0.4",
32678+
"abbrev": "^1.1.1",
3267932679
"once": "^1.3.2"
3268032680
}
3268132681
},
@@ -32687,7 +32687,7 @@ exports[`test/arborist/reify.js TAP save package.json on update should update na
3268732687
"b": {
3268832688
"version": "file:b",
3268932689
"requires": {
32690-
"abbrev": "^1.0.4"
32690+
"abbrev": "^1.1.1"
3269132691
}
3269232692
},
3269332693
"once": {
@@ -32766,7 +32766,7 @@ exports[`test/arborist/reify.js TAP save package.json on update should update si
3276632766
"version": "file:a",
3276732767
"requires": {
3276832768
"abbrev": "^1.0.4",
32769-
"once": "^1.3.2"
32769+
"once": "^1.4.0"
3277032770
}
3277132771
},
3277232772
"abbrev": {

0 commit comments

Comments
 (0)