Skip to content

Commit 3f0dbea

Browse files
authored
fix(cli): create build dir before writing lock file (#1295)
1 parent 2fd7bcf commit 3f0dbea

2 files changed

Lines changed: 21 additions & 1 deletion

File tree

packages/nuxi/src/utils/lockfile.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { readFileSync, unlinkSync, writeFileSync } from 'node:fs'
1+
import { mkdirSync, readFileSync, unlinkSync, writeFileSync } from 'node:fs'
22
import process from 'node:process'
33

44
import { join } from 'pathe'
@@ -98,6 +98,13 @@ export function acquireLock(
9898
...info,
9999
}
100100

101+
// The build dir may not exist yet (e.g. `rimraf .nuxt && nuxt dev`); the
102+
// lock is acquired before `clearBuildDir` runs, so create it lazily.
103+
try {
104+
mkdirSync(buildDir, { recursive: true })
105+
}
106+
catch {}
107+
101108
// Try exclusive-create up to twice: the first attempt may race with a stale
102109
// lock that we then clean up and retry.
103110
for (let attempt = 0; attempt < 2; attempt++) {

packages/nuxi/test/unit/lockfile.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,19 @@ describe('lockfile', () => {
6767
expect(existsSync(lockPath)).toBe(false)
6868
})
6969

70+
it('creates the build dir if it does not exist yet', async () => {
71+
const buildDir = join(tempDir, 'missing', '.nuxt')
72+
expect(existsSync(buildDir)).toBe(false)
73+
74+
const lock = acquireLock(buildDir, { command: 'dev', cwd: '/project' })
75+
const lockPath = join(buildDir, 'nuxt.lock')
76+
77+
expect(lock.existing).toBeUndefined()
78+
expect(existsSync(lockPath)).toBe(true)
79+
80+
lock.release!()
81+
})
82+
7083
it('returns existing lock when another live process holds it', async () => {
7184
// Stub process.kill so liveness is deterministic across OSes (Windows
7285
// PID 1 semantics differ).

0 commit comments

Comments
 (0)