Ignore Electron download skip flag during startup
Template tests / tests (push) Failing after 15s

This commit is contained in:
Iisyourdad
2026-06-10 19:34:00 -05:00
parent a94f262565
commit e20af548a0
3 changed files with 39 additions and 21 deletions
+23 -17
View File
@@ -4,6 +4,12 @@ const { spawnSync } = require('node:child_process');
const fs = require('node:fs');
const path = require('node:path');
const ELECTRON_SKIP_ENV_KEYS = [
'ELECTRON_SKIP_BINARY_DOWNLOAD',
'npm_config_electron_skip_binary_download',
'NPM_CONFIG_ELECTRON_SKIP_BINARY_DOWNLOAD',
];
function resolveElectronPackageRoot() {
try {
return path.dirname(require.resolve('electron/package.json'));
@@ -35,6 +41,17 @@ function platformBinaryCandidates(platform) {
}
}
function sanitizeElectronEnv(baseEnv = process.env) {
const env = { ...baseEnv };
delete env.ELECTRON_RUN_AS_NODE;
for (const key of ELECTRON_SKIP_ENV_KEYS) {
delete env[key];
}
return env;
}
function electronBinaryCandidates({ packageRoot, distDir, platform }) {
const candidatePaths = [];
const pathHint = packageRoot ? readElectronPathHint(packageRoot) : null;
@@ -52,8 +69,6 @@ function electronBinaryCandidates({ packageRoot, distDir, platform }) {
function runNpmRebuild({
packageRoot,
platform = process.platform,
arch = process.arch,
npmExecPath = process.env.npm_execpath || null,
npmNodeExecPath = process.env.npm_node_execpath || process.execPath,
}) {
@@ -66,11 +81,7 @@ function runNpmRebuild({
[npmExecPath, 'rebuild', 'electron', '--force', '--foreground-scripts'],
{
cwd: packageRoot,
env: {
...process.env,
npm_config_platform: platform,
npm_config_arch: arch,
},
env: sanitizeElectronEnv(),
stdio: 'inherit',
}
);
@@ -92,8 +103,6 @@ function runNpmRebuild({
function repairElectronInstall({
packageRoot,
platform = process.platform,
arch = process.arch,
}) {
const installScript = path.join(packageRoot, 'install.js');
if (!fs.existsSync(installScript)) {
@@ -102,11 +111,7 @@ function repairElectronInstall({
const result = spawnSync(process.execPath, [installScript], {
cwd: packageRoot,
env: {
...process.env,
npm_config_platform: platform,
npm_config_arch: arch,
},
env: sanitizeElectronEnv(),
stdio: 'inherit',
});
@@ -137,6 +142,7 @@ function buildMissingElectronError({ packageRoot, distDir, candidatePaths }) {
'',
' npm install',
' npm rebuild electron --force --foreground-scripts',
' make sure ELECTRON_SKIP_BINARY_DOWNLOAD is not set',
'',
'If that does not help, delete node_modules/electron and install again.',
'',
@@ -149,7 +155,6 @@ function resolveElectronBinary({
packageRoot = resolveElectronPackageRoot(),
platform = process.platform,
overrideDistPath = process.env.ELECTRON_OVERRIDE_DIST_PATH || null,
arch = process.arch,
} = {}) {
if (!packageRoot && !overrideDistPath) {
throw new Error(
@@ -164,7 +169,7 @@ function resolveElectronBinary({
const resolved = candidatePaths.find((candidate) => fs.existsSync(candidate));
if (!resolved) {
if (packageRoot) {
if (runNpmRebuild({ packageRoot, platform, arch })) {
if (runNpmRebuild({ packageRoot })) {
const rebuilt = electronBinaryCandidates({ packageRoot, distDir, platform }).find((candidate) =>
fs.existsSync(candidate)
);
@@ -173,7 +178,7 @@ function resolveElectronBinary({
}
}
if (repairElectronInstall({ packageRoot, platform, arch })) {
if (repairElectronInstall({ packageRoot })) {
const repaired = electronBinaryCandidates({ packageRoot, distDir, platform }).find((candidate) =>
fs.existsSync(candidate)
);
@@ -195,6 +200,7 @@ module.exports = {
readElectronPathHint,
repairElectronInstall,
runNpmRebuild,
sanitizeElectronEnv,
resolveElectronBinary,
resolveElectronPackageRoot,
platformBinaryCandidates,
+2 -3
View File
@@ -3,7 +3,7 @@
const { spawn } = require('node:child_process');
const { resolveElectronBinary } = require('./electron-launcher');
const { resolveElectronBinary, sanitizeElectronEnv } = require('./electron-launcher');
let electronPath;
try {
@@ -12,8 +12,7 @@ try {
console.error(error && error.message ? error.message : error);
process.exit(1);
}
const env = { ...process.env };
delete env.ELECTRON_RUN_AS_NODE;
const env = sanitizeElectronEnv();
const child = spawn(electronPath, ['.'], {
stdio: 'inherit',
+14 -1
View File
@@ -49,14 +49,22 @@ test('repairs a broken Electron install before resolving the binary', (t) => {
[
"const fs = require('node:fs');",
"const path = require('node:path');",
"if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) process.exit(2);",
"fs.mkdirSync(path.join(__dirname, 'dist'), { recursive: true });",
"fs.writeFileSync(path.join(__dirname, 'dist', 'electron.exe'), 'binary');",
"fs.writeFileSync(path.join(__dirname, 'path.txt'), 'electron.exe');",
].join('\n')
);
const originalSkip = process.env.ELECTRON_SKIP_BINARY_DOWNLOAD;
process.env.ELECTRON_SKIP_BINARY_DOWNLOAD = '1';
t.after(() => {
if (originalSkip === undefined) delete process.env.ELECTRON_SKIP_BINARY_DOWNLOAD;
else process.env.ELECTRON_SKIP_BINARY_DOWNLOAD = originalSkip;
});
assert.equal(
repairElectronInstall({ packageRoot: root, platform: 'win32' }),
repairElectronInstall({ packageRoot: root }),
true
);
assert.equal(
@@ -76,6 +84,7 @@ test('rebuilds Electron through npm when the binary is missing', (t) => {
[
"const fs = require('node:fs');",
"const path = require('node:path');",
"if (process.env.ELECTRON_SKIP_BINARY_DOWNLOAD) process.exit(2);",
"fs.mkdirSync(path.join(__dirname, 'dist'), { recursive: true });",
"fs.writeFileSync(path.join(__dirname, 'dist', 'electron.exe'), 'binary');",
"fs.writeFileSync(path.join(__dirname, 'path.txt'), 'electron.exe');",
@@ -84,13 +93,17 @@ test('rebuilds Electron through npm when the binary is missing', (t) => {
const originalNpmExecPath = process.env.npm_execpath;
const originalNpmNodeExecPath = process.env.npm_node_execpath;
const originalSkip = process.env.ELECTRON_SKIP_BINARY_DOWNLOAD;
process.env.npm_execpath = fakeNpmCli;
process.env.npm_node_execpath = process.execPath;
process.env.ELECTRON_SKIP_BINARY_DOWNLOAD = '1';
t.after(() => {
if (originalNpmExecPath === undefined) delete process.env.npm_execpath;
else process.env.npm_execpath = originalNpmExecPath;
if (originalNpmNodeExecPath === undefined) delete process.env.npm_node_execpath;
else process.env.npm_node_execpath = originalNpmNodeExecPath;
if (originalSkip === undefined) delete process.env.ELECTRON_SKIP_BINARY_DOWNLOAD;
else process.env.ELECTRON_SKIP_BINARY_DOWNLOAD = originalSkip;
});
assert.equal(