Skip to content
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

[python] Don't throw error directly when emitter crash #6104

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions .chronus/changes/trace-optimize-2025-1-24-9-32-20.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Change versionKind to one of: internal, fix, dependencies, feature, deprecation, breaking
changeKind: fix
packages:
- "@typespec/http-client-python"
---

[python] Don't throw error directly when emitter crash
117 changes: 68 additions & 49 deletions packages/http-client-python/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { EmitContext, NoTarget } from "@typespec/compiler";
import { execSync } from "child_process";
import fs from "fs";
import path, { dirname } from "path";
import process from "process";
import { loadPyodide } from "pyodide";
import { fileURLToPath } from "url";
import { emitCodeModel } from "./code-model.js";
Expand All @@ -24,9 +25,11 @@ export function getModelsMode(context: SdkContext): "dpg" | "none" {
if (modelModes.includes(specifiedModelsMode)) {
return specifiedModelsMode;
}
throw new Error(
`Need to specify models mode with the following values: ${modelModes.join(", ")}`,
);
reportDiagnostic(context.program, {
code: "invalid-models-mode",
target: NoTarget,
format: { inValidValue: specifiedModelsMode },
});
}
return "dpg";
}
Expand Down Expand Up @@ -144,54 +147,70 @@ export async function $onEmit(context: EmitContext<PythonEmitterOptions>) {
}
}

if (resolvedOptions["use-pyodide"]) {
// here we run with pyodide
const pyodide = await setupPyodideCall(root);
// create the output folder if not exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// mount output folder to pyodide
pyodide.FS.mkdirTree("/output");
pyodide.FS.mount(pyodide.FS.filesystems.NODEFS, { root: outputDir }, "/output");
// mount yaml file to pyodide
pyodide.FS.mkdirTree("/yaml");
pyodide.FS.mount(pyodide.FS.filesystems.NODEFS, { root: path.dirname(yamlPath) }, "/yaml");
const globals = pyodide.toPy({
outputFolder: "/output",
yamlFile: `/yaml/${path.basename(yamlPath)}`,
commandArgs,
});
const pythonCode = `
async def main():
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", SyntaxWarning) # bc of m2r2 dep issues
from pygen import m2r, preprocess, codegen, black
m2r.M2R(output_folder=outputFolder, cadl_file=yamlFile, **commandArgs).process()
preprocess.PreProcessPlugin(output_folder=outputFolder, cadl_file=yamlFile, **commandArgs).process()
codegen.CodeGenerator(output_folder=outputFolder, cadl_file=yamlFile, **commandArgs).process()
black.BlackScriptPlugin(output_folder=outputFolder, **commandArgs).process()

await main()`;
await pyodide.runPythonAsync(pythonCode, { globals });
} else {
// here we run with native python
let venvPath = path.join(root, "venv");
if (fs.existsSync(path.join(venvPath, "bin"))) {
venvPath = path.join(venvPath, "bin", "python");
} else if (fs.existsSync(path.join(venvPath, "Scripts"))) {
venvPath = path.join(venvPath, "Scripts", "python.exe");
try {
if (resolvedOptions["use-pyodide"]) {
// here we run with pyodide
const pyodide = await setupPyodideCall(root);
// create the output folder if not exists
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
// mount output folder to pyodide
pyodide.FS.mkdirTree("/output");
pyodide.FS.mount(pyodide.FS.filesystems.NODEFS, { root: outputDir }, "/output");
// mount yaml file to pyodide
pyodide.FS.mkdirTree("/yaml");
pyodide.FS.mount(pyodide.FS.filesystems.NODEFS, { root: path.dirname(yamlPath) }, "/yaml");
const globals = pyodide.toPy({
outputFolder: "/output",
yamlFile: `/yaml/${path.basename(yamlPath)}`,
commandArgs,
});
const pythonCode = `
async def main():
import warnings
with warnings.catch_warnings():
warnings.simplefilter("ignore", SyntaxWarning) # bc of m2r2 dep issues
from pygen import m2r, preprocess, codegen, black
m2r.M2R(output_folder=outputFolder, cadl_file=yamlFile, **commandArgs).process()
preprocess.PreProcessPlugin(output_folder=outputFolder, cadl_file=yamlFile, **commandArgs).process()
codegen.CodeGenerator(output_folder=outputFolder, cadl_file=yamlFile, **commandArgs).process()
black.BlackScriptPlugin(output_folder=outputFolder, **commandArgs).process()

await main()`;
await pyodide.runPythonAsync(pythonCode, { globals });
} else {
throw new Error("Virtual environment doesn't exist.");
// here we run with native python
let venvPath = path.join(root, "venv");
if (fs.existsSync(path.join(venvPath, "bin"))) {
venvPath = path.join(venvPath, "bin", "python");
} else if (fs.existsSync(path.join(venvPath, "Scripts"))) {
venvPath = path.join(venvPath, "Scripts", "python.exe");
} else {
reportDiagnostic(program, {
code: "pyodide-flag-conflict",
target: NoTarget,
});
}
commandArgs["output-folder"] = outputDir;
commandArgs["cadl-file"] = yamlPath;
const commandFlags = Object.entries(commandArgs)
.map(([key, value]) => `--${key}=${value}`)
.join(" ");
const command = `${venvPath} ${root}/eng/scripts/setup/run_tsp.py ${commandFlags}`;
execSync(command, { stdio: [process.stdin, process.stdout] });
}
commandArgs["output-folder"] = outputDir;
commandArgs["cadl-file"] = yamlPath;
const commandFlags = Object.entries(commandArgs)
.map(([key, value]) => `--${key}=${value}`)
.join(" ");
const command = `${venvPath} ${root}/eng/scripts/setup/run_tsp.py ${commandFlags}`;
execSync(command);
} catch (error: any) {
const errStackStart =
"========================================= error stack start ================================================";
const errStackEnd =
"========================================= error stack end ================================================";
const errStack = error.stack ? `\n${errStackStart}\n${error.stack}\n${errStackEnd}` : "";
reportDiagnostic(program, {
code: "unknown-error",
target: NoTarget,
format: { stack: errStack },
});
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion packages/http-client-python/emitter/src/lib.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SdkContext, SdkServiceOperation } from "@azure-tools/typespec-client-generator-core";
import { createTypeSpecLibrary, JSONSchemaType } from "@typespec/compiler";
import { createTypeSpecLibrary, JSONSchemaType, paramMessage } from "@typespec/compiler";

export interface PythonEmitterOptions {
"package-version"?: string;
Expand Down Expand Up @@ -56,6 +56,27 @@ const EmitterOptionsSchema: JSONSchemaType<PythonEmitterOptions> = {
const libDef = {
name: "@typespec/http-client-python",
diagnostics: {
// error
"unknown-error": {
severity: "error",
messages: {
default: paramMessage`Can't generate Python client code from this TypeSpec. Please open an issue on https://github.com/microsoft/typespec'.${"stack"}`,
},
},
"invalid-models-mode": {
severity: "error",
messages: {
default: paramMessage`Invalid value '${"inValidValue"}' for 'models-mode' of tspconfig.yaml and expected values are 'dpg'/'none'.`,
},
},
"pyodide-flag-conflict": {
severity: "error",
messages: {
default:
"Python is not installed. Please follow https://www.python.org/ to install Python or set 'use-pyodide' to true.",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shouldn't be an error, because we should automatically call use-pyodide if that is the case. The only way an error like this would occur is if someone explicitly set use-pyodide=false without having python. In that case, I think the error name would be better being something like pyodide-flag-conflict or something like that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense and I think pyodide-flag-conflict is reasonable here.
Updated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you make invalid-models-mode and potentially pyodide-flag-conflict have severity warning instead of error? We can continue generating in these situations, we just have to fallback. We really should only throw errors if there is no way for us to continue. For invalid-models-mode, we can just generat without models. For pyodide-flag-conflict, one option is to just generate with pyodide, but IU'm also not sure that's the best solution since the user explicitly said use-pyodide=false. what do you guys think?

},
},
// warning
"no-valid-client": {
severity: "warning",
messages: {
Expand Down