Skip to content

When both Ruff and isort run on-save, last line of file is repeated #240

Closed as not planned

Description

This may well be a Ruff problem but I'm having trouble understanding it, and it only occurs when both Ruff and isort are installed together, so I thought it was worth raising here.

When both Ruff and isort are installed, and configured to organize imports on save, the last line of the file ends up being repeated on-save.

For example, given:

import sys
from setuptools import setup

aa

And this settings.json:

{
    "isort.importStrategy": "useBundled",
    "ruff.importStrategy": "useBundled",
    "editor.formatOnSave": false,
    "editor.codeActionsOnSave": {
        "source.fixAll": false,
        "source.organizeImports": true
    }
}

Then on-save, I get:

import sys

from setuptools import setup

aa
aa

Disabling either extension causes the file to format properly.

I think this has to do with the use of len(document.lines) to format the "whole file" edit, but I would've thought that the "versioned" edits would avoid this issue.

You might decide that this either (1) Ruff's problem, or (2) user error (given that you shouldn't really have two plugins organizing imports), but this does seem like unexpected behavior and possibly indicative of bug in either or both extension.

Activity

  1. added
    triage-needed
    on Feb 13, 2023
  2. karthiknadig commented on Feb 13, 2023

    @karthiknadig
    Member

    Charlie Marsh (@charliermarsh) do both the extensions run the formatting? Can you share the logs from isort and Ruff?

    FYI, In the next release of the python extension, we won't be installing isort. Going forward, people will not have isort extension by default.

  3. charliermarsh commented on Feb 13, 2023

    @charliermarsh
    Author

    Yeah I believe they both run.

    I don't get much from the isort logs (I should probably clone it, run locally, and add more logging):

    /usr/local/bin/python -m isort - --filename /Users/crmarsh/workspace/ruff/setup.py
    CWD Linter: /Users/crmarsh/workspace/ruff
    

    I added some very verbose logging to ruff locally:

    code_action: CodeActionParams(text_document=TextDocumentIdentifier(uri='file:///Users/crmarsh/workspace/ruff/setup.py'), range=0:0-3:2, context=CodeActionContext(diagnostics=[Diagnostic(range=0:7-0:10, message='`sys` imported but unused', severity=<DiagnosticSeverity.Warning: 2>, code='F401', code_description=None, source='Ruff', tags=[<DiagnosticTag.Unnecessary: 1>], related_information=None, data={'content': '', 'message': 'Remove unused import: `sys`', 'location': {'row': 1, 'column': 0}, 'end_location': {'row': 2, 'column': 0}}), Diagnostic(range=1:23-1:28, message='`setuptools.setup` imported but unused', severity=<DiagnosticSeverity.Warning: 2>, code='F401', code_description=None, source='Ruff', tags=[<DiagnosticTag.Unnecessary: 1>], related_information=None, data={'content': '', 'message': 'Remove unused import: `setuptools.setup`', 'location': {'row': 2, 'column': 0}, 'end_location': {'row': 3, 'column': 0}}), Diagnostic(range=3:0-3:2, message='Undefined name `aa`', severity=<DiagnosticSeverity.Warning: 2>, code='F821', code_description=None, source='Ruff', tags=None, related_information=None, data=None)], only=['source.organizeImports'], trigger_kind=None), work_done_token=None, partial_result_token=None)
    Running Ruff with: ['/Users/crmarsh/workspace/vscode-ruff/bundled/libs/bin/ruff', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--fix', '--extend-ignore', 'ALL', '--extend-select', 'I001', '--stdin-filename', '/Users/crmarsh/workspace/ruff/setup.py']
    Number of lines: 4
    lines: ['import sys\n', 'from setuptools import setup\n', '\n', 'aa']
    source: import sys
    from setuptools import setup
    
    aa
    edits_version = 36
    results = [TextEdit(range=0:0-4:0, new_text='import sys\n\nfrom setuptools import setup\n\naa')]
    Running Ruff with: ['/Users/crmarsh/workspace/vscode-ruff/bundled/libs/bin/ruff', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--stdin-filename', '/Users/crmarsh/workspace/ruff/setup.py']
    Running Ruff with: ['/Users/crmarsh/workspace/vscode-ruff/bundled/libs/bin/ruff', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--stdin-filename', '/Users/crmarsh/workspace/ruff/setup.py']
    Running Ruff with: ['/Users/crmarsh/workspace/vscode-ruff/bundled/libs/bin/ruff', '--no-cache', '--no-fix', '--quiet', '--format', 'json', '-', '--stdin-filename', '/Users/crmarsh/workspace/ruff/setup.py']
    code_action: CodeActionParams(text_document=TextDocumentIdentifier(uri='file:///Users/crmarsh/workspace/ruff/setup.py'), range=5:2-5:2, context=CodeActionContext(diagnostics=[Diagnostic(range=5:0-5:2, message='Undefined name `aa`', severity=<DiagnosticSeverity.Warning: 2>, code='F821', code_description=None, source='Ruff', tags=None, related_information=None, data=None)], only=None, trigger_kind=None), work_done_token=None, partial_result_token=None)
    

    After the edit, if I hit Command + Z once, I get:

    import sys
    
    from setuptools import setup
    
    aa

    If I hit it again, I'm back to:

    import sys
    from setuptools import setup
    
    aa
  4. charliermarsh commented on Feb 13, 2023

    @charliermarsh
    Author

    My guess is that they're both generating the edit, and trying to apply it to lines 0-to-3 (or rather, start of line 0 to start of line 4). When the "second" formatter runs, the previous last-line is now on line 4, since we inserted a newline between the sys and setuptools imports. So we end up re-inserting that last line, rather than overwriting it.

  5. karthiknadig commented on Feb 13, 2023

    @karthiknadig
    Member

    This might actually be a VS Code bug. It should not be running both the import organizers. It should be running only one. The thing is I don't think we are even getting diferent versions here. I suspect we may be getting the same version.

  6. karthiknadig commented on Feb 13, 2023

    @karthiknadig
    Member

    Both get the same version:

    This is Ruff
    image

    This is isort
    image

  7. karthiknadig commented on Feb 14, 2023

    @karthiknadig
    Member

    Closing against microsoft/vscode#174295

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage-needed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions