Skip to content
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

Infinite loop in import sorting for long variable names with force-single-line (ruff >= 0.0.256) #4062

Closed
pstjohn opened this issue Apr 21, 2023 · 3 comments · Fixed by #4074
Assignees
Labels
bug Something isn't working isort Related to import sorting

Comments

@pstjohn
Copy link

pstjohn commented Apr 21, 2023

The following code snippet causes ruff to hang for ruff >= 0.0.256.

pyproject.toml

[tool.ruff]
select = ["I"]
fix = true
line-length = 88

[tool.ruff.isort]
force-single-line = true

test.py

from mypackage.subpackage2 import (  # long comment that seems to be a problem
    a_long_variable_name_that_causes_problems,
    item2,
)

Doing some bisection, it works in 0.0.255 and hangs in 0.0.256 onwards.
Probably related to #3530? But it happens regardless of the combine-as-imports setting

In the newer versions, it seems to get stuck in an infinite loop

[2023-04-21][20:08:42][ruff_cli::commands::run][DEBUG] Identified files to lint in: 521.625µs
[2023-04-21][20:08:42][ruff_cli::diagnostics][DEBUG] Checking: /Users/pstjohn/Misc/ruff_issue/test.py
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:08:42][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)

The older versions run through twice

[2023-04-21][20:15:05][ruff_cli::commands::run][DEBUG] Identified files to lint in: 381.25µs
[2023-04-21][20:15:05][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:15:05][ruff::rules::isort::categorize][DEBUG] Categorized 'mypackage' as ThirdParty (NoMatch)
[2023-04-21][20:15:05][ruff_cli::commands::run][DEBUG] Checked 1 files in: 1.910292ms
Found 1 error (1 fixed, 0 remaining).

resulting in this formatted import

from mypackage.subpackage2 import (  # long comment that seems to be a problem
    a_long_variable_name_that_causes_problems,
)
from mypackage.subpackage2 import item2
@pstjohn pstjohn changed the title Ruff hangs when sorting imports with long variable names / comments Infinite loop in for long variable names with force-single-line in ruff >= 0.0.256 Apr 22, 2023
@pstjohn pstjohn changed the title Infinite loop in for long variable names with force-single-line in ruff >= 0.0.256 Infinite loop in import sorting for long variable names with force-single-line in ruff >= 0.0.256 Apr 22, 2023
@pstjohn pstjohn changed the title Infinite loop in import sorting for long variable names with force-single-line in ruff >= 0.0.256 Infinite loop in import sorting for long variable names with force-single-line (ruff >= 0.0.256) Apr 22, 2023
@charliermarsh charliermarsh added bug Something isn't working isort Related to import sorting labels Apr 22, 2023
@charliermarsh charliermarsh self-assigned this Apr 23, 2023
@charliermarsh
Copy link
Member

Thanks for the clear issue, taking a look :)

@charliermarsh
Copy link
Member

I've identified the issue (introduced in #3521), which is that we're duplicating the comment with an exponential blowup. Figuring out a proper fix, if I can't fix it properly I'll revert that change prior to the next release.

@pstjohn
Copy link
Author

pstjohn commented Apr 23, 2023

You're welcome, thanks for the quick reply (and great package)!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working isort Related to import sorting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants