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 assert from 'node:assert/strict'
|
|
|
|
|
import path from 'node:path'
|
|
|
|
|
|
|
|
|
|
import { test } from 'vitest'
|
|
|
|
|
|
|
|
|
|
import { createSourcePythonBackend } from './source-backend'
|
|
|
|
|
import { resolveSourcePython } from './source-python'
|
|
|
|
|
|
|
|
|
|
const ROOT: string = path.join(path.sep, 'checkout')
|
|
|
|
|
|
|
|
|
|
/** A checkout reporting exactly `present` (root-relative) as existing files. */
|
|
|
|
|
function checkout(present: string[] = [], exists: boolean = true) {
|
|
|
|
|
const files = new Set(present.map(entry => path.join(ROOT, entry)))
|
|
|
|
|
|
2026-09-26 03:39:27 +00:00
|
|
|
return { root: ROOT, fileExists: (candidate: string): boolean => exists && files.has(candidate) }
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
test('a checkout with no virtualenv resolves to no interpreter, never a PATH Python', (): void => {
|
|
|
|
|
const { root, fileExists } = checkout()
|
|
|
|
|
|
|
|
|
|
// The invariant: "this root has no runtime" must reach callers as null.
|
|
|
|
|
// Answering with a PATH Python runs an install's update probe and its backend
|
|
|
|
|
// under an interpreter nothing selected — it can import the checkout while
|
|
|
|
|
// lacking its selected dependencies (updater/checkout-source.ts).
|
|
|
|
|
assert.equal(resolveSourcePython(root, { fileExists, isWindows: false }), null)
|
|
|
|
|
assert.equal(resolveSourcePython(root, { fileExists, isWindows: true }), null)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('a virtualenv-less checkout yields no source backend, so the ladder moves on', (): void => {
|
|
|
|
|
const { root, fileExists } = checkout()
|
|
|
|
|
const python: string | null = resolveSourcePython(root, { fileExists, isWindows: false })
|
|
|
|
|
|
|
|
|
|
assert.equal(createSourcePythonBackend(root, python, ['serve']), null)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('the checkout virtualenv is preferred over the flat venv, on both layouts', (): void => {
|
|
|
|
|
const venv: string = path.join('venv', 'bin', 'python')
|
|
|
|
|
const dotVenv: string = path.join('.venv', 'bin', 'python')
|
|
|
|
|
const windows: string = path.join('.venv', 'Scripts', 'python.exe')
|
|
|
|
|
|
|
|
|
|
assert.equal(
|
|
|
|
|
resolveSourcePython(ROOT, { fileExists: checkout([venv, dotVenv]).fileExists, isWindows: false }),
|
|
|
|
|
path.join(ROOT, dotVenv)
|
|
|
|
|
)
|
|
|
|
|
assert.equal(
|
|
|
|
|
resolveSourcePython(ROOT, { fileExists: checkout([venv]).fileExists, isWindows: false }),
|
|
|
|
|
path.join(ROOT, venv)
|
|
|
|
|
)
|
|
|
|
|
assert.equal(
|
|
|
|
|
resolveSourcePython(ROOT, { fileExists: checkout([windows]).fileExists, isWindows: true }),
|
|
|
|
|
path.join(ROOT, windows)
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
test('an explicit developer interpreter wins, and is only honoured when it exists', (): void => {
|
|
|
|
|
const preferred: string = path.join(path.sep, 'opt', 'hermes', 'bin', 'python')
|
|
|
|
|
const aged: string = path.join(ROOT, 'venv', 'bin', 'python')
|
|
|
|
|
const { root, fileExists } = checkout(['venv/bin/python'], true)
|
|
|
|
|
const withPreferred = (candidate: string): boolean => candidate === preferred || fileExists(candidate)
|
|
|
|
|
|
2026-09-26 03:39:27 +00:00
|
|
|
assert.equal(
|
|
|
|
|
resolveSourcePython(root, { override: preferred, fileExists: withPreferred, isWindows: false }),
|
|
|
|
|
preferred
|
|
|
|
|
)
|
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
|
|
|
// An override naming a file that is gone does not resurrect a PATH Python;
|
|
|
|
|
// it falls through to the checkout, then to null.
|
2026-09-26 03:39:27 +00:00
|
|
|
assert.equal(resolveSourcePython(root, { override: preferred, fileExists, isWindows: false }), aged)
|
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
|
|
|
assert.equal(
|
2026-09-26 03:39:27 +00:00
|
|
|
resolveSourcePython(ROOT, { override: preferred, fileExists: checkout().fileExists, isWindows: false }),
|
|
|
|
|
null
|
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
|
|
|
)
|
2026-09-26 03:39:27 +00:00
|
|
|
})
|