fix(modules-yaml): don't crash on empty file (#7508)

This commit is contained in:
Zoltan Kochan
2024-01-09 11:51:29 +01:00
parent dcf3ef7e4f
commit d349bc3a2d
4 changed files with 11 additions and 0 deletions

View File

@@ -0,0 +1,5 @@
---
"@pnpm/modules-yaml": patch
---
readModulesYaml should not crash on empty file.

View File

@@ -38,6 +38,7 @@ export async function readModulesManifest (modulesDir: string): Promise<Modules
let modules!: Modules
try {
modules = await readYamlFile<Modules>(modulesYamlPath)
if (!modules) return modules
} catch (err: any) { // eslint-disable-line
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') {
throw err

View File

View File

@@ -113,3 +113,8 @@ test('readModulesManifest() should create a node_modules directory if makeModule
await writeModulesManifest(modulesDir, modulesYaml, { makeModulesDir: true })
expect(await readModulesManifest(modulesDir)).toEqual(modulesYaml)
})
test('readModulesManifest does not fail on empty file', async () => {
const modulesYaml = await readModulesManifest(path.join(__dirname, 'fixtures/empty-modules-yaml'))
expect(modulesYaml).toBeUndefined()
})