From 8c5dd121d500c1743fe291144858e8039c0c7414 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 1 Sep 2026 14:10:31 -0400 Subject: [PATCH 1/2] Fix Python playground bundle esbuild failure by isolating browser-safe YAML util emitter.ts imports dumpCodeModelToYaml from external-process.ts for use on the browser/Pyodide path, but external-process.ts also contains Node-only code (execAsync, saveCodeModelAsYaml) using child_process/crypto/fs/os. Since emitter.ts isn't swapped for a browser stub (only node-runner.js is), esbuild bundled the whole file with platform: browser, failing to resolve those Node builtins during the playground bundle upload step. Move dumpCodeModelToYaml into a new browser-safe yaml-utils.ts module and have both emitter.ts and external-process.ts import it from there. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- .../http-client-python/emitter/src/emitter.ts | 2 +- .../emitter/src/external-process.ts | 19 +--------------- .../emitter/src/yaml-utils.ts | 22 +++++++++++++++++++ ...nal-process.test.ts => yaml-utils.test.ts} | 4 ++-- 4 files changed, 26 insertions(+), 21 deletions(-) create mode 100644 packages/http-client-python/emitter/src/yaml-utils.ts rename packages/http-client-python/emitter/test/{external-process.test.ts => yaml-utils.test.ts} (91%) diff --git a/packages/http-client-python/emitter/src/emitter.ts b/packages/http-client-python/emitter/src/emitter.ts index bc7fc5d68ab..ab9bd654a08 100644 --- a/packages/http-client-python/emitter/src/emitter.ts +++ b/packages/http-client-python/emitter/src/emitter.ts @@ -9,13 +9,13 @@ import { PYGEN_WHEEL_FILENAME, PYODIDE_VERSION, } from "./constants.js"; -import { dumpCodeModelToYaml } from "./external-process.js"; import type { PythonEmitterOptions, PythonSdkContext } from "./lib.js"; import { reportDiagnostic } from "./lib.js"; import { runNodeEmit } from "./node-runner.js"; import type { PyodideInterface } from "./pyodide-loader.js"; import { loadPyodide } from "./pyodide-loader.js"; import { getRootNamespace, md2Rst } from "./utils.js"; +import { dumpCodeModelToYaml } from "./yaml-utils.js"; function getBrowserPygenWheelUrl(): string { return `${BLOB_STORAGE_BASE_URL}/${PACKAGE_NAME}/${pkgJson.version}/generator/dist/${PYGEN_WHEEL_FILENAME}`; diff --git a/packages/http-client-python/emitter/src/external-process.ts b/packages/http-client-python/emitter/src/external-process.ts index 74f0b3b2cbf..9f3504b9f89 100644 --- a/packages/http-client-python/emitter/src/external-process.ts +++ b/packages/http-client-python/emitter/src/external-process.ts @@ -3,8 +3,8 @@ import type { ChildProcess, SpawnOptions } from "child_process"; import { spawn } from "child_process"; import { randomUUID } from "crypto"; import { mkdir, writeFile } from "fs/promises"; -import jsyaml from "js-yaml"; import os from "os"; +import { dumpCodeModelToYaml } from "./yaml-utils.js"; const tspCodeGenTempDir = joinPaths(os.tmpdir(), "tsp-codegen"); @@ -12,23 +12,6 @@ export function createTempPath(extension: string, prefix: string = "") { return joinPaths(tspCodeGenTempDir, prefix + randomUUID() + extension); } -/** - * Serialize the given codemodel to a YAML string. - * - * The generated YAML is consumed by the Python generator, which parses it with - * PyYAML (YAML 1.1). js-yaml, on the other hand, dumps using YAML 1.2 rules, so - * plain scalars such as `2020_01_01` (a snake-cased enum member name) are left - * unquoted because YAML 1.2 does not treat underscores as integer separators. - * PyYAML would then read `2020_01_01` back as the integer `20200101`, corrupting - * string values (e.g. enum member names, descriptions). Forcing every string - * scalar to be quoted guarantees that PyYAML round-trips them as strings. - * @param codemodel Codemodel to serialize - * @return the YAML representation of the codemodel. - */ -export function dumpCodeModelToYaml(codemodel: unknown): string { - return jsyaml.dump(codemodel, { forceQuotes: true, quotingType: '"' }); -} - /** * Save the given codemodel in a yaml file. * @param name Name of the codemodel. To give a guide to the temp file name. diff --git a/packages/http-client-python/emitter/src/yaml-utils.ts b/packages/http-client-python/emitter/src/yaml-utils.ts new file mode 100644 index 00000000000..4508a2ffa89 --- /dev/null +++ b/packages/http-client-python/emitter/src/yaml-utils.ts @@ -0,0 +1,22 @@ +import jsyaml from "js-yaml"; + +/** + * Serialize the given codemodel to a YAML string. + * + * The generated YAML is consumed by the Python generator, which parses it with + * PyYAML (YAML 1.1). js-yaml, on the other hand, dumps using YAML 1.2 rules, so + * plain scalars such as `2020_01_01` (a snake-cased enum member name) are left + * unquoted because YAML 1.2 does not treat underscores as integer separators. + * PyYAML would then read `2020_01_01` back as the integer `20200101`, corrupting + * string values (e.g. enum member names, descriptions). Forcing every string + * scalar to be quoted guarantees that PyYAML round-trips them as strings. + * + * This module must remain browser-safe (no Node built-ins): `emitter.ts` calls + * this function on the browser/Pyodide path, and esbuild bundles it into the + * playground bundle with `platform: "browser"`. + * @param codemodel Codemodel to serialize + * @return the YAML representation of the codemodel. + */ +export function dumpCodeModelToYaml(codemodel: unknown): string { + return jsyaml.dump(codemodel, { forceQuotes: true, quotingType: '"' }); +} diff --git a/packages/http-client-python/emitter/test/external-process.test.ts b/packages/http-client-python/emitter/test/yaml-utils.test.ts similarity index 91% rename from packages/http-client-python/emitter/test/external-process.test.ts rename to packages/http-client-python/emitter/test/yaml-utils.test.ts index fd6a77135c3..95ef96a7c3f 100644 --- a/packages/http-client-python/emitter/test/external-process.test.ts +++ b/packages/http-client-python/emitter/test/yaml-utils.test.ts @@ -1,9 +1,9 @@ import { ok, strictEqual } from "assert"; import { load } from "js-yaml"; import { describe, it } from "vitest"; -import { dumpCodeModelToYaml } from "../src/external-process.js"; +import { dumpCodeModelToYaml } from "../src/yaml-utils.js"; -describe("typespec-python: external-process", () => { +describe("typespec-python: yaml-utils", () => { // The Python generator parses the emitted YAML with PyYAML (YAML 1.1), where a plain // scalar such as `2020_01_01` is interpreted as the integer `20200101`. js-yaml dumps // using YAML 1.2 rules and would otherwise leave such string scalars unquoted, so we From c1bdc52227cafbdb9a88b9fe2d4b698607e6f07b Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 1 Sep 2026 14:15:16 -0400 Subject: [PATCH 2/2] Add changeset for Python playground bundle esbuild fix Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- ...ix-python-playground-bundle-esbuild-2026-9-1-14-15-1.md | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .chronus/changes/fix-python-playground-bundle-esbuild-2026-9-1-14-15-1.md diff --git a/.chronus/changes/fix-python-playground-bundle-esbuild-2026-9-1-14-15-1.md b/.chronus/changes/fix-python-playground-bundle-esbuild-2026-9-1-14-15-1.md new file mode 100644 index 00000000000..a0a7a0d7628 --- /dev/null +++ b/.chronus/changes/fix-python-playground-bundle-esbuild-2026-9-1-14-15-1.md @@ -0,0 +1,7 @@ +--- +changeKind: fix +packages: + - "@typespec/http-client-python" +--- + +Fix playground bundle publish failing with esbuild errors ("Could not resolve child_process/crypto/fs/promises/os") by moving the browser-safe YAML serialization helper out of a module that also contains Node-only code