mirror of https://github.com/actions/toolkit
core: fix possible filesystem race condition
Otherwise CodeQL complains when @actions/core is used in bundled scripts.pull/1822/head
parent
6c4e082c18
commit
e817aa0d36
|
@ -15,13 +15,24 @@ export function issueFileCommand(command: string, message: any): void {
|
||||||
`Unable to find environment variable for file command ${command}`
|
`Unable to find environment variable for file command ${command}`
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
if (!fs.existsSync(filePath)) {
|
|
||||||
throw new Error(`Missing file at path: ${filePath}`)
|
// do not use appendFileSync() because of CodeQL js/file-system-race
|
||||||
|
let fd
|
||||||
|
try {
|
||||||
|
fd = fs.openSync(filePath, 'a')
|
||||||
|
} catch (err) {
|
||||||
|
if (err.code === 'ENOENT') {
|
||||||
|
throw new Error(`Missing file at path: ${filePath}`)
|
||||||
|
} else {
|
||||||
|
throw err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fs.appendFileSync(filePath, `${toCommandValue(message)}${os.EOL}`, {
|
try {
|
||||||
encoding: 'utf8'
|
fs.writeSync(fd, `${toCommandValue(message)}${os.EOL}`, null, 'utf8')
|
||||||
})
|
} finally {
|
||||||
|
fs.closeSync(fd)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function prepareKeyValueMessage(key: string, value: any): string {
|
export function prepareKeyValueMessage(key: string, value: any): string {
|
||||||
|
|
Loading…
Reference in New Issue