From 9e81c33f8a39d4e401bbbc792963b404e83a6f34 Mon Sep 17 00:00:00 2001 From: Will Miao Date: Tue, 17 Mar 2026 11:24:59 +0800 Subject: [PATCH] fix(utils): make sanitize_folder_name idempotent by combining strip/rstrip calls --- py/utils/utils.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/py/utils/utils.py b/py/utils/utils.py index ada56f50..75b19738 100644 --- a/py/utils/utils.py +++ b/py/utils/utils.py @@ -173,10 +173,13 @@ def sanitize_folder_name(name: str, replacement: str = "_") -> str: # Collapse repeated replacement characters to a single instance if replacement: sanitized = re.sub(f"{re.escape(replacement)}+", replacement, sanitized) - sanitized = sanitized.strip(replacement) - - # Remove trailing spaces or periods which are invalid on Windows - sanitized = sanitized.rstrip(" .") + # Combine stripping to be idempotent: + # Right side: strip replacement, space, and dot (Windows restriction) + # Left side: strip replacement and space (leading dots are allowed) + sanitized = sanitized.rstrip(" ." + replacement).lstrip(" " + replacement) + else: + # If no replacement, just strip spaces and dots from right, spaces from left + sanitized = sanitized.rstrip(" .").lstrip(" ") if not sanitized: return "unnamed"