From ac18e75a32220be26c94fca0f54f8bb8066ea0bd Mon Sep 17 00:00:00 2001 From: DeeDeeG Date: Tue, 19 Nov 2024 00:42:46 -0500 Subject: [PATCH] electron-builder: Fix race condition when preparing to copy binaries There was a race condition where two separate pieces of async code would A) create the 'binaries' folder, and B) copy binaries to that folder. The problem was the lack of a guarantee they would happen in that order! If the attempt to copy files happened before the directory got created, the whole electron-builder run would error and/or stall out from unsuccessful auto-retries failing. To fix the situation: Ensure the 'binaries' dir exists *before* copying binaries to it. Do so by making the directory *first* with a *synchronous* function. (I might usually try to eliminate race conditions while still using asynchronous code, but for simple scripts, I prefer to use synchronous code, for simplicity's sake and for reliability.) In other words: Can't copy files to a destination dir that doesn't exist! This makes sure the directory exists *first*, then copy. Also: Log any error we hit while making the dir, but try to continue. (For anyone who has successfully run electorn-builder once in the repo, they already have a 'binaries' folder, so we can ignore any error when trying to "make the dir" that already exists. If it doesn't exist, the copy operation will print another relevant error message. So we don't need to error early here. But logging it should help if this issue needs any troubleshooting on anyone's machines (including CI) any time in the future.) --- script/electron-builder.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/script/electron-builder.js b/script/electron-builder.js index a49fd52f40..45d94836dd 100644 --- a/script/electron-builder.js +++ b/script/electron-builder.js @@ -1,6 +1,7 @@ const path = require('path') const normalizePackageData = require('normalize-package-data'); const fs = require("fs/promises"); +const {mkdirSync} = require("fs"); const generateMetadata = require('./generate-metadata-for-builder') const macBundleDocumentTypes = require("./mac-bundle-document-types.js"); @@ -289,7 +290,13 @@ async function main() { config: options }).then((result) => { console.log("Built binaries") - fs.mkdir('binaries').catch(() => "") + try { + mkdirSync('binaries', {recursive: true}) + } catch (err) { + console.warn("Warning: error encountered when making the 'binaries' dir.") + console.warn("(HINT: If the 'binaries' folder already exists, then this error message is probably fine to ignore!)") + console.warn(err) + } Promise.all(result.map(r => fs.copyFile(r, path.join('binaries', path.basename(r))))) }).catch((error) => { console.error("Error building binaries")