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
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion packages/http-client-python/emitter/src/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down
19 changes: 1 addition & 18 deletions packages/http-client-python/emitter/src/external-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,15 @@ 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");

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.
Expand Down
22 changes: 22 additions & 0 deletions packages/http-client-python/emitter/src/yaml-utils.ts
Original file line number Diff line number Diff line change
@@ -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: '"' });
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading