Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/vite/src/node/__tests__/commandLock.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function writeExistingLock(
config: ResolvedConfig,
startTime?: string,
pid = process.ppid,
outDir?: string,
) {
const lockDir = path.join(config.cacheDir, 'lock')
fs.mkdirSync(lockDir, { recursive: true })
Expand All @@ -59,6 +60,7 @@ function writeExistingLock(
pid,
startTime,
urls: null,
outDir,
}),
)
return lockPath
Expand Down Expand Up @@ -187,6 +189,50 @@ describe('command lock', () => {
expect(record.startTime).toBeUndefined()
})

test('records the resolved outDir for builds', async ({
config,
lockDir,
}) => {
const lock = await createCommandLock(config, 'build', null)
onTestFinished(() => lock.release())
const files = fs.readdirSync(lockDir)
const record = JSON.parse(
fs.readFileSync(path.join(lockDir, files[0]), 'utf8'),
)
expect(record.outDir).toBe(path.resolve(config.root, 'dist'))
})

test('allows a parallel build into a different outDir', async ({
tempDir,
}) => {
const clientConfig = await createConfig(tempDir, {
build: { outDir: 'dist/client' },
})
writeExistingLock(
clientConfig,
undefined,
undefined,
path.resolve(tempDir, 'dist/client'),
)
const serverConfig = await createConfig(tempDir, {
build: { outDir: 'dist/server' },
})
const lock = await createCommandLock(serverConfig, 'build', null)
onTestFinished(() => lock.release())
})

test('checks a parallel build into the same outDir', async ({ config }) => {
writeExistingLock(
config,
undefined,
undefined,
path.resolve(config.root, 'dist'),
)
await expect(createCommandLock(config, 'build', null)).rejects.toThrow(
'Found another Vite build process',
)
})

test('checks for duplicates when the current start time cannot be read', async ({
config,
}) => {
Expand Down
14 changes: 13 additions & 1 deletion packages/vite/src/node/commandLock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ interface CommandLockRecord {
pid: number
startTime?: string
urls: CommandLockUrls | null | undefined
/** The resolved output directory, only set for builds */
outDir?: string
}

export interface CommandLock {
Expand Down Expand Up @@ -52,6 +54,7 @@ function isCommandLockRecord(value: unknown): value is CommandLockRecord {
record.pid > 0 &&
(record.startTime === undefined ||
(typeof record.startTime === 'string' && record.startTime.length > 0)) &&
(record.outDir === undefined || typeof record.outDir === 'string') &&
(record.urls == null ||
(typeof record.urls === 'object' &&
Array.isArray(record.urls?.local) &&
Expand All @@ -69,7 +72,12 @@ function isSameCommand(
first.command === second.command &&
first.root === second.root &&
first.configFile === second.configFile &&
first.mode === second.mode
first.mode === second.mode &&
// Builds into different directories (e.g. a client and an SSR build) can
// run in parallel. Records from older versions have no `outDir`.
(first.outDir === undefined ||
second.outDir === undefined ||
first.outDir === second.outDir)
)
}

Expand Down Expand Up @@ -231,6 +239,10 @@ function createCommandLockController(
pid: process.pid,
startTime,
urls: getCommandLockUrls(urls),
outDir:
command === 'build'
? path.resolve(config.root, config.build.outDir)
: undefined,
}
const serialized = `${JSON.stringify(record, null, 2)}\n`
const lockDir = path.resolve(config.cacheDir, 'lock')
Expand Down
Loading