rsnext/scripts/pull-turbo-cache.js
Tobias Koppers 2766ebc8d3
improve turborepo caching (#64493)
### What?

There is a race condition in the `pull-build-cache` script. When the
remote cache entry was removed between the dry run and the real run, it
will run the command and caches whatever is in the native folder.

Seems like there are some very old leftover files there which lead to an
broken publish.

This changes the command to fail when there are no files and empties the
folder before running the script. This should lead to pull-build-cache
to failing instead.

Closes PACK-2957
Fixes #64468
2024-04-15 12:42:28 +02:00

67 lines
1.6 KiB
JavaScript

#!/usr/bin/env node
// @ts-check
const { spawn } = require('child_process')
;(async function () {
const target = process.argv[process.argv.length - 1]
let turboResult = ''
await new Promise((resolve, reject) => {
const child = spawn(
'/bin/bash',
['-c', `pnpm turbo run cache-build-native --dry=json -- ${target}`],
{
stdio: 'pipe',
}
)
child.stdout.on('data', (data) => {
turboResult += data.toString()
})
child.on('exit', (code, signal) => {
if (code || signal) {
return reject(
new Error(`invalid exit code ${code} or signal ${signal}`)
)
}
resolve(0)
})
})
const turboData = JSON.parse(turboResult)
const task = turboData.tasks.find((t) => t.command !== '<NONEXISTENT>')
if (!task) {
console.warn(`Failed to find related turbo task`, turboResult)
return
}
// pull cache if it was available
if (task.cache.local || task.cache.remote) {
console.log('Cache Status', task.taskId, task.hash, task.cache)
await new Promise((resolve, reject) => {
const child = spawn(
'/bin/bash',
['-c', `pnpm turbo run cache-build-native -- ${target}`],
{
stdio: 'inherit',
}
)
child.on('exit', (code, signal) => {
if (code || signal) {
return reject(
new Error(`invalid exit code ${code} or signal ${signal}`)
)
}
resolve(0)
})
})
} else {
console.warn(`No turbo cache was available, continuing...`)
console.warn(task)
}
})()