From 2c49270f386501d0a8355649f3317f08b42f09c3 Mon Sep 17 00:00:00 2001 From: Laurent Cozic Date: Tue, 11 Oct 2022 11:43:22 +0100 Subject: [PATCH] Tools: Trying to fix encodeAssets EEXIST error --- packages/app-mobile/tools/encodeAssets.js | 5 +++-- packages/tools/gulp/utils.js | 21 ++++++++++++++++++++- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/packages/app-mobile/tools/encodeAssets.js b/packages/app-mobile/tools/encodeAssets.js index d67389d1e..3e37465a2 100644 --- a/packages/app-mobile/tools/encodeAssets.js +++ b/packages/app-mobile/tools/encodeAssets.js @@ -25,7 +25,8 @@ async function encodeFile(sourcePath, destPath) { const hash = md5(buffer.toString('base64')); const js = `module.exports = \`${buffer.toString('base64')}\`;`; const outputPath = `${outputDir}/${destPath}.base64.js`; - await fs.mkdirp(utils.dirname(outputPath)); + console.info(`Encoding "${sourcePath}" => "${outputPath}"`); + await utils.mkdirp(utils.dirname(outputPath)); await fs.writeFile(outputPath, js); const ext = utils.fileExtension(sourcePath).toLowerCase(); @@ -44,7 +45,7 @@ async function encodeFile(sourcePath, destPath) { async function main() { await fs.remove(outputDir); - await fs.mkdirp(outputDir); + await utils.mkdirp(outputDir); const encodedFiles = []; const sourceAssetDir = `${rootDir}/../renderer/assets`; diff --git a/packages/tools/gulp/utils.js b/packages/tools/gulp/utils.js index 94312b451..18c244d58 100644 --- a/packages/tools/gulp/utils.js +++ b/packages/tools/gulp/utils.js @@ -134,14 +134,33 @@ utils.copyDir = async function(src, dest, options) { } }; +// Occasionally, fs.mkdirp throws a "EEXIST" error if the directory already +// exists, while it should actually ignore the error. So we have this wrapper +// that actually handle the error. It means in general this method should be +// preferred to avoid random failures on CI or when building the app. +// +// https://github.com/laurent22/joplin/issues/6935#issuecomment-1274404470 utils.mkdir = async function(dir) { if (utils.isWindows()) { return utils.execCommand(`if not exist "${utils.toSystemSlashes(dir)}" mkdir "${utils.toSystemSlashes(dir)}"`); } else { - return fs.mkdirp(dir); + try { + // Can't return right away, or the exception won't be caught + const result = await fs.mkdirp(dir); + return result; + } catch (error) { + // Shouldn't happen but sometimes does. So we ignore the error in + // this case. + if (error.code === 'EEXIST') return; + throw error; + } } }; +utils.mkdirp = async function(dir) { + return utils.mkdir(dir); +}; + utils.copyFile = async function(src, dest) { await fs.copy(src, dest); };