Skip to content

Commit e2d7f7f

Browse files
fix: make resource module usage Windows-compatible
1 parent ac96b79 commit e2d7f7f

File tree

4 files changed

+42
-4
lines changed

4 files changed

+42
-4
lines changed

docs/graph-sitter/installation.mdx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,12 @@ Having issues? Here are some common problems and their solutions:
8484

8585
- **I'm hitting an UV error related to `[[ packages ]]`**: This means you're likely using an outdated version of UV. Try updating to the latest version with: `uv self update`.
8686
- **I'm hitting an error about `No module named 'codegen.sdk.extensions.utils'`**: The compiled cython extensions are out of sync. Update them with `uv sync --reinstall-package codegen`.
87-
- **I'm hitting a `RecursionError: maximum recursion depth exceeded` error while parsing my codebase**: If you are using python 3.12, try upgrading to 3.13. If you are already on 3.13, try upping the recursion limit with `sys.setrecursionlimit(10000)`.
87+
- **I'm hitting a `RecursionError: maximum recursion depth exceeded` error while parsing my codebase**:
88+
- If you are using python 3.12, try upgrading to 3.13
89+
- If you are already on 3.13:
90+
- On Linux/macOS: The recursion limit is automatically increased
91+
- On Windows: Try increasing the recursion limit with `sys.setrecursionlimit(10000)`
92+
- If issues persist, consider using WSL for better compatibility
8893

8994
<Note>
9095
For more help, join our [community Slack](/introduction/community) or check the [FAQ](/introduction/faq).

src/codegen/sdk/core/file.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import os
22
import re
3-
import resource
43
import sys
54
from abc import abstractmethod
65
from collections.abc import Generator, Sequence
@@ -438,7 +437,11 @@ def __init__(self, ts_node: TSNode, filepath: PathLike, ctx: CodebaseContext) ->
438437
try:
439438
self.parse(ctx)
440439
except RecursionError as e:
441-
logger.exception(f"RecursionError parsing file {filepath}: {e} at depth {sys.getrecursionlimit()} and {resource.getrlimit(resource.RLIMIT_STACK)}")
440+
if sys.platform == "win32":
441+
logger.exception(f"RecursionError parsing file {filepath}: {e} at depth {sys.getrecursionlimit()}")
442+
else:
443+
import resource
444+
logger.exception(f"RecursionError parsing file {filepath}: {e} at depth {sys.getrecursionlimit()} and {resource.getrlimit(resource.RLIMIT_STACK)}")
442445
raise e
443446
except Exception as e:
444447
logger.exception(f"Failed to parse file {filepath}: {e}")

tests/shared/utils/recursion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import resource
32
import sys
43

54
logger = logging.getLogger(__name__)
@@ -8,5 +7,6 @@
87
def set_recursion_limit():
98
sys.setrecursionlimit(10**9)
109
if sys.platform == "linux":
10+
import resource
1111
logger.info(f"Setting stack limit to {resource.RLIM_INFINITY}")
1212
resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))

tests/shared/utils/test_recursion.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
import pytest
3+
from unittest.mock import patch, MagicMock
4+
5+
from tests.shared.utils.recursion import set_recursion_limit
6+
7+
8+
def test_set_recursion_limit_windows():
9+
"""Test that set_recursion_limit works on Windows without trying to import resource."""
10+
with patch('sys.platform', 'win32'), \
11+
patch('sys.setrecursionlimit') as mock_setrecursion, \
12+
patch('importlib.import_module') as mock_import:
13+
set_recursion_limit()
14+
mock_setrecursion.assert_called_once_with(10**9)
15+
mock_import.assert_not_called()
16+
17+
18+
def test_set_recursion_limit_linux():
19+
"""Test that set_recursion_limit works on Linux with resource module."""
20+
mock_resource = MagicMock()
21+
mock_resource.RLIM_INFINITY = -1
22+
mock_resource.RLIMIT_STACK = 8
23+
24+
with patch('sys.platform', 'linux'), \
25+
patch('sys.setrecursionlimit') as mock_setrecursion, \
26+
patch('importlib.import_module', return_value=mock_resource) as mock_import:
27+
set_recursion_limit()
28+
mock_setrecursion.assert_called_once_with(10**9)
29+
mock_import.assert_called_once_with('resource')
30+
mock_resource.setrlimit.assert_called_once_with(8, (-1, -1))

0 commit comments

Comments
 (0)