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.

fix: resolve relateive paths in uv.sources (#4894)

Myles Scolnick26ac4c577383
diff --git a/marimo/_cli/sandbox.py b/marimo/_cli/sandbox.py
index efd673b305..ccf1e8318a 100644
--- a/marimo/_cli/sandbox.py
+++ b/marimo/_cli/sandbox.py
@@ -210,7 +210,7 @@ def construct_uv_command(
pyproject = (
PyProjectReader.from_filename(name)
if name is not None
- else PyProjectReader({})
+ else PyProjectReader({}, config_path=None)
)
uv_cmd = ["uv", "run"]
diff --git a/marimo/_utils/inline_script_metadata.py b/marimo/_utils/inline_script_metadata.py
index f979fa5958..ebb025f104 100644
--- a/marimo/_utils/inline_script_metadata.py
+++ b/marimo/_utils/inline_script_metadata.py
@@ -2,6 +2,7 @@
from __future__ import annotations
import re
+from pathlib import Path
from typing import Any, cast
from marimo import _loggers
@@ -12,16 +13,21 @@ LOGGER = _loggers.marimo_logger()
class PyProjectReader:
- def __init__(self, project: dict[str, Any]):
+ def __init__(self, project: dict[str, Any], *, config_path: str | None):
self.project = project
+ self.config_path = config_path
@staticmethod
def from_filename(name: str) -> PyProjectReader:
- return PyProjectReader(_get_pyproject_from_filename(name) or {})
+ return PyProjectReader(
+ _get_pyproject_from_filename(name) or {}, config_path=name
+ )
@staticmethod
def from_script(script: str) -> PyProjectReader:
- return PyProjectReader(read_pyproject_from_script(script) or {})
+ return PyProjectReader(
+ read_pyproject_from_script(script) or {}, config_path=None
+ )
@property
def extra_index_urls(self) -> list[str]:
@@ -64,7 +70,9 @@ class PyProjectReader:
def requirements_txt_lines(self) -> list[str]:
"""Get dependencies from string representation of script."""
try:
- return _pyproject_toml_to_requirements_txt(self.project)
+ return _pyproject_toml_to_requirements_txt(
+ self.project, self.config_path
+ )
except Exception as e:
LOGGER.warning(f"Failed to parse dependencies: {e}")
return []
@@ -102,6 +110,7 @@ def _get_pyproject_from_filename(name: str) -> dict[str, Any] | None:
def _pyproject_toml_to_requirements_txt(
pyproject: dict[str, Any],
+ config_path: str | None = None,
) -> list[str]:
"""
Convert a pyproject.toml file to a requirements.txt file.
@@ -115,6 +124,11 @@ def _pyproject_toml_to_requirements_txt(
#
# [tool.uv.sources]
# python-gcode = { git = "https://github.com/fetlab/python_gcode", rev = "new" }
+
+ Args:
+ pyproject: A dict containing the pyproject.toml contents.
+ config_path: The path to the pyproject.toml or inline script metadata. This
+ is used to resolve relative paths used in the dependencies.
""" # noqa: E501
dependencies = cast(list[str], pyproject.get("dependencies", []))
if not dependencies:
@@ -155,7 +169,12 @@ def _pyproject_toml_to_requirements_txt(
)
# Handle local paths
elif "path" in source:
- new_dependency = f"{dependency} @ {source['path']}"
+ source_path = Path(source["path"])
+ # If path is relative and we have a config path, resolve it relative to the config path
+ if not source_path.is_absolute() and config_path:
+ config_dir = Path(config_path).parent
+ source_path = (config_dir / source_path).resolve()
+ new_dependency = f"{dependency} @ {str(source_path)}"
# Handle URLs
elif "url" in source:
diff --git a/tests/_cli/test_sandbox.py b/tests/_cli/test_sandbox.py
index 583562ccbe..27ae4c4e0c 100644
--- a/tests/_cli/test_sandbox.py
+++ b/tests/_cli/test_sandbox.py
@@ -195,7 +195,7 @@ def test_construct_uv_cmd_with_index_urls() -> None:
}
}
with patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:
- mock.return_value = PyProjectReader(pyproject)
+ mock.return_value = PyProjectReader(pyproject, config_path=None)
uv_cmd = construct_uv_command(
["edit", "test.py", "--sandbox"],
"test.py",
@@ -223,7 +223,7 @@ def test_construct_uv_cmd_with_index_configs() -> None:
}
}
with patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:
- mock.return_value = PyProjectReader(pyproject)
+ mock.return_value = PyProjectReader(pyproject, config_path=None)
uv_cmd = construct_uv_command(
["edit", "test.py", "--sandbox"],
name="test.py",
@@ -248,7 +248,7 @@ def test_construct_uv_cmd_with_sandbox_flag() -> None:
def test_construct_uv_cmd_empty_dependencies() -> None:
# Test empty dependencies triggers refresh
with patch("marimo._cli.sandbox.PyProjectReader.from_filename") as mock:
- mock.return_value = PyProjectReader({})
+ mock.return_value = PyProjectReader({}, config_path=None)
uv_cmd = construct_uv_command(
["edit", "test.py"],
name="test.py",
diff --git a/tests/_utils/test_inline_script_metadata.py b/tests/_utils/test_inline_script_metadata.py
index 4dc480f42b..4bea07f989 100644
--- a/tests/_utils/test_inline_script_metadata.py
+++ b/tests/_utils/test_inline_script_metadata.py
@@ -157,6 +157,30 @@ def test_pyproject_toml_to_requirements_txt_with_local_path():
]
+def test_pyproject_toml_to_requirements_txt_with_relative_path():
+ pyproject = {
+ "dependencies": [
+ "marimo",
+ "polars",
+ ],
+ "tool": {
+ "uv": {
+ "sources": {
+ "marimo": {
+ "path": "../local/marimo",
+ }
+ }
+ }
+ },
+ }
+ # Test with a config path to verify relative path resolution
+ config_path = "/Users/me/project/script.py"
+ assert _pyproject_toml_to_requirements_txt(pyproject, config_path) == [
+ "marimo @ /Users/me/local/marimo",
+ "polars",
+ ]
+
+
@pytest.mark.parametrize(
"version_spec",
[
@@ -193,15 +217,17 @@ def test_pyproject_toml_to_requirements_txt_with_versioned_dependencies(
def test_get_python_version_requirement():
pyproject = {"requires-python": ">=3.11"}
- assert PyProjectReader(pyproject).python_version == ">=3.11"
+ assert (
+ PyProjectReader(pyproject, config_path=None).python_version == ">=3.11"
+ )
pyproject = {"dependencies": ["polars"]}
- assert PyProjectReader(pyproject).python_version is None
+ assert PyProjectReader(pyproject, config_path=None).python_version is None
- assert PyProjectReader({}).python_version is None
+ assert PyProjectReader({}, config_path=None).python_version is None
pyproject = {"requires-python": {"invalid": "type"}}
- assert PyProjectReader(pyproject).python_version is None
+ assert PyProjectReader(pyproject, config_path=None).python_version is None
def test_get_dependencies_with_python_version():
@@ -217,7 +243,9 @@ import marimo
pyproject = read_pyproject_from_script(SCRIPT)
assert pyproject is not None
- assert PyProjectReader(pyproject).python_version == ">=3.11"
+ assert (
+ PyProjectReader(pyproject, config_path=None).python_version == ">=3.11"
+ )
SCRIPT_NO_PYTHON = """
# /// script
@@ -228,7 +256,10 @@ import marimo
"""
pyproject_no_python = read_pyproject_from_script(SCRIPT_NO_PYTHON)
assert pyproject_no_python is not None
- assert PyProjectReader(pyproject_no_python).python_version is None
+ assert (
+ PyProjectReader(pyproject_no_python, config_path=None).python_version
+ is None
+ )
assert PyProjectReader.from_script(SCRIPT_NO_PYTHON).dependencies == [
"polars"
]