From e2381e8f96517dbfae913553c50be6514b067c5f Mon Sep 17 00:00:00 2001 From: fabasoad Date: Wed, 29 Oct 2025 23:29:36 +0900 Subject: [PATCH] fix: js/file-system-race --- packages/core/src/file-command.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/core/src/file-command.ts b/packages/core/src/file-command.ts index 30c9519eed..0e02c5d663 100644 --- a/packages/core/src/file-command.ts +++ b/packages/core/src/file-command.ts @@ -15,13 +15,21 @@ export function issueFileCommand(command: string, message: any): void { `Unable to find environment variable for file command ${command}` ) } - if (!fs.existsSync(filePath)) { + + let fileDescriptor: number | undefined + try { + fileDescriptor = fs.openSync(filePath, fs.constants.O_APPEND | fs.constants.O_WRONLY) + } catch (err) { throw new Error(`Missing file at path: ${filePath}`) } - fs.appendFileSync(filePath, `${toCommandValue(message)}${os.EOL}`, { - encoding: 'utf8' - }) + try { + fs.appendFileSync(fileDescriptor, `${toCommandValue(message)}${os.EOL}`, { + encoding: 'utf8' + }) + } finally { + fs.closeSync(fileDescriptor) + } } export function prepareKeyValueMessage(key: string, value: any): string {