From 5e5f15a60cd0040544f0a0369aba5a9766e1ff04 Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 2 Dec 2026 14:29:51 -0500 Subject: [PATCH 1/2] fix(bazel): allow slow Windows Python startup --- src/commands/manifest/bazel/python-shim.mts | 53 +++++++++++-------- .../manifest/bazel/python-shim.test.mts | 29 +++++++++- 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/src/commands/manifest/bazel/python-shim.mts b/src/commands/manifest/bazel/python-shim.mts index 7d4f14efb..aa8d5cfae 100644 --- a/src/commands/manifest/bazel/python-shim.mts +++ b/src/commands/manifest/bazel/python-shim.mts @@ -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, @@ -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.`, ) @@ -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') { @@ -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' && @@ -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( @@ -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')) diff --git a/src/commands/manifest/bazel/python-shim.test.mts b/src/commands/manifest/bazel/python-shim.test.mts index cc213e314..516d893ed 100644 --- a/src/commands/manifest/bazel/python-shim.test.mts +++ b/src/commands/manifest/bazel/python-shim.test.mts @@ -1,7 +1,9 @@ +import { execFileSync } from 'node:child_process' import { copyFileSync, existsSync, mkdtempSync, + realpathSync, readlinkSync, rmSync, symlinkSync, @@ -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( @@ -58,6 +61,30 @@ 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`, + ) + expect(probe).toHaveBeenLastCalledWith( + realpathSync(executable), + expect.any(Array), + expect.objectContaining({ timeout: timeoutMs }), + ) + }) + it.skipIf(!python)( 'checks a working interpreter in the supplied environment', async () => { @@ -85,7 +112,7 @@ describe('provisionPythonShim', () => { ), ).rejects.toThrow(/python/) }, - 30_000, + 45_000, ) it.skipIf(!python || process.platform === 'win32')( From 7f35abd22d73501a769a5bbdc69526263952df94 Mon Sep 17 00:00:00 2001 From: jdalton Date: Wed, 2 Dec 2026 14:42:42 -0500 Subject: [PATCH 2/2] test: account for Windows executable path casing --- src/commands/manifest/bazel/python-shim.test.mts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/commands/manifest/bazel/python-shim.test.mts b/src/commands/manifest/bazel/python-shim.test.mts index 516d893ed..05063fc71 100644 --- a/src/commands/manifest/bazel/python-shim.test.mts +++ b/src/commands/manifest/bazel/python-shim.test.mts @@ -78,11 +78,10 @@ describe('provisionPythonShim', () => { ).rejects.toThrow( `Python 3 did not respond within ${timeoutMs / 1000} seconds`, ) - expect(probe).toHaveBeenLastCalledWith( - realpathSync(executable), - expect.any(Array), - expect.objectContaining({ timeout: timeoutMs }), - ) + 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)(