Skip to content

A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.

improvement: fallback to --no-cache for uv installations that fail (#7256)

Myles Scolnick7c82a2fe6dbd
diff --git a/marimo/_runtime/packages/package_manager.py b/marimo/_runtime/packages/package_manager.py
index d453910add..665681c177 100644
--- a/marimo/_runtime/packages/package_manager.py
+++ b/marimo/_runtime/packages/package_manager.py
@@ -131,7 +131,7 @@ class PackageManager(abc.ABC):
return completed_process.returncode == 0
# Stream output to both the callback and the terminal
- proc = subprocess.Popen(
+ proc = subprocess.Popen( # noqa: ASYNC220
command,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
diff --git a/marimo/_runtime/packages/pypi_package_manager.py b/marimo/_runtime/packages/pypi_package_manager.py
index 3a611105b4..7cd5c7a378 100644
--- a/marimo/_runtime/packages/pypi_package_manager.py
+++ b/marimo/_runtime/packages/pypi_package_manager.py
@@ -162,6 +162,18 @@ class UvPackageManager(PypiPackageManager):
def _uv_bin(self) -> str:
return find_uv_bin()
+ def _is_cache_write_error(self, output_text: str) -> bool:
+ """Check if the output text indicates a cache write error.
+
+ This is somewhat fragile and could break with new uv output.
+ This was tested with uv ~0.9.7
+ """
+ output_text = output_text.lower()
+ return (
+ "failed to write to the distribution cache" in output_text
+ or "operation not permitted" in output_text
+ )
+
def is_manager_installed(self) -> bool:
return self._uv_bin != "uv" or super().is_manager_installed()
@@ -195,16 +207,68 @@ class UvPackageManager(PypiPackageManager):
upgrade: bool,
log_callback: Optional[LogCallback] = None,
) -> bool:
- """Installation logic."""
+ """Installation logic with fallback to --no-cache on cache write errors."""
LOGGER.info(
f"Installing in {package} with 'uv {'add' if self.is_in_uv_project else 'pip install'}'"
)
- return await super()._install(
- package,
- upgrade=upgrade,
- log_callback=log_callback,
+
+ # For uv projects, use the standard install flow without fallback
+ if self.is_in_uv_project:
+ return await super()._install(
+ package,
+ upgrade=upgrade,
+ log_callback=log_callback,
+ )
+
+ # For uv pip install, try with output capture to enable fallback
+ cmd = self.install_command(package, upgrade=upgrade)
+
+ # Run the command and capture output
+ proc = subprocess.Popen( # noqa: ASYNC220
+ cmd,
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ universal_newlines=False,
+ bufsize=0,
)
+ output_lines: list[str] = []
+ if proc.stdout:
+ for line in iter(proc.stdout.readline, b""):
+ # Send to terminal
+ sys.stdout.buffer.write(line)
+ sys.stdout.buffer.flush()
+ decoded_line = line.decode("utf-8", errors="replace")
+ # Send to callback for streaming
+ if log_callback:
+ log_callback(decoded_line)
+ # Store for error checking
+ output_lines.append(decoded_line)
+ proc.stdout.close()
+
+ return_code = proc.wait()
+
+ # If successful, we're done
+ if return_code == 0:
+ return True
+
+ # Check if we should retry with --no-cache
+ output_text = "".join(output_lines)
+ if self._is_cache_write_error(output_text):
+ LOGGER.info(
+ f"Retrying installation of {package} with --no-cache due to cache write error"
+ )
+ if log_callback:
+ log_callback(
+ "\nRetrying with --no-cache due to cache write permission error...\n"
+ )
+
+ # Retry with --no-cache flag
+ cmd_with_no_cache = cmd + ["--no-cache"]
+ return self.run(cmd_with_no_cache, log_callback=log_callback)
+
+ return False
+
def update_notebook_script_metadata(
self,
filepath: str,
diff --git a/tests/_runtime/packages/test_pypi_package_manager.py b/tests/_runtime/packages/test_pypi_package_manager.py
index d1ab1543fc..e9e31af641 100644
--- a/tests/_runtime/packages/test_pypi_package_manager.py
+++ b/tests/_runtime/packages/test_pypi_package_manager.py
@@ -1,6 +1,7 @@
from __future__ import annotations
import json
+import subprocess
import sys
from functools import partial
from typing import TYPE_CHECKING
@@ -285,16 +286,19 @@ def test_uv_is_in_uv_project_cached(mock_exists: MagicMock):
assert mock_exists.call_count == 2
-@patch("subprocess.run")
+@patch("subprocess.Popen")
@patch.object(UvPackageManager, "is_in_uv_project", False)
-async def test_uv_install_not_in_project(mock_run: MagicMock):
+async def test_uv_install_not_in_project(mock_popen: MagicMock):
"""Test UV install uses pip subcommand when not in UV project"""
- mock_run.return_value = MagicMock(returncode=0)
+ mock_process = MagicMock()
+ mock_process.wait.return_value = 0
+ mock_process.stdout.readline.return_value = b""
+ mock_popen.return_value = mock_process
mgr = UvPackageManager()
result = await mgr._install("package1 package2", upgrade=False)
- mock_run.assert_called_once_with(
+ mock_popen.assert_called_once_with(
[
"uv",
"pip",
@@ -305,15 +309,22 @@ async def test_uv_install_not_in_project(mock_run: MagicMock):
"-p",
PY_EXE,
],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ universal_newlines=False,
+ bufsize=0,
)
assert result is True
-@patch("subprocess.run")
+@patch("subprocess.Popen")
@patch.object(UvPackageManager, "is_in_uv_project", False)
-async def test_uv_install_not_in_project_with_target(mock_run: MagicMock):
+async def test_uv_install_not_in_project_with_target(mock_popen: MagicMock):
"""Test UV install uses pip with target"""
- mock_run.return_value = MagicMock(returncode=0)
+ mock_process = MagicMock()
+ mock_process.wait.return_value = 0
+ mock_process.stdout.readline.return_value = b""
+ mock_popen.return_value = mock_process
mgr = UvPackageManager()
# Explicitly set environ, since patch doesn't work in an asynchronous
@@ -324,7 +335,7 @@ async def test_uv_install_not_in_project_with_target(mock_run: MagicMock):
result = await mgr._install("package1 package2", upgrade=False)
del os.environ["MARIMO_UV_TARGET"]
- mock_run.assert_called_once_with(
+ mock_popen.assert_called_once_with(
[
"uv",
"pip",
@@ -336,6 +347,10 @@ async def test_uv_install_not_in_project_with_target(mock_run: MagicMock):
"-p",
PY_EXE,
],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ universal_newlines=False,
+ bufsize=0,
)
assert result is True
@@ -665,3 +680,108 @@ def test_has_script_metadata_binary_file(tmp_path: Path):
mgr = UvPackageManager()
assert mgr._has_script_metadata(str(binary_file)) is False
+
+
+@patch("subprocess.Popen")
+@patch("subprocess.run")
[email protected](UvPackageManager, "is_in_uv_project", False)
+async def test_uv_install_cache_error_fallback(
+ mock_run: MagicMock, mock_popen: MagicMock
+):
+ """Test UV install retries with --no-cache on cache write errors"""
+ # Mock the first install attempt (via Popen) to fail with cache error
+ mock_process = MagicMock()
+ mock_process.wait.return_value = 1 # Failure
+ mock_process.stdout.readline.side_effect = [
+ b" \xc3\x97 Failed to download and build `pyqtree==1.0.0`\n",
+ b" \xe2\x94\x9c\xe2\x94\x80\xe2\x96\xb6 Failed to write to the distribution cache\n",
+ b" \xe2\x95\xb0\xe2\x94\x80\xe2\x96\xb6 Operation not permitted (os error 1)\n",
+ b"", # End of output
+ ]
+ mock_popen.return_value = mock_process
+
+ # Mock the retry (via run) to succeed
+ mock_run.return_value = MagicMock(returncode=0)
+
+ mgr = UvPackageManager()
+ with patch.object(mgr, "is_manager_installed", return_value=True):
+ result = await mgr._install("datamapplot", upgrade=False)
+
+ # First attempt should use Popen
+ mock_popen.assert_called_once_with(
+ [
+ "uv",
+ "pip",
+ "install",
+ "--compile",
+ "datamapplot",
+ "-p",
+ PY_EXE,
+ ],
+ stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT,
+ universal_newlines=False,
+ bufsize=0,
+ )
+
+ # Retry should add --no-cache flag
+ mock_run.assert_called_once_with(
+ [
+ "uv",
+ "pip",
+ "install",
+ "--compile",
+ "datamapplot",
+ "-p",
+ PY_EXE,
+ "--no-cache",
+ ],
+ )
+
+ # Should ultimately succeed
+ assert result is True
+
+
+@patch("subprocess.Popen")
[email protected](UvPackageManager, "is_in_uv_project", False)
+async def test_uv_install_no_fallback_on_different_error(
+ mock_popen: MagicMock,
+):
+ """Test UV install does not retry when error is not cache-related"""
+ # Mock the install attempt to fail with a different error
+ mock_process = MagicMock()
+ mock_process.wait.return_value = 1 # Failure
+ mock_process.stdout.readline.side_effect = [
+ b" \xc3\x97 Failed to download package\n",
+ b" \xe2\x94\x9c\xe2\x94\x80\xe2\x96\xb6 Network error\n",
+ b"", # End of output
+ ]
+ mock_popen.return_value = mock_process
+
+ mgr = UvPackageManager()
+ with patch.object(mgr, "is_manager_installed", return_value=True):
+ result = await mgr._install("nonexistent-package", upgrade=False)
+
+ # Should only call Popen once (no retry)
+ mock_popen.assert_called_once()
+
+ # Should fail
+ assert result is False
+
+
+@patch("subprocess.run")
[email protected](UvPackageManager, "is_in_uv_project", True)
+async def test_uv_install_in_project_no_fallback(mock_run: MagicMock):
+ """Test UV install in a project does not use fallback mechanism"""
+ mock_run.return_value = MagicMock(returncode=1) # Failure
+ mgr = UvPackageManager()
+
+ result = await mgr._install("package1", upgrade=False)
+
+ # Should only call run once (no fallback for project mode)
+ mock_run.assert_called_once_with(
+ ["uv", "add", "--compile", "package1", "-p", PY_EXE],
+ )
+
+ # Should fail without retry
+ assert result is False