From 4f0db5f2719b6c7775921d46f4f85eb86eeb2069 Mon Sep 17 00:00:00 2001 From: Jesse Vincent Date: Mon, 18 Dec 2023 14:33:36 -0800 Subject: [PATCH] https://www.update.rocks/blog/fixing-the-python3/ If you are using electron-packager or electron-forge, you might encounter the following error : An unhandled rejection has occurred inside Forge: Error: /var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/electron-packager/darwin-x64/electron-example-darwin-x64-5AYzr2/Electron.app/Contents/Resources/app/node_modules/macos-alias/build/node_gyp_bins/python3: file "../../../../../../../../../../../../../Library/Frameworks/Python.framework/Versions/3.11/bin/python3.11" links out of the package This error is showing when trying to package an electron application for macOS with Electron Forge. It takes its root in electron-packager and is produced by node-gyp like described in this Github issue. https://github.com/nodejs/node-gyp/issues/2713 --- forge.config.js | 74 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 55 insertions(+), 19 deletions(-) diff --git a/forge.config.js b/forge.config.js index 4f3e11dfb..631161511 100644 --- a/forge.config.js +++ b/forge.config.js @@ -2,6 +2,7 @@ const { spawnSync } = require("child_process"); const { generateCLDRData } = require("./tools/precompile.js"); const { glob } = require('glob'); const fs = require('fs'); +const path = require('path'); let config = { packagerConfig: { @@ -142,28 +143,63 @@ let config = { } return packageJson; }, - packageAfterPrune: async (forgeConfig, buildPath, electronVersion, platform, arch) => { - const npmInstall = spawnSync("npm", ["install", "--omit=dev"], { - cwd: buildPath, - stdio: "inherit", - shell: true, - }); + packageAfterPrune: async ( + forgeConfig, + buildPath, + electronVersion, + platform, + arch, + ) => { + const npmInstall = spawnSync("npm", ["install", "--omit=dev"], { + cwd: buildPath, + stdio: "inherit", + shell: true, + }); + + // Clear out prebuilds for other architectures + // 1) we don't need them + // 2) windows binary signing tool blows up when it tries to sign them. + + const prebuilds = glob.GlobSync(`${buildPath}/**/prebuilds/*`); + const matchString = new RegExp(`prebuilds/${platform}`); + prebuilds.found.forEach(function (path) { + if (!path.match(matchString)) { + fs.rmSync(path, { recursive: true }); + } + }); + // Workaround from https://www.update.rocks/blog/fixing-the-python3/ + if (platform === "darwin") { + // Directory to inspect + const dirPath = path.join( + buildPath, + "node_modules/macos-alias/build/node_gyp_bins", + ); + // Check if the directory exists + if (fs.existsSync(dirPath)) { + // List files in the directory + console.log("Contents of the directory before removal: "); + const files = fs.readdirSync(path.join(dirPath)); + files.forEach((file) => { + console.log(file); + }); - console.log(buildPath); + // Files to remove + const filesToRemove = ["python", "python2", "python3"]; - // Clear out prebuilds for other architectures - // 1) we don't need them - // 2) windows binary signing tool blows up when it tries to sign them. - - const prebuilds = glob.GlobSync(`${buildPath}/**/prebuilds/*`); - const matchString = new RegExp(`prebuilds/${platform}`); - prebuilds.found.forEach(function(path) { - if (! path.match(matchString)) { - fs.rmdirSync(path, { recursive: true }); - } - }); + // Remove files if they exist + filesToRemove.forEach((file) => { + const filePath = path.join(dirPath, file); + if (fs.existsSync(filePath)) { + console.log(`Removing file: ${file}`); + fs.unlinkSync(filePath); + } + }); + } else { + console.log(`Directory not found: ${dirPath}`); + } + } + }, }, -} }; if (process.env.UNTRUSTED) { delete config['packagerConfig']['osxSign'];