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 OSError when running marimo with uv projects on Windows (#7502) (#7504)

Myles Scolnick5802982c699f
diff --git a/marimo/_config/utils.py b/marimo/_config/utils.py
index a2abadb6dd..7544d587dd 100644
--- a/marimo/_config/utils.py
+++ b/marimo/_config/utils.py
@@ -37,15 +37,27 @@ def get_or_create_user_config_path() -> str:
If no config file is found, one will be created under the proper XDG path
(i.e. `~/.config/marimo` or `$XDG_CONFIG_HOME/marimo`)
"""
- current_config_path = get_user_config_path()
- if current_config_path:
- return current_config_path
- else:
- config_path = marimo_config_path()
- config_path.parent.mkdir(parents=True, exist_ok=True)
- # Create an empty file
- config_path.touch()
- return str(config_path)
+ try:
+ current_config_path = get_user_config_path()
+ if current_config_path:
+ return current_config_path
+ except OSError as e:
+ # Handle OSError that can occur on Windows when os.path.realpath()
+ # fails due to issues like deleted directory, permission problems,
+ # UNC path issues, or special characters in path.
+ # See https://github.com/marimo-team/marimo/issues/7502
+ LOGGER.error(
+ "Could not search for config file due to path error: %s. "
+ "Falling back to XDG config path.",
+ str(e),
+ )
+
+ # No config found or error occurred, create XDG config
+ config_path = marimo_config_path()
+ config_path.parent.mkdir(parents=True, exist_ok=True)
+ # Create an empty file
+ config_path.touch()
+ return str(config_path)
# This operation may be expensive due to searching for a config file up to
diff --git a/tests/_config/test_config_utils.py b/tests/_config/test_config_utils.py
index 15235de6a5..ed928d033d 100644
--- a/tests/_config/test_config_utils.py
+++ b/tests/_config/test_config_utils.py
@@ -106,3 +106,33 @@ def test_get_or_create_config_path():
with _mock_file_exists(exists=[xdg_config_path, home_config_path]):
found_config_path = get_or_create_user_config_path()
assert found_config_path == home_config_path
+
+
+def test_get_or_create_config_path_handles_oserror():
+ """Test that get_or_create_user_config_path handles OSError gracefully.
+
+ This can happen on Windows when os.path.realpath(os.getcwd()) fails due to:
+ - Deleted directory
+ - Permission issues
+ - UNC path problems
+ - Special characters in path
+
+ See issue #7502
+ """
+ with tempfile.TemporaryDirectory() as temp_dir:
+ # Use temp dir to avoid creating stray xdg config
+ os.environ["XDG_CONFIG_HOME"] = temp_dir
+ xdg_config_path = str(Path(temp_dir) / "marimo/marimo.toml")
+
+ get_user_config_path.cache_clear()
+
+ # Mock os.path.realpath to raise OSError (simulating Windows path issue)
+ with mock.patch(
+ "marimo._config.utils.os.path.realpath",
+ side_effect=OSError("Failed to canonicalize path"),
+ ):
+ # Should handle the error gracefully and return XDG config path
+ found_config_path = get_or_create_user_config_path()
+ assert found_config_path == xdg_config_path
+ # Verify the config file was created
+ assert Path(found_config_path).exists()