Skip to content

Commit

Permalink
fix(js_run_devserver): ensure files are synced before waiting for nex…
Browse files Browse the repository at this point in the history
…t build event
  • Loading branch information
jbedard committed Nov 2, 2024
1 parent 207928e commit 193785d
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions js/private/js_run_devserver.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -447,38 +447,44 @@ async function main(args, sandbox) {
process.exit(code)
})

// Process stdin data in order using a promise chain.
let syncing = Promise.resolve()
process.stdin.on('data', async (chunk) => {
return (syncing = syncing.then(() => processChunk(chunk)))
})

async function processChunk(chunk) {
try {
const chunkString = chunk.toString()
if (chunkString.includes('IBAZEL_BUILD_COMPLETED SUCCESS')) {
if (process.env.JS_BINARY__LOG_DEBUG) {
console.error('IBAZEL_BUILD_COMPLETED SUCCESS')
}
// Chain promises via syncing.then()
syncing = syncing.then(() => {
// Re-parse the config file to get the latest list of data files to copy
const updatedDataFiles = JSON.parse(
fs.readFileSync(configPath)
).data_files

const oldFiles = config.data_files

// Re-parse the config file to get the latest list of data files to copy
const updatedDataFiles = JSON.parse(
await fs.promises.readFile(configPath)
).data_files

// Await promises to catch any exceptions, and wait for the
// sync to be complete before writing to stdin of the child
// process
await Promise.all([
// Remove files that were previously synced but are no longer in the updated list of files to sync
deleteFiles(
config.data_files,
updatedDataFiles,
sandbox
)
deleteFiles(oldFiles, updatedDataFiles, sandbox),

// Sync changed files
config.data_files = updatedDataFiles
syncFiles(
config.data_files,
updatedDataFiles,
sandbox,
config.grant_sandbox_write_permissions
)
})
// Await promise to catch any exceptions, and wait for the
// sync to be complete before writing to stdin of the child
// process
await syncing
),
])

// The latest state of copied data files
config.data_files = updatedDataFiles
} else if (chunkString.includes('IBAZEL_BUILD_STARTED')) {
if (process.env.JS_BINARY__LOG_DEBUG) {
console.error('IBAZEL_BUILD_STARTED')
Expand All @@ -499,7 +505,7 @@ async function main(args, sandbox) {
)
process.exit(1)
}
})
}
})
}

Expand Down

0 comments on commit 193785d

Please sign in to comment.