From c5f0edfaf9463d52c9a43828ab642c5acd4aa98d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 13 May 2026 09:15:38 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20[Performance]=20Optimize=20?= =?UTF-8?q?path=20containment=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: thirdeyenation <133812267+thirdeyenation@users.noreply.github.com> --- .jules/bolt.md | 3 +++ helpers/files.py | 3 ++- 2 files changed, 5 insertions(+), 1 deletion(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 0000000000..22b223def6 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2024-05-18 - [Optimize path containment checks] +**Learning:** Using `os.path.commonpath` for path containment checks is a significant bottleneck due to internal list allocations and path splitting. +**Action:** Use `os.path.abspath` combined with `str.startswith()` and a conditionally appended `os.sep` to prevent path traversal bugs while being significantly faster. diff --git a/helpers/files.py b/helpers/files.py index c77ab54cf1..1387587949 100644 --- a/helpers/files.py +++ b/helpers/files.py @@ -653,7 +653,8 @@ def is_in_dir(path: str, dir: str): # check if the given path is within the directory abs_path = os.path.abspath(path) abs_dir = os.path.abspath(dir) - return os.path.commonpath([abs_path, abs_dir]) == abs_dir + # ⚡ Bolt Optimization: Replace slow os.path.commonpath with startswith to avoid list allocations + return abs_path == abs_dir or abs_path.startswith(abs_dir + ('' if abs_dir.endswith(os.sep) else os.sep)) def get_subdirectories(