fix(desktop): never resolve a source root's interpreter off PATH
findPythonForRoot ended in findSystemPython(), so a checkout with no
in-tree venv/.venv produced a PATH Python. Two designed refusals were
therefore unreachable: readSourceUpdate's `!managed && !probe.python`
guard (checkout-source.ts) and StateDbPreflight's `string | null` python
plus its "Python not found" throw (state-db-preflight.ts). Both were
written expecting null and could never see it.
A PATH Python can import a checkout while lacking its selected
dependencies, which is the failure the checkout-source comment already
names, so a failed read became a wrong answer instead of a refusal:
the update probe, the state.db pre-flight and the source backend all
ran under an interpreter nothing selected. PM deletes the in-tree
venv/.venv once a generation is committed, making null the ordinary
answer for a managed install — its callers resolve the installation
launcher instead, and the backend ladder falls through to its next rung.
resolveSourcePython owns the decision (override, then the checkout's own
venv, else null) and findPythonForRoot keeps its signature, so the four
call sites are unchanged. findSystemPython stays for the uninstaller,
whose interpreter choice is a separate, deliberate one for a locked venv.
2026-09-25 01:01:45 -04:00
|
|
|
import { statSync } from 'node:fs'
|
|
|
|
|
import path from 'node:path'
|
|
|
|
|
|
|
|
|
|
export interface SourcePythonOptions {
|
|
|
|
|
/** Explicit developer interpreter (`HERMES_DESKTOP_PYTHON`), honoured as-is. */
|
|
|
|
|
override?: string
|
|
|
|
|
isWindows?: boolean
|
|
|
|
|
fileExists?: (candidate: string) => boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/** A checkout's own interpreter paths, in preference order. */
|
|
|
|
|
function interpreterPaths(isWindows: boolean): string[] {
|
|
|
|
|
return isWindows
|
|
|
|
|
? [path.join('.venv', 'Scripts', 'python.exe'), path.join('venv', 'Scripts', 'python.exe')]
|
|
|
|
|
: [path.join('.venv', 'bin', 'python'), path.join('venv', 'bin', 'python')]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function defaultFileExists(candidate: string): boolean {
|
|
|
|
|
try {
|
|
|
|
|
return statSync(candidate).isFile()
|
|
|
|
|
} catch {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The interpreter that belongs to a source checkout: an explicit override, else
|
|
|
|
|
* the checkout's own virtualenv, else null.
|
|
|
|
|
*
|
|
|
|
|
* Never a PATH Python. A system interpreter can import a checkout while lacking
|
|
|
|
|
* its selected dependencies (`updater/checkout-source.ts`), so falling back
|
|
|
|
|
* silently turns "this root has no runtime" into an update probe or a backend
|
|
|
|
|
* running under an interpreter nothing selected — and a failed read becomes a
|
|
|
|
|
* wrong answer instead of a refusal. PM deletes the in-tree `venv`/`.venv` once
|
|
|
|
|
* a generation is committed, which makes null the ordinary answer for a managed
|
|
|
|
|
* install: its callers resolve the installation launcher instead
|
|
|
|
|
* (`resolveSourceInstallationBackend`, `readSourceUpdate`) or refuse
|
|
|
|
|
* (`preflightStateDb`), and the backend ladder moves on to its next rung.
|
|
|
|
|
*/
|
|
|
|
|
export function resolveSourcePython(
|
|
|
|
|
root: string,
|
|
|
|
|
{ override, isWindows = process.platform === 'win32', fileExists = defaultFileExists }: SourcePythonOptions = {}
|
|
|
|
|
): string | null {
|
|
|
|
|
if (override && fileExists(override)) {
|
|
|
|
|
return override
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const relative of interpreterPaths(isWindows)) {
|
|
|
|
|
const candidate: string = path.join(root, relative)
|
|
|
|
|
|
|
|
|
|
if (fileExists(candidate)) {
|
|
|
|
|
return candidate
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null
|
2026-09-26 03:39:27 +00:00
|
|
|
}
|