Skip to content

Commit 843d5f6

Browse files
goranaivdusek
andauthored
fix(statistics): Show only file names in error groups on Windows (#2515)
### Description - `ErrorTracker._get_file_and_line` cut the frame filename with `split("/")`. On Windows the filename has backslashes, so the error group held the whole path (`C:\...\crawl_job.py:2:ValueError:boom`) instead of the file name (`crawl_job.py:2:ValueError:boom`). It now uses `Path(filename).name`. - `test_error_tracker_similar_messages_full_stack` built its expected value with the same `__file__.split('/')[-1]`, so it passed on Windows with the bug in place. It now uses `Path(__file__).name`. - Adds `test_error_tracker_shows_file_name_without_directories`, which compiles code with a filename built by `os.path.join`, so the path has the separator of the platform. Behavior changes: - On Windows the error group name, and so the keys of `get_most_common_errors()` and the hash in the error snapshot file name, change from a full path to a file name. Linux and macOS are unchanged. - Two errors raised in different folders but in files with the same name now share a group on Windows, as they already do on Linux and macOS. ### Issues - Closes: #2514 ### Testing - `pytest tests/unit/_statistics/test_error_tracker.py` on Windows 11, Python 3.14: 17 passed. With only the source change reverted, 4 fail (the new test and the 3 cases of `test_error_tracker_similar_messages_full_stack`, whose expected value now holds the file name). - `pytest tests/unit/_statistics tests/unit/crawlers/_basic`: 161 passed, 1 skipped (the existing skip for issue 908). - `ruff check` and `ruff format --check` pass on the changed files. - Compared the old code, the new code and `ntpath.basename` on 100000 generated frame filenames (backslash, slash and mixed separators, drive letters, UNC paths, relative paths, `<string>`, non-ASCII names). The old code differs on 67425 of them on Windows, the new code on none. No input that worked before fails now. ### Checklist - [x] CI passed Validated with Claude Opus 5.5. --------- Co-authored-by: Vlada Dusek <v.dusek96@gmail.com>
1 parent 24cadc8 commit 843d5f6

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎src/crawlee/statistics/_error_tracker.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from collections import Counter, defaultdict
77
from itertools import zip_longest
88
from logging import getLogger
9+
from pathlib import Path
910
from typing import TYPE_CHECKING
1011

1112
from crawlee.statistics._error_snapshotter import ErrorSnapshotter
@@ -111,7 +112,7 @@ def _get_file_and_line(self, error: Exception) -> str:
111112
# The exception has no traceback (e.g. it was never raised).
112113
return ''
113114
# Show only the most specific frame.
114-
return f'{error_traceback[-1].filename.split("/")[-1]}:{error_traceback[-1].lineno}'
115+
return f'{Path(error_traceback[-1].filename).name}:{error_traceback[-1].lineno}'
115116
return ''
116117

117118
def _get_error_message(self, error: Exception) -> str:

‎tests/unit/_statistics/test_error_tracker.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import traceback
2+
from pathlib import Path
23
from unittest.mock import AsyncMock, Mock
34

45
import pytest
@@ -73,7 +74,7 @@ async def test_error_tracker_similar_messages_full_stack(
7374
await error_tracker.add(e)
7475
line = traceback.extract_tb(e.__traceback__)[0].lineno
7576

76-
file_name = __file__.split('/')[-1]
77+
file_name = Path(__file__).name
7778
errors = error_tracker.get_most_common_errors()
7879
assert errors[0][0] == f'{file_name}:{line}:KeyError:{message_1}'
7980
assert errors[0][1] == 3
@@ -83,6 +84,19 @@ async def test_error_tracker_similar_messages_full_stack(
8384
assert errors[2][1] == 1
8485

8586

87+
async def test_error_tracker_shows_file_name_without_directories() -> None:
88+
"""Test that the error group shows the file name only, whatever separator the platform puts in the path."""
89+
error_tracker = ErrorTracker()
90+
path = str(Path('some_directory', 'nested', 'failing_module.py'))
91+
92+
try:
93+
exec(compile('raise ValueError("Some value error")', path, 'exec'))
94+
except Exception as e:
95+
await error_tracker.add(e)
96+
97+
assert error_tracker.get_most_common_errors()[0][0] == 'failing_module.py:1:ValueError:Some value error'
98+
99+
86100
@pytest.mark.parametrize(
87101
('show_full_message', 'expected_message'),
88102
[

0 commit comments

Comments
 (0)