Skip to content

Commit 21c5c24

Browse files
authored
Fix %run magic with databricks notebooks on windows (#1628)
## Changes On windows `tempfile.NamedTemporaryFile` with `delete=True` (default) deletes the temp file when it's closed. Internally the `%run` logic opens and closes the file before execution (to check that it can open it), which leads to deletion of the file and an error seen in #1624 Here we use lower-level temp file logic and unlink it ourselves after the execution is fully done. One more change is an installation of `nbformat` dependency at runtime (together with `databricks-connect`), which is required for the `%run` to work. ## Tests e2e
1 parent 60d5def commit 21c5c24

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

packages/databricks-vscode/resources/python/00-databricks-init.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,14 @@ def databricks_notebook_exec_env(project_root: str, py_file: str):
221221
try:
222222
if is_databricks_notebook(py_file):
223223
notebook = convert_databricks_notebook_to_ipynb(py_file)
224-
with tempfile.NamedTemporaryFile(suffix=".ipynb") as f:
225-
f.write(notebook.encode())
226-
f.flush()
227-
yield f.name
224+
fd, temp_path = tempfile.mkstemp(suffix=".ipynb")
225+
with os.fdopen(fd, 'wb') as temp_file:
226+
temp_file.write(notebook.encode())
227+
try:
228+
yield temp_path
229+
finally:
230+
if os.path.exists(temp_path):
231+
os.unlink(temp_path)
228232
else:
229233
yield py_file
230234
finally:

packages/databricks-vscode/src/language/EnvironmentDependenciesInstaller.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ export class EnvironmentDependenciesInstaller implements Disposable {
5050
version,
5151
this.outputChannel
5252
);
53+
// Required for executing notebooks with %run magic
54+
await this.pythonExtension.installPackageInEnvironment(
55+
"nbformat",
56+
undefined,
57+
this.outputChannel
58+
);
5359
} catch (e: unknown) {
5460
if (e instanceof Error) {
5561
window.showErrorMessage(e.message);

packages/databricks-vscode/src/test/e2e/run_dbconnect.ucws.e2e.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,20 @@ describe("Run files on serverless compute", async function () {
100100
})
101101
);
102102

103+
await fs.writeFile(
104+
path.join(nestedDir, "databricks-run-notebook.py"),
105+
[
106+
"# Databricks notebook source",
107+
"# DBTITLE 1,My cell title",
108+
"# MAGIC %sql",
109+
"# MAGIC select 1 + 1;",
110+
"# MAGIC select 'hello run;'",
111+
"# COMMAND ----------",
112+
`df = _sqldf.toPandas()`,
113+
`df.to_json(os.path.join(os.getcwd(), "databricks-run-notebook-output.json"))`,
114+
].join("\n")
115+
);
116+
103117
await fs.writeFile(
104118
path.join(nestedDir, "databricks-notebook.py"),
105119
[
@@ -114,7 +128,7 @@ describe("Run files on serverless compute", async function () {
114128
`df = _sqldf.toPandas()`,
115129
`df.to_json(os.path.join(os.getcwd(), "databricks-notebook-output.json"))`,
116130
"# COMMAND ----------",
117-
"# MAGIC %run './hello.py'",
131+
"# MAGIC %run './databricks-run-notebook.py'",
118132
].join("\n")
119133
);
120134

@@ -288,8 +302,8 @@ describe("Run files on serverless compute", async function () {
288302
const runOutputFile = path.join(
289303
projectDir,
290304
"nested",
291-
"file-output.json"
305+
"databricks-run-notebook-output.json"
292306
);
293-
await checkOutputFile(runOutputFile, "hello world");
307+
await checkOutputFile(runOutputFile, "hello run;");
294308
});
295309
});

0 commit comments

Comments
 (0)