From 9d1fb43e5773a98c26a9ebcb2ef10f040e48ac6a Mon Sep 17 00:00:00 2001 From: tomas Date: Wed, 18 Nov 2026 11:38:27 +0000 Subject: [PATCH] fix(kernel): keep local imports and DEEPNOTE_PROJECT_ID working outside Deepnote Cloud runtime-core, which the VS Code extension and the deepnote CLI use, starts the toolkit in detached mode, with Jupyter and the kernel in the .deepnote file's folder. set_notebook_path still rewrote sys.path and the working directory against ~/work there. That removed the notebook's folder from sys.path, so a module next to the .deepnote file could not be imported. Imports only worked when parsing the project ID from the session name crashed first; a notebook name with two colons parsed into a garbage ID and broke them again. In detached mode, outside dev mode, and with neither paths.notebook_root nor paths.home_dir set, the kernel now keeps the working directory and sys.path it starts with. In detached or dev mode, runtime.project_id is exported as DEEPNOTE_PROJECT_ID before the Jupyter session lookup, so user code sees the same variable as in Cloud. In the detached case above, the session name is no longer parsed. Attached runs, dev mode and configs with explicit paths keep the rewrite. Closes #153 --- deepnote_toolkit/set_notebook_path.py | 36 ++++++-- docs/user/configuration.md | 4 +- tests/unit/test_set_notebook_path.py | 4 +- tests/unit/test_set_notebook_path_config.py | 91 +++++++++++++++++++++ 4 files changed, 123 insertions(+), 12 deletions(-) diff --git a/deepnote_toolkit/set_notebook_path.py b/deepnote_toolkit/set_notebook_path.py index d3f9ab89..2d80db84 100644 --- a/deepnote_toolkit/set_notebook_path.py +++ b/deepnote_toolkit/set_notebook_path.py @@ -15,12 +15,36 @@ def set_notebook_path() -> None: """Set the notebook path and working directory based on the Jupyter session. This function: - - Determines the notebook root directory from config or environment - - Finds the current notebook's path using the Jupyter API + - Sets DEEPNOTE_PROJECT_ID from runtime.project_id in detached or dev mode + - Returns early in detached mode outside dev mode when neither + paths.notebook_root nor paths.home_dir is set + - Otherwise determines the notebook root directory from config or environment + - Finds the current notebook's path using the Jupyter API; in detached or + dev mode, parses DEEPNOTE_PROJECT_ID from the session name if still unset - Updates sys.path to include the notebook's directory - Changes the working directory to the notebook's directory - - Sets DEEPNOTE_PROJECT_ID in detached mode """ + try: + cfg = get_config() + rt = cfg.runtime + _detached = bool(rt.running_in_detached_mode or rt.dev_mode) + project_id = rt.project_id + # runtime-core starts Jupyter and the kernel in the directory it was given: + # the .deepnote file's folder, or the deepnote CLI's --cwd + _keep_kernel_cwd = ( + rt.running_in_detached_mode + and not rt.dev_mode + and not cfg.paths.notebook_root + and not cfg.paths.home_dir + ) + except Exception: + _detached, _keep_kernel_cwd, project_id = False, False, None + + if _detached and project_id: + env.set_env("DEEPNOTE_PROJECT_ID", project_id) + if _keep_kernel_cwd: + return + root_path = resolve_notebook_root() try: @@ -48,14 +72,8 @@ def set_notebook_path() -> None: if session["kernel"]["id"] == kernel_id ) - # If not provided as an environmental variable, use projectId parsed from session name # ProjectID is used for integration functionality # Expected session name format: "{schema_version}:{type}:{project_id}:{...additional segments}" - try: - rt = get_config().runtime - _detached = bool(rt.running_in_detached_mode or rt.dev_mode) - except Exception: - _detached = False if _detached and not env.has_env("DEEPNOTE_PROJECT_ID"): env.set_env("DEEPNOTE_PROJECT_ID", session["name"].split(":")[2]) diff --git a/docs/user/configuration.md b/docs/user/configuration.md index 933abd02..1500a24c 100644 --- a/docs/user/configuration.md +++ b/docs/user/configuration.md @@ -138,11 +138,11 @@ At runtime, the Toolkit automatically loads `DEEPNOTE_CONFIG_FILE` if present. - `installation.index_url`: base index URL for bundles - `installation.bundle_path`: local bundle dir - `installation.cache_path`: cache dir -- `runtime.running_in_detached_mode`: detached/direct mode toggle +- `runtime.running_in_detached_mode`: detached/direct mode toggle. Outside dev mode and without `paths.notebook_root` or `paths.home_dir`, kernels keep the working directory and `sys.path` they start with - `runtime.dev_mode`: local dev toggles (e.g., default notebook root `/work`) - `runtime.ci`: CI toggle for logging behavior - `runtime.webapp_url`: base webapp URL (used in detached/dev mode) -- `runtime.project_id`: runtime UUID/project identifier +- `runtime.project_id`: runtime UUID/project identifier; in detached or dev mode, kernels see it as `DEEPNOTE_PROJECT_ID` - `runtime.coerce_float`: controls pandas `read_sql_query(coerce_float=...)` - `runtime.env_integration_enabled`: enables variable injection from integrations diff --git a/tests/unit/test_set_notebook_path.py b/tests/unit/test_set_notebook_path.py index 26390f5a..53eb600a 100644 --- a/tests/unit/test_set_notebook_path.py +++ b/tests/unit/test_set_notebook_path.py @@ -15,7 +15,9 @@ def _cfg(jupyter_port=9999, notebook_root: str = "/tmp", detached=True, dev=Fals return types.SimpleNamespace( server=types.SimpleNamespace(jupyter_port=jupyter_port), paths=types.SimpleNamespace(notebook_root=notebook_root, home_dir=None), - runtime=types.SimpleNamespace(running_in_detached_mode=detached, dev_mode=dev), + runtime=types.SimpleNamespace( + running_in_detached_mode=detached, dev_mode=dev, project_id=None + ), ) diff --git a/tests/unit/test_set_notebook_path_config.py b/tests/unit/test_set_notebook_path_config.py index 97686437..7b245ba1 100644 --- a/tests/unit/test_set_notebook_path_config.py +++ b/tests/unit/test_set_notebook_path_config.py @@ -1,6 +1,97 @@ import json +import os +import sys +import types from unittest import mock +import pytest + +from deepnote_toolkit import env as dnenv +from deepnote_toolkit.set_notebook_path import set_notebook_path + + +@pytest.fixture +def kernel(tmp_path, monkeypatch): + """Kernel whose Jupyter session opens ``folder/notebook.ipynb``.""" + monkeypatch.setenv("HOME", str(tmp_path)) + monkeypatch.delenv("DEEPNOTE_PROJECT_ID", raising=False) + monkeypatch.setattr( + "ipykernel.connect.get_connection_file", + lambda: str(tmp_path / "kernel-1234.json"), + ) + session = {"kernel": {"id": "1234"}, "path": "folder/notebook.ipynb", "name": ""} + monkeypatch.setattr( + "requests.get", + lambda url, headers: mock.MagicMock(text=json.dumps([session])), + ) + chdirs = [] + monkeypatch.setattr("os.chdir", chdirs.append) + monkeypatch.setattr(sys, "path", ["", "/opt/site-packages"]) + + yield types.SimpleNamespace(home=tmp_path, session=session, chdirs=chdirs) + + # set_notebook_path exports to os.environ, which monkeypatch does not restore + dnenv.unset_env("DEEPNOTE_PROJECT_ID") + + +def test_detached_without_paths_keeps_kernel_cwd_and_exports_project_id( + kernel, monkeypatch +): + monkeypatch.setenv("DEEPNOTE_RUNTIME__RUNNING_IN_DETACHED_MODE", "true") + monkeypatch.setenv("DEEPNOTE_RUNTIME__PROJECT_ID", "proj-1") + kernel.session["name"] = "report-1234.deepnote" + + set_notebook_path() + + assert kernel.chdirs == [] + assert sys.path == ["", "/opt/site-packages"] + assert os.environ.get("DEEPNOTE_PROJECT_ID") == "proj-1" + + +def test_detached_without_paths_or_project_id_ignores_session_name(kernel, monkeypatch): + monkeypatch.setenv("DEEPNOTE_RUNTIME__RUNNING_IN_DETACHED_MODE", "true") + monkeypatch.delenv("DEEPNOTE_RUNTIME__PROJECT_ID", raising=False) + kernel.session["name"] = "sales:q1:report-1234.deepnote" + + set_notebook_path() + + assert kernel.chdirs == [] + assert sys.path == ["", "/opt/site-packages"] + assert os.environ.get("DEEPNOTE_PROJECT_ID") is None + + +@pytest.mark.parametrize( + "detached, dev_mode, notebook_dir", + [ + ("false", "false", "{home}/work/folder"), + ("false", "true", "/work/folder"), + ("true", "true", "/work/folder"), + ], + ids=["attached", "dev-mode", "detached-dev-mode"], +) +def test_rewrites_sys_path_and_cwd_when_attached_or_in_dev_mode( + kernel, monkeypatch, detached, dev_mode, notebook_dir +): + monkeypatch.setenv("DEEPNOTE_RUNTIME__RUNNING_IN_DETACHED_MODE", detached) + monkeypatch.setenv("DEEPNOTE_RUNTIME__DEV_MODE", dev_mode) + notebook_dir = notebook_dir.format(home=kernel.home) + kernel.session["name"] = "1:type:proj-xyz:rest" + + set_notebook_path() + + assert kernel.chdirs == [notebook_dir] + assert sys.path == ["/opt/site-packages", notebook_dir] + + +def test_dev_mode_exports_configured_project_id_over_session_name(kernel, monkeypatch): + monkeypatch.setenv("DEEPNOTE_RUNTIME__DEV_MODE", "true") + monkeypatch.setenv("DEEPNOTE_RUNTIME__PROJECT_ID", "proj-1") + kernel.session["name"] = "1:type:other-id:rest" + + set_notebook_path() + + assert os.environ.get("DEEPNOTE_PROJECT_ID") == "proj-1" + def test_set_notebook_path_uses_config_home_and_port(tmp_path, monkeypatch): # Prepare config: home_dir and jupyter_port, detached mode true