Skip to content

fix: make resource module usage Windows-compatible #1112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion docs/graph-sitter/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,12 @@ Having issues? Here are some common problems and their solutions:

- **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`.
- **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`.
- **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)`.
- **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:
- On Linux/macOS: The recursion limit is automatically increased
- On Windows: Try increasing the recursion limit with `sys.setrecursionlimit(10000)`
- If issues persist, consider using WSL for better compatibility

<Note>
For more help, join our [community Slack](/introduction/community) or check the [FAQ](/introduction/faq).
Expand Down
7 changes: 5 additions & 2 deletions src/codegen/sdk/core/file.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os
import re
import resource
import sys
from abc import abstractmethod
from collections.abc import Generator, Sequence
Expand Down Expand Up @@ -438,7 +437,11 @@ def __init__(self, ts_node: TSNode, filepath: PathLike, ctx: CodebaseContext) ->
try:
self.parse(ctx)
except RecursionError as e:
logger.exception(f"RecursionError parsing file {filepath}: {e} at depth {sys.getrecursionlimit()} and {resource.getrlimit(resource.RLIMIT_STACK)}")
if sys.platform == "win32":
logger.exception(f"RecursionError parsing file {filepath}: {e} at depth {sys.getrecursionlimit()}")
else:
import resource
logger.exception(f"RecursionError parsing file {filepath}: {e} at depth {sys.getrecursionlimit()} and {resource.getrlimit(resource.RLIMIT_STACK)}")
raise e
except Exception as e:
logger.exception(f"Failed to parse file {filepath}: {e}")
Expand Down
2 changes: 1 addition & 1 deletion tests/shared/utils/recursion.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import resource
import sys

logger = logging.getLogger(__name__)
Expand All @@ -8,5 +7,6 @@
def set_recursion_limit():
sys.setrecursionlimit(10**9)
if sys.platform == "linux":
import resource
logger.info(f"Setting stack limit to {resource.RLIM_INFINITY}")
resource.setrlimit(resource.RLIMIT_STACK, (resource.RLIM_INFINITY, resource.RLIM_INFINITY))
30 changes: 30 additions & 0 deletions tests/shared/utils/test_recursion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import sys
import pytest
from unittest.mock import patch, MagicMock

from tests.shared.utils.recursion import set_recursion_limit


def test_set_recursion_limit_windows():
"""Test that set_recursion_limit works on Windows without trying to import resource."""
with patch('sys.platform', 'win32'), \
patch('sys.setrecursionlimit') as mock_setrecursion, \
patch('importlib.import_module') as mock_import:
set_recursion_limit()
mock_setrecursion.assert_called_once_with(10**9)
mock_import.assert_not_called()


def test_set_recursion_limit_linux():
"""Test that set_recursion_limit works on Linux with resource module."""
mock_resource = MagicMock()
mock_resource.RLIM_INFINITY = -1
mock_resource.RLIMIT_STACK = 8

with patch('sys.platform', 'linux'), \
patch('sys.setrecursionlimit') as mock_setrecursion, \
patch('importlib.import_module', return_value=mock_resource) as mock_import:
set_recursion_limit()
mock_setrecursion.assert_called_once_with(10**9)
mock_import.assert_called_once_with('resource')
mock_resource.setrlimit.assert_called_once_with(8, (-1, -1))
Loading