diff --git a/electron/main/child-env.test.mjs b/electron/main/child-env.test.mjs new file mode 100644 index 00000000..e71b3a70 --- /dev/null +++ b/electron/main/child-env.test.mjs @@ -0,0 +1,72 @@ +import test from 'node:test' +import assert from 'node:assert/strict' +import { buildSync } from 'esbuild' +import { createRequire } from 'node:module' +import { mkdtempSync, writeFileSync } from 'node:fs' +import { tmpdir } from 'node:os' +import { join, resolve } from 'node:path' + +function loadModule() { + const outfile = join(mkdtempSync(join(tmpdir(), 'modly-child-env-test-')), 'child-env.cjs') + const require = createRequire(import.meta.url) + const result = buildSync({ + entryPoints: [resolve('electron/main/child-env.ts')], + bundle: true, + platform: 'node', + format: 'cjs', + write: false, + }) + writeFileSync(outfile, result.outputFiles[0].text, 'utf8') + return require(outfile) +} + +test('pythonChildEnv strips vars that redirect to the user\'s Python', () => { + const { pythonChildEnv } = loadModule() + const env = pythonChildEnv({ HOME: '/home/u', PYTHONPATH: '/x', VIRTUAL_ENV: '/venv', PIP_USER: '1', CONDA_PREFIX: '/c' }) + assert.deepEqual(env, { HOME: '/home/u' }) +}) + +test('extensionChildEnv drops AppImage mount entries from library and binary paths (#236)', () => { + const { extensionChildEnv } = loadModule() + const mount = '/tmp/.mount_Modly-AbC123' + const env = extensionChildEnv({ + APPDIR: mount, + APPIMAGE: '/home/u/Modly.AppImage', + LD_LIBRARY_PATH: `${mount}/usr/lib:${mount}:/usr/local/cuda/lib64`, + LD_PRELOAD: `${mount}/usr/lib/libfoo.so /usr/lib/libbar.so`, + PATH: `${mount}/usr/bin:/usr/local/bin:/usr/bin`, + }) + assert.equal(env.LD_LIBRARY_PATH, '/usr/local/cuda/lib64') + assert.equal(env.LD_PRELOAD, '/usr/lib/libbar.so') + assert.equal(env.PATH, '/usr/local/bin:/usr/bin') + // The AppImage identity vars themselves are harmless and kept + assert.equal(env.APPDIR, mount) +}) + +test('extensionChildEnv removes a path var left empty, and keeps look-alike siblings', () => { + const { extensionChildEnv } = loadModule() + const mount = '/tmp/.mount_Modly-AbC123/' + const env = extensionChildEnv({ + APPDIR: mount, + LD_LIBRARY_PATH: '/tmp/.mount_Modly-AbC123/usr/lib', + PATH: '/tmp/.mount_Modly-AbC123-other/bin:/usr/bin', + }) + assert.equal('LD_LIBRARY_PATH' in env, false) + assert.equal(env.PATH, '/tmp/.mount_Modly-AbC123-other/bin:/usr/bin') +}) + +test('extensionChildEnv leaves paths alone outside an AppImage', () => { + const { extensionChildEnv } = loadModule() + const input = { LD_LIBRARY_PATH: '/opt/lib', PATH: '/usr/bin' } + assert.deepEqual(extensionChildEnv(input), input) + // The caller's env object is not mutated + const appEnv = { APPDIR: '/tmp/.mount_x', LD_LIBRARY_PATH: '/tmp/.mount_x/usr/lib' } + extensionChildEnv(appEnv) + assert.equal(appEnv.LD_LIBRARY_PATH, '/tmp/.mount_x/usr/lib') +}) + +test('pythonChildEnv keeps AppImage paths, for processes that relaunch the bundled Electron', () => { + const { pythonChildEnv } = loadModule() + const env = pythonChildEnv({ APPDIR: '/tmp/.mount_x', LD_LIBRARY_PATH: '/tmp/.mount_x/usr/lib', PYTHONPATH: '/x' }) + assert.deepEqual(env, { APPDIR: '/tmp/.mount_x', LD_LIBRARY_PATH: '/tmp/.mount_x/usr/lib' }) +}) diff --git a/electron/main/child-env.ts b/electron/main/child-env.ts new file mode 100644 index 00000000..28cf48a9 --- /dev/null +++ b/electron/main/child-env.ts @@ -0,0 +1,56 @@ +// Environment for the Python processes Modly spawns. Kept free of Electron +// imports so it can be unit-tested directly. + +/** Vars that would redirect imports or installs to the user's own Python. */ +const PYTHON_REDIRECT_VARS = [ + 'PYTHONHOME', 'PYTHONPATH', 'PYTHONSTARTUP', 'PYTHONUSERBASE', + 'PIP_USER', 'PIP_TARGET', 'PIP_PREFIX', 'PIP_REQUIRE_VIRTUALENV', + 'VIRTUAL_ENV', 'CONDA_PREFIX', +] + +/** + * Search-path vars an AppImage launcher points into its own mount. Python and + * the compilers pip runs would load the AppImage's bundled libraries instead of + * the system ones, which breaks venv creation and native builds (#236). + */ +const APPIMAGE_PATH_VARS: Record = { + LD_LIBRARY_PATH: ':', + LD_PRELOAD: ' ', + PATH: ':', +} + +function withoutEntriesUnder(value: string, dir: string, separator: string): string { + const root = dir.replace(/\/+$/, '') + return value + .split(separator === ' ' ? /[\s:]+/ : separator) + .filter((entry) => entry !== '' && entry !== root && !entry.startsWith(`${root}/`)) + .join(separator) +} + +/** The env without vars that redirect to the user's system Python. */ +export function pythonChildEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv { + const clean: NodeJS.ProcessEnv = { ...env } + for (const name of PYTHON_REDIRECT_VARS) delete clean[name] + return clean +} + +/** + * pythonChildEnv, plus no path entries into the AppImage mount. Only for + * extension setup, which runs the stable Python copy, pip and system compilers. + * Not for the API or process extensions: they relaunch the Electron binary that + * lives inside the mount (MODLY_NODE_EXECUTABLE) and may need its libraries. + */ +export function extensionChildEnv(env: NodeJS.ProcessEnv): NodeJS.ProcessEnv { + const clean = pythonChildEnv(env) + // APPDIR is set by the AppImage runtime to its mount (/tmp/.mount_Modly-XXXX). + const appDir = env.APPDIR + if (!appDir) return clean + for (const [name, separator] of Object.entries(APPIMAGE_PATH_VARS)) { + const value = clean[name] + if (value === undefined) continue + const kept = withoutEntriesUnder(value, appDir, separator) + if (kept) clean[name] = kept + else delete clean[name] + } + return clean +} diff --git a/electron/main/ipc-handlers.ts b/electron/main/ipc-handlers.ts index 0632620f..6fb1256c 100644 --- a/electron/main/ipc-handlers.ts +++ b/electron/main/ipc-handlers.ts @@ -47,6 +47,7 @@ import { validateStorageLocation, type StorageKind, } from './storage-directory-guard' import { checkSetupNeeded, markSetupDone, runFullSetup, getVenvPythonExe, ensureSslPatch } from './python-setup' +import { extensionChildEnv } from './child-env' import { logger } from './logger' import { getProcessRunner, getPythonProcessRunner, getExtPythonExe, terminateProcessRunner, terminateAllProcessRunners } from './process-runner' import { getBuiltinExtensionsDir } from './builtin-sync' @@ -143,7 +144,7 @@ function runExtensionSetup( const proc = spawn(pythonExe, ['-c', launcher, setupPy, args], { stdio: ['ignore', 'pipe', 'pipe'], env: { - ...process.env, + ...extensionChildEnv(process.env), PIP_CACHE_DIR: pipCacheDir, MODLY_TORCH_FLAVOR: torchFlavor, MODLY_TORCH_INDEX_URL: gpu.torchIndexUrl ?? '', diff --git a/electron/main/python-setup.ts b/electron/main/python-setup.ts index 13e86a25..15daff1c 100644 --- a/electron/main/python-setup.ts +++ b/electron/main/python-setup.ts @@ -6,6 +6,7 @@ import { spawn, execSync } from 'child_process' import { createHash } from 'crypto' import { getSettings } from './settings-store' import { copyRuntimeTree } from './copy-runtime' +import { pythonChildEnv } from './child-env' const SETUP_VERSION = 3 @@ -36,16 +37,7 @@ function hashRequirements(): string { * Strips vars that could redirect imports or installs to the user's system Python. */ export function cleanPythonEnv(): NodeJS.ProcessEnv { - const { - PYTHONHOME, PYTHONPATH, PYTHONSTARTUP, PYTHONUSERBASE, - PIP_USER, PIP_TARGET, PIP_PREFIX, PIP_REQUIRE_VIRTUALENV, - VIRTUAL_ENV, CONDA_PREFIX, - ...rest - } = process.env - void PYTHONHOME; void PYTHONPATH; void PYTHONSTARTUP; void PYTHONUSERBASE - void PIP_USER; void PIP_TARGET; void PIP_PREFIX; void PIP_REQUIRE_VIRTUALENV - void VIRTUAL_ENV; void CONDA_PREFIX - return rest + return pythonChildEnv(process.env) } // ─── Path helpers ─────────────────────────────────────────────────────────────