-
Notifications
You must be signed in to change notification settings - Fork 1.2k
update the init functions python flow to find available python runtimes on user's machine #10673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,8 @@ import { spawn } from "cross-spawn"; | |
| import * as cp from "child_process"; | ||
| import { logger } from "../logger"; | ||
| import { IS_WINDOWS } from "../utils"; | ||
| import * as supported from "../deploy/functions/runtimes/supported"; | ||
| import { getPythonBinary } from "../deploy/functions/runtimes/python"; | ||
|
|
||
| /** | ||
| * Default directory for python virtual environment. | ||
|
|
@@ -45,3 +47,51 @@ export function runWithVirtualEnv( | |
| env: envs as any, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Check if a python binary is available and return its version. | ||
| */ | ||
| export async function checkPythonVersion(binary: string): Promise<string | undefined> { | ||
| return new Promise((resolve) => { | ||
| const child = spawn(binary, ["--version"], { stdio: "pipe" }); | ||
| let output = ""; | ||
| child.stdout?.on("data", (data: Buffer) => { | ||
| output += data.toString(); | ||
| }); | ||
| child.stderr?.on("data", (data: Buffer) => { | ||
| output += data.toString(); | ||
| }); | ||
| child.on("close", (code: number) => { | ||
| if (code === 0) { | ||
| resolve(output.trim()); | ||
| } else { | ||
| resolve(undefined); | ||
| } | ||
| }); | ||
| child.on("error", () => { | ||
| resolve(undefined); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Get all available python runtimes on the user machine. | ||
| */ | ||
| export async function getAvailablePythonRuntimes(): Promise< | ||
| { runtime: supported.Runtime & supported.RuntimeOf<"python">; binary: string; version: string }[] | ||
| > { | ||
| const pythonRuntimes = (Object.keys(supported.RUNTIMES) as supported.Runtime[]).filter( | ||
| (runtime): runtime is supported.Runtime & supported.RuntimeOf<"python"> => | ||
| runtime.startsWith("python"), | ||
| ); | ||
|
|
||
| const results = await Promise.all( | ||
| pythonRuntimes.map(async (runtime) => { | ||
| const binary = getPythonBinary(runtime); | ||
| const version = await checkPythonVersion(binary); | ||
| return version ? { runtime, binary, version } : undefined; | ||
| }), | ||
| ); | ||
|
|
||
| return results.filter((r): r is NonNullable<typeof r> => !!r); | ||
| } | ||
|
Comment on lines
+80
to
+97
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On Windows, To fix this, we should parse the returned version string and verify that it matches the expected major and minor version of the runtime. export async function getAvailablePythonRuntimes(): Promise<
{ runtime: supported.Runtime & supported.RuntimeOf<"python">; binary: string; version: string }[]
> {
const pythonRuntimes = (Object.keys(supported.RUNTIMES) as supported.Runtime[]).filter(
(runtime): runtime is supported.Runtime & supported.RuntimeOf<"python"> =>
runtime.startsWith("python"),
);
const results = await Promise.all(
pythonRuntimes.map(async (runtime) => {
const binary = getPythonBinary(runtime);
const version = await checkPythonVersion(binary);
if (!version) {
return undefined;
}
const parts = runtime.match(/^python(\d)(\d+)$/);
const match = version.match(/(?:Python\s+)?(\d+)\.(\d+)/i);
if (parts && match) {
const [_, expectedMajor, expectedMinor] = parts;
const [__, major, minor] = match;
if (major !== expectedMajor || minor !== expectedMinor) {
return undefined;
}
}
return { runtime, binary, version };
}),
);
return results.filter((r): r is NonNullable<typeof r> => !!r);
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
checkPythonVersionfunction spawns a child process to check the Python version but does not enforce a timeout. If a binary hangs indefinitely (e.g., due to a broken installation or interactive prompt), the entirefirebase initflow will block forever.Additionally, the
codeparameter in thecloseevent listener can benullif the process is terminated by a signal. Specifyingcode: numberexplicitly might cause TypeScript compilation errors under strict type checking.Adding a timeout and letting TypeScript infer the
codetype improves robustness and type safety.