fix(lockfile): better lockfile stringify error

This commit is contained in:
Zoltan Kochan
2020-11-01 00:27:38 +02:00
parent 7ccdad1ad8
commit dbcc6c96fb
3 changed files with 16 additions and 12 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/lockfile-file": patch
---
Print a better error message when stringifying a lockfile object fails.

View File

@@ -63,14 +63,12 @@ function writeLockfile (
}
function yamlStringify (lockfile: Lockfile, forceSharedFormat: boolean) {
const normalizedLockfile = normalizeLockfile(lockfile, forceSharedFormat)
try {
return yaml.safeDump(
normalizeLockfile(lockfile, forceSharedFormat),
LOCKFILE_YAML_FORMAT
)
return yaml.safeDump(normalizedLockfile, LOCKFILE_YAML_FORMAT)
} catch (err) {
if (err.message.includes('[object Undefined]')) {
const brokenValuePath = findBrokenRecord(lockfile)
const brokenValuePath = findBrokenRecord(normalizedLockfile)
if (brokenValuePath) {
throw new PnpmError('LOCKFILE_STRINGIFY', `Failed to stringify the lockfile object. Undefined value at: ${brokenValuePath}`)
}
@@ -127,7 +125,7 @@ function normalizeLockfile (lockfile: Lockfile, forceSharedFormat: boolean) {
importers: R.keys(lockfile.importers).reduce((acc, alias) => {
const importer = lockfile.importers[alias]
const normalizedImporter = {
specifiers: importer.specifiers,
specifiers: importer.specifiers ?? {},
}
for (const depType of DEPENDENCIES_FIELDS) {
if (!R.isEmpty(importer[depType] ?? {})) {

View File

@@ -149,16 +149,17 @@ test('writeLockfiles() fails with meaningful error, when an invalid lockfile obj
'is-negative': '1.0.0',
'is-positive': '1.0.0',
},
// eslint-disable-next-line
specifiers: undefined as any,
specifiers: {
'is-negative': '^1.0.0',
'is-positive': '^1.0.0',
},
},
},
lockfileVersion: LOCKFILE_VERSION,
packages: {
'/is-negative/1.0.0': {
dependencies: {
'is-positive': '2.0.0',
},
// eslint-disable-next-line
dependencies: undefined as any,
resolution: {
integrity: 'sha1-ChbBDewTLAqLCzb793Fo5VDvg/g=',
},
@@ -180,5 +181,5 @@ test('writeLockfiles() fails with meaningful error, when an invalid lockfile obj
currentLockfileDir: projectPath,
wantedLockfile,
wantedLockfileDir: projectPath,
})).toThrow(new PnpmError('LOCKFILE_STRINGIFY', 'Failed to stringify the lockfile object. Undefined value at: importers[.].specifiers'))
})).toThrow(new PnpmError('LOCKFILE_STRINGIFY', 'Failed to stringify the lockfile object. Undefined value at: packages./is-negative/1.0.0.dependencies'))
})