Files
pnpm/workspace/state/test/loadWorkspaceState.test.ts
qybaihe 37669c200d fix: avoid workspace state parse crashes (#12094)
* fix: avoid workspace state parse crashes

* fix(workspace-state): write workspace state atomically

Port the atomic-write half of the pnpm fix for #12020 to pacquet.
pacquet's install/add/update/remove all call update_workspace_state,
and the on-disk file is shared with the pnpm CLI, so a non-atomic
fs::write could leave a torn file that a concurrent `pnpm run` reads
and crashes on. Write to a temp file in the same directory and rename
it into place, mirroring pnpm's switch to write-file-atomic.

---------

Co-authored-by: Zoltan Kochan <z@kochan.io>
2026-06-01 15:57:41 +02:00

86 lines
2.9 KiB
TypeScript

import fs from 'node:fs'
import path from 'node:path'
import { afterEach, beforeEach, expect, jest, test } from '@jest/globals'
import { logger } from '@pnpm/logger'
import { prepareEmpty } from '@pnpm/prepare'
import type { ProjectRootDir } from '@pnpm/types'
import { getFilePath } from '../src/filePath.js'
import { loadWorkspaceState, type WorkspaceState } from '../src/index.js'
const lastValidatedTimestamp = Date.now()
const originalLoggerDebug = logger.debug
beforeEach(() => {
logger.debug = jest.fn(originalLoggerDebug)
})
afterEach(() => {
logger.debug = originalLoggerDebug
})
const expectedLoggerCalls = [[{ msg: 'loading workspace state' }]]
test('loadWorkspaceState() when cache dir does not exist', async () => {
prepareEmpty()
const workspaceDir = process.cwd()
expect(loadWorkspaceState(workspaceDir)).toBeUndefined()
expect(jest.mocked(logger.debug).mock.calls).toStrictEqual(expectedLoggerCalls)
})
test('loadWorkspaceState() when cache dir exists but not the file', async () => {
prepareEmpty()
const workspaceDir = process.cwd()
const cacheFile = getFilePath(workspaceDir)
fs.mkdirSync(path.dirname(cacheFile), { recursive: true })
expect(loadWorkspaceState(workspaceDir)).toBeUndefined()
expect(jest.mocked(logger.debug).mock.calls).toStrictEqual(expectedLoggerCalls)
})
test('loadWorkspaceState() when cache file exists and is correct', async () => {
prepareEmpty()
const workspaceDir = process.cwd()
const cacheFile = getFilePath(workspaceDir)
fs.mkdirSync(path.dirname(cacheFile), { recursive: true })
const workspaceState: WorkspaceState = {
settings: {
autoInstallPeers: true,
dedupeDirectDeps: true,
excludeLinksFromLockfile: false,
preferWorkspacePackages: false,
injectWorkspacePackages: false,
catalogs: {
default: {
foo: '0.1.2',
},
},
linkWorkspacePackages: true,
},
lastValidatedTimestamp,
projects: {
[path.resolve('packages/a') as ProjectRootDir]: {},
[path.resolve('packages/b') as ProjectRootDir]: {},
[path.resolve('packages/c') as ProjectRootDir]: {},
[path.resolve('packages/d') as ProjectRootDir]: {},
},
pnpmfiles: [],
filteredInstall: false,
}
fs.writeFileSync(cacheFile, JSON.stringify(workspaceState))
expect(loadWorkspaceState(workspaceDir)).toStrictEqual(workspaceState)
expect(jest.mocked(logger.debug).mock.calls).toStrictEqual(expectedLoggerCalls)
})
test('loadWorkspaceState() when cache file contains partial JSON', async () => {
prepareEmpty()
const workspaceDir = process.cwd()
const cacheFile = getFilePath(workspaceDir)
fs.mkdirSync(path.dirname(cacheFile), { recursive: true })
fs.writeFileSync(cacheFile, '{\n "settings": ')
expect(loadWorkspaceState(workspaceDir)).toBeUndefined()
expect(jest.mocked(logger.debug).mock.calls).toStrictEqual(expectedLoggerCalls)
})