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: traverse previous directories even if directory is not located in home (#7551)

Dylan Madisetti7cc64437e38f
diff --git a/marimo/_config/utils.py b/marimo/_config/utils.py
index 7544d587dd..009d0fa46f 100644
--- a/marimo/_config/utils.py
+++ b/marimo/_config/utils.py
@@ -68,14 +68,10 @@ def get_or_create_user_config_path() -> str:
def get_user_config_path() -> Optional[str]:
"""Find path of config file (.marimo.toml).
- Searches from current directory to home, return the first config file
- found, if Any.
+ Searches from current directory up through all parent directories,
+ then checks home directory, then XDG paths.
- If current directory isn't contained in home, just searches current
- directory and home.
-
- If not found between current directory and home, will search XDG paths
- (i.e. `~/.config/marimo` and `$XDG_CONFIG_HOME/marimo`).
+ Returns the first config file found, if any.
May raise an OSError.
"""
@@ -84,38 +80,39 @@ def get_user_config_path() -> Optional[str]:
# some these functions don't eliminate symlinks on some platforms
current_directory = os.path.realpath(os.getcwd())
home_expansion = os.path.expanduser("~")
- if home_expansion == "~":
- # path expansion failed
- return None
- home_directory = os.path.realpath(home_expansion)
-
- if not _is_parent(home_directory, current_directory):
- # Can't search back to home, since current_directory not in
- # home_directory
+ home_directory = None
+ if home_expansion != "~":
+ home_directory = os.path.realpath(home_expansion)
+
+ # Track whether we've already checked home during parent traversal
+ checked_home = False
+
+ # Traverse parent directories, stopping at home if under home
+ previous_directory = None
+ search_directory = current_directory
+ while search_directory != previous_directory:
+ previous_directory = search_directory
config_path = _check_directory_for_file(
- current_directory, CONFIG_FILENAME
+ search_directory, CONFIG_FILENAME
+ )
+ if config_path is not None:
+ return config_path
+
+ # Stop at home directory if we're under it
+ if search_directory == home_directory:
+ checked_home = True
+ break
+
+ search_directory = os.path.realpath(os.path.dirname(search_directory))
+
+ # Check home directory if not already checked during traversal
+ # (happens when cwd is not under home)
+ if home_directory and not checked_home:
+ config_path = _check_directory_for_file(
+ home_directory, CONFIG_FILENAME
)
if config_path is not None:
return config_path
- else:
- previous_directory = None
- # Search up to home; terminate when at home or at a fixed point
- while (
- current_directory != home_directory
- and current_directory != previous_directory
- ):
- previous_directory = current_directory
- config_path = os.path.join(current_directory, CONFIG_FILENAME)
- if os.path.isfile(config_path):
- return config_path
- else:
- current_directory = os.path.realpath(
- os.path.dirname(current_directory)
- )
-
- config_path = os.path.join(home_directory, CONFIG_FILENAME)
- if os.path.isfile(config_path):
- return config_path
xdg_config_path = marimo_config_path()
if xdg_config_path.is_file():
diff --git a/tests/_config/test_config_utils.py b/tests/_config/test_config_utils.py
index ed928d033d..5f26d8ea92 100644
--- a/tests/_config/test_config_utils.py
+++ b/tests/_config/test_config_utils.py
@@ -8,6 +8,8 @@ from pathlib import Path
from typing import Optional
from unittest import mock
+import pytest
+
from marimo._config.utils import (
get_or_create_user_config_path,
get_user_config_path,
@@ -136,3 +138,139 @@ def test_get_or_create_config_path_handles_oserror():
assert found_config_path == xdg_config_path
# Verify the config file was created
assert Path(found_config_path).exists()
+
+
+class TestGetConfigPathParentTraversal:
+ """Tests for parent directory traversal in config discovery.
+
+ These tests verify the fix for issue #5825 - marimo should search
+ parent directories for .marimo.toml, not just the current directory.
+
+ Test cases are parameterized as (config_locations, cwd, home, expected):
+ - config_locations: list of paths (relative to tmpdir) where .marimo.toml exists
+ - cwd: current working directory (relative to tmpdir)
+ - home: home directory (relative to tmpdir, or absolute path if starts with /)
+ - expected: which config should be found (relative to tmpdir), or None
+ """
+
+ @pytest.mark.parametrize(
+ ("config_locations", "cwd", "home", "expected"),
+ [
+ pytest.param(
+ ["parent"],
+ "parent/child",
+ "", # tmpdir is home
+ "parent",
+ id="config_in_parent_under_home",
+ ),
+ pytest.param(
+ ["grandparent"],
+ "grandparent/parent/child",
+ "", # tmpdir is home
+ "grandparent",
+ id="config_in_grandparent_under_home",
+ ),
+ pytest.param(
+ ["grandparent", "grandparent/parent"],
+ "grandparent/parent/child",
+ "", # tmpdir is home
+ "grandparent/parent", # closer config wins
+ id="closer_config_takes_precedence",
+ ),
+ pytest.param(
+ ["project"],
+ "project/subdir",
+ "/home/testuser", # absolute path, not under tmpdir
+ "project",
+ id="parent_traversal_outside_home",
+ ),
+ pytest.param(
+ ["opt/project", "home/user"],
+ "opt/project/subdir",
+ "home/user",
+ "opt/project", # parent traversal before home
+ id="disjoint_paths_parent_before_home",
+ ),
+ pytest.param(
+ ["home/user"],
+ "opt/project",
+ "home/user",
+ "home/user", # falls back to home
+ id="falls_back_to_home_when_no_parent_config",
+ ),
+ pytest.param(
+ ["project"],
+ "project/subdir",
+ "~", # home expansion fails (service daemon account)
+ "project",
+ id="home_expansion_fails_still_finds_parent",
+ ),
+ pytest.param(
+ ["", "home/user"], # config at both root and home
+ "home/user/example",
+ "home/user",
+ "home/user", # should stop at home, not traverse to root
+ id="stops_at_home_ignores_root_config",
+ ),
+ pytest.param(
+ [""], # config ONLY at root
+ "home/user/example",
+ "home/user",
+ None, # should NOT find root config when under home
+ id="does_not_traverse_past_home_to_root",
+ ),
+ ],
+ )
+ def test_config_path_discovery(
+ self, tmpdir, config_locations, cwd, home, expected
+ ):
+ # Create all necessary directories
+ cwd_path = tmpdir
+ for part in cwd.split("/"):
+ cwd_path = (
+ cwd_path.mkdir(part)
+ if not cwd_path.join(part).exists()
+ else cwd_path.join(part)
+ )
+
+ # Create config files at specified locations
+ for loc in config_locations:
+ config_dir = tmpdir
+ if loc: # non-empty path
+ for part in loc.split("/"):
+ if not config_dir.join(part).exists():
+ config_dir = config_dir.mkdir(part)
+ else:
+ config_dir = config_dir.join(part)
+ config_dir.join(".marimo.toml").write("")
+
+ # Determine home path
+ if home == "~":
+ home_path = "~" # simulate failed expansion
+ elif home.startswith("/"):
+ home_path = home # absolute path
+ elif home == "":
+ home_path = str(tmpdir)
+ else:
+ home_path = str(tmpdir.join(home))
+
+ # Determine expected path
+ if expected is None:
+ expected_path = None
+ else:
+ expected_path = str(tmpdir.join(expected).join(".marimo.toml"))
+
+ get_user_config_path.cache_clear()
+
+ with (
+ mock.patch(
+ "marimo._config.utils.os.getcwd",
+ return_value=str(cwd_path),
+ ),
+ mock.patch(
+ "marimo._config.utils.os.path.expanduser",
+ return_value=home_path,
+ ),
+ ):
+ found_config_path = get_user_config_path()
+ assert found_config_path == expected_path