[ty] Fix playground crash when file name includes path separator (#21151)

This commit is contained in:
David Peter 2025-11-04 18:36:36 +01:00 committed by GitHub
parent 47e41ac6b6
commit f79044478c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 19 additions and 3 deletions

View File

@ -103,11 +103,17 @@ function Content({
}
}
const SANDBOX_BASE_DIRECTORY = "/playground/";
function Run({ files, theme }: { files: ReadonlyFiles; theme: Theme }) {
const [runOutput, setRunOutput] = useState<Promise<string> | 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