From f79044478c9999c58707f716d220767505bcf2d4 Mon Sep 17 00:00:00 2001 From: David Peter Date: Tue, 4 Nov 2025 18:36:36 +0100 Subject: [PATCH] [ty] Fix playground crash when file name includes path separator (#21151) --- playground/ty/src/Editor/SecondaryPanel.tsx | 22 ++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/playground/ty/src/Editor/SecondaryPanel.tsx b/playground/ty/src/Editor/SecondaryPanel.tsx index a73cb57961..6c8cd0189a 100644 --- a/playground/ty/src/Editor/SecondaryPanel.tsx +++ b/playground/ty/src/Editor/SecondaryPanel.tsx @@ -103,11 +103,17 @@ function Content({ } } +const SANDBOX_BASE_DIRECTORY = "/playground/"; + function Run({ files, theme }: { files: ReadonlyFiles; theme: Theme }) { const [runOutput, setRunOutput] = useState | null>(null); const handleRun = () => { const output = (async () => { - const pyodide = await loadPyodide(); + const pyodide = await loadPyodide({ + env: { + HOME: SANDBOX_BASE_DIRECTORY, + }, + }); let combined_output = ""; @@ -122,7 +128,17 @@ function Run({ files, theme }: { files: ReadonlyFiles; theme: Theme }) { let fileName = "main.py"; for (const file of files.index) { - pyodide.FS.writeFile(file.name, files.contents[file.id]); + const last_separator = file.name.lastIndexOf("/"); + + if (last_separator !== -1) { + const directory = + SANDBOX_BASE_DIRECTORY + file.name.slice(0, last_separator); + pyodide.FS.mkdirTree(directory); + } + pyodide.FS.writeFile( + SANDBOX_BASE_DIRECTORY + file.name, + files.contents[file.id], + ); if (file.id === files.selected) { fileName = file.name; @@ -133,7 +149,7 @@ function Run({ files, theme }: { files: ReadonlyFiles; theme: Theme }) { const globals = dict(); try { - // Patch up reveal types + // Patch `reveal_type` to print runtime values pyodide.runPython(` import builtins