Skip to content

Commit f790444

Browse files
authored
[ty] Fix playground crash when file name includes path separator (#21151)
1 parent 47e41ac commit f790444

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

playground/ty/src/Editor/SecondaryPanel.tsx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,17 @@ function Content({
103103
}
104104
}
105105

106+
const SANDBOX_BASE_DIRECTORY = "/playground/";
107+
106108
function Run({ files, theme }: { files: ReadonlyFiles; theme: Theme }) {
107109
const [runOutput, setRunOutput] = useState<Promise<string> | null>(null);
108110
const handleRun = () => {
109111
const output = (async () => {
110-
const pyodide = await loadPyodide();
112+
const pyodide = await loadPyodide({
113+
env: {
114+
HOME: SANDBOX_BASE_DIRECTORY,
115+
},
116+
});
111117

112118
let combined_output = "";
113119

@@ -122,7 +128,17 @@ function Run({ files, theme }: { files: ReadonlyFiles; theme: Theme }) {
122128

123129
let fileName = "main.py";
124130
for (const file of files.index) {
125-
pyodide.FS.writeFile(file.name, files.contents[file.id]);
131+
const last_separator = file.name.lastIndexOf("/");
132+
133+
if (last_separator !== -1) {
134+
const directory =
135+
SANDBOX_BASE_DIRECTORY + file.name.slice(0, last_separator);
136+
pyodide.FS.mkdirTree(directory);
137+
}
138+
pyodide.FS.writeFile(
139+
SANDBOX_BASE_DIRECTORY + file.name,
140+
files.contents[file.id],
141+
);
126142

127143
if (file.id === files.selected) {
128144
fileName = file.name;
@@ -133,7 +149,7 @@ function Run({ files, theme }: { files: ReadonlyFiles; theme: Theme }) {
133149
const globals = dict();
134150

135151
try {
136-
// Patch up reveal types
152+
// Patch `reveal_type` to print runtime values
137153
pyodide.runPython(`
138154
import builtins
139155

0 commit comments

Comments
 (0)