Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 32 additions & 21 deletions src/commands/manifest/bazel/python-shim.mts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,21 @@ import path from 'node:path'

import { findBazelExecutable, prependBazelPath } from './process.mts'
import { isSea } from '../../../sea/runtime.mts'
import { InputError } from '../../../utils/errors.mts'
import { InputError, isErrnoException } from '../../../utils/errors.mts'

export type PythonShimResult = {
env: NodeJS.ProcessEnv
shimDir: string | undefined
}

function isPython3(
// Python can take longer to start on a loaded Windows runner.
const PYTHON_PROBE_TIMEOUT_MS = process.platform === 'win32' ? 30_000 : 10_000

function checkPython3(
executable: string,
env: NodeJS.ProcessEnv,
cwd: string,
): boolean {
): InputError | undefined {
try {
const version = execFileSync(
executable,
Expand All @@ -33,17 +36,20 @@ function isPython3(
env,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'pipe'],
timeout: 10_000,
timeout: PYTHON_PROBE_TIMEOUT_MS,
},
)
return version.trim() === '3'
} catch {
if (version.trim() === '3') {
return undefined
}
} catch (error) {
if (isErrnoException(error) && error.code === 'ETIMEDOUT') {
return new InputError(
`Python 3 did not respond within ${PYTHON_PROBE_TIMEOUT_MS / 1000} seconds: ${executable}. Retry once the interpreter can start normally.`,
)
}
// Windows application aliases can resolve without a working interpreter.
return false
}
}

function notPython3Error(executable: string): InputError {
return new InputError(
`Python 3 is required for Bazel manifest extraction. ${executable} did not run as Python 3. Install Python 3 and ensure python${process.platform === 'win32' ? '.exe' : ''} is on PATH or set SOCKET_CLI_PYTHON_PATH in a standalone build.`,
)
Expand Down Expand Up @@ -82,8 +88,9 @@ export async function provisionPythonShim(
'Bazel on Windows requires SOCKET_CLI_PYTHON_PATH to point to a native Python 3 python.exe.',
)
}
if (!isPython3(executable, env, cwd)) {
throw notPython3Error(executable)
const error = checkPython3(executable, env, cwd)
if (error) {
throw error
}
if (process.platform === 'win32') {
if (path.basename(executable).toLowerCase() !== 'python.exe') {
Expand All @@ -109,6 +116,7 @@ export async function provisionPythonShim(
return { env: prependBazelPath(env, shimDir), shimDir }
}
const python = await findBazelExecutable('python', env, cwd)
let pythonError: InputError | undefined
if (python) {
if (
process.platform === 'win32' &&
Expand All @@ -118,11 +126,12 @@ export async function provisionPythonShim(
'Bazel on Windows requires a working Python 3 python.exe on PATH. Python batch launchers are not supported.',
)
}
if (isPython3(python, env, cwd)) {
pythonError = checkPython3(python, env, cwd)
if (!pythonError) {
return { env, shimDir: undefined }
}
if (process.platform === 'win32') {
throw notPython3Error(python)
throw pythonError
}
} else if (process.platform === 'win32') {
throw new InputError(
Expand All @@ -131,14 +140,16 @@ export async function provisionPythonShim(
}
const python3 = await findBazelExecutable('python3', env, cwd)
if (!python3) {
throw python
? notPython3Error(python)
: new InputError(
'Neither python nor python3 was found on PATH. Install Python 3 and ensure it is on PATH, then retry.',
)
throw (
pythonError ??
new InputError(
'Neither python nor python3 was found on PATH. Install Python 3 and ensure it is on PATH, then retry.',
)
)
}
if (!isPython3(python3, env, cwd)) {
throw notPython3Error(python3)
const python3Error = checkPython3(python3, env, cwd)
if (python3Error) {
throw python3Error
}
const shimDir = mkdtempSync(path.join(os.tmpdir(), 'socket-bazel-python-'))
symlinkSync(python3, path.join(shimDir, 'python'))
Expand Down
28 changes: 27 additions & 1 deletion src/commands/manifest/bazel/python-shim.test.mts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { execFileSync } from 'node:child_process'
import {
copyFileSync,
existsSync,
mkdtempSync,
realpathSync,
readlinkSync,
rmSync,
symlinkSync,
Expand All @@ -19,6 +21,7 @@ import {
import { provisionPythonShim } from './python-shim.mts'
import { isSea } from '../../../sea/runtime.mts'

vi.mock('node:child_process', { spy: true })
vi.mock('../../../sea/runtime.mts', () => ({ isSea: vi.fn() }))

const python = await findBazelExecutable(
Expand Down Expand Up @@ -58,6 +61,29 @@ describe('provisionPythonShim', () => {
).rejects.toThrow(/did not run as Python 3/)
})

it('reports an interpreter probe timeout separately from a non-Python binary', async () => {
const executable = path.join(
cwd,
process.platform === 'win32' ? 'python.exe' : 'python',
)
copyFileSync(process.execPath, executable)
const probe = vi.mocked(execFileSync)
probe.mockImplementationOnce(() => {
throw Object.assign(new Error('spawn timed out'), { code: 'ETIMEDOUT' })
})

const timeoutMs = process.platform === 'win32' ? 30_000 : 10_000
await expect(
provisionPythonShim(getBazelEnvironment({ PATH: cwd }), cwd),
).rejects.toThrow(
`Python 3 did not respond within ${timeoutMs / 1000} seconds`,
)
const [command, args, options] = probe.mock.lastCall ?? []
expect(command && path.relative(realpathSync(executable), command)).toBe('')
expect(args).toEqual(['-c', 'import sys\nprint(sys.version_info.major)'])
expect(options).toMatchObject({ timeout: timeoutMs })
})

it.skipIf(!python)(
'checks a working interpreter in the supplied environment',
async () => {
Expand Down Expand Up @@ -85,7 +111,7 @@ describe('provisionPythonShim', () => {
),
).rejects.toThrow(/python/)
},
30_000,
45_000,
)

it.skipIf(!python || process.platform === 'win32')(
Expand Down
Loading