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

Sort imports on save is very buggy #4831

Closed
DonJayamanne opened this issue Mar 19, 2019 · 19 comments
Closed

Sort imports on save is very buggy #4831

DonJayamanne opened this issue Mar 19, 2019 · 19 comments
Assignees
Labels
area-editor-* User-facing catch-all area-formatting bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on

Comments

@DonJayamanne
Copy link

Problems:

  • Lines go missing
  • Keeps sorting imports even when there are no changes
    (add 4-5 imports with relative imports, every time you save, the imports get formatted differently)
@DonJayamanne DonJayamanne added bug Issue identified by VS Code Team member as probable bug needs PR area-formatting labels Mar 19, 2019
@DavidArchibald
Copy link

I'm unable to reproduce any of these behaviors. However I do sometimes find that import will be seemingly repeated for a second and then fixed, but even that is pretty rare. I use these settings:

"[python]": {
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    }
}

I've been using them for some time without much issue, but I only used a clean environment with those exact settings for a bit so I could've missed those glitches. In case it's specific to your settings, could you verify if they are the same or different?

@cb109
Copy link

cb109 commented Sep 10, 2019

@MapleCCC
Copy link

MapleCCC commented Nov 16, 2019

Encountered same issue. Every time pressed Ctrl-S, imports get formatted differently. Quickly pressing multiple Ctrl-S even causes the file to be seemingly modified concurrently by two sorting processes, and in turn causes import statements to be messy or deleted.

I think the behaviour I saw is similar to the one presented in #8245 .

@pcko1
Copy link

pcko1 commented Jan 22, 2020

This issue is still there for VSCode v1.41.1.

Right click + Sort imports works fine but subsequent sorting on save keeps reformatting the imports or even arbitrarily deleting some (!)

@iutlu
Copy link

iutlu commented May 11, 2020

Hello, I was looking at the linked issues, and it looks like the main isort inconsistency / data loss issues reported are:

  1. isort not picking up the correct config: Sorting of imports (isort) is different if the file has unsaved modifications #4891, isort not honoring setup.cfg #5840, "organize imports" is inconsistent depending on whether the file is saved or not #8245
  2. Repeated invocations of isort corrupting code: Running organize imports quickly ends up corrupting code #10579
  3. A race condition between format-and-save and sort-on-save: Organize imports and formatters are conflicting due to a race condition vscode#83586 (comment), which is reported to have been fixed (?)

I was wondering if the current issue is a separate issue from the above three and if so whether it's still ongoing. Thanks!

@mattstrayer
Copy link

mattstrayer commented Jul 11, 2020

Was trying to get isort working on-save using the latest vscode-python/pylance setup.

As of now this settings.json config works, but the file gets written to twice in the process, so you see a flicker between when it sorts the imports and when it formats with black.

    "python.formatting.provider": "black",
    "python.languageServer": "Pylance",
 
    "[python]": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "ms-python.python",
        "editor.tabSize": 4,     
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }   
    },

Would be ideal to pipe each stage into the next for a clean single UI update/file save.

It is most noticeable when there's a tuple of imports.... like:

from .models import (
    Model1,
    Model2
    Model3,
    Model4,
    Model5,
    Model6,
    Model7,
    Model8,
    Model9,
    Model10,
)

You see the sorting happen and the imports get compressed to a line or two, and then black will format the newly sorted imports.

@karrtikr
Copy link

karrtikr commented Aug 3, 2020

Related: #5748

@Zacharias030
Copy link

The sorting imports functionality is a mess :|

@karrtikr
Copy link

karrtikr commented Aug 12, 2020

Some issues mentioned here are fixed now. I recommend everyone to open up new issues if they encounter it. @Zacharias030

@Zacharias030
Copy link

Zacharias030 commented Aug 12, 2020 via email

@pcko1
Copy link

pcko1 commented Sep 15, 2020

I still get imports deleted automagically when I apply:

"[python]": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "ms-python.python",
        "editor.tabSize": 4,     
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }   
    },

My setup description is VSCode 1.45.1 on Windows10.

It's been a year and this simple issue remains unresolved somehow...

br3ndonland added a commit to br3ndonland/dotfiles that referenced this issue Dec 11, 2020
microsoft/vscode-python#4831

There are still several related open issues in microsoft/vscode-python,
but this seems to be working as of [email protected].
@ansar-sa
Copy link

ansar-sa commented Aug 6, 2021

Ridiculously slow to sort imports. Buggy, slow, magical deletions.

Version: 1.58.2 (Universal)
Commit: c3f126316369cd610563c75b1b1725e0679adfb3
Date: 2021-07-14T22:09:06.581Z
Electron: 12.0.13
Chrome: 89.0.4389.128
Node.js: 14.16.0
V8: 8.9.255.25-electron.0
OS: Darwin x64 20.5.0

"[python]": {
        "editor.formatOnSave": true,
        "editor.defaultFormatter": "ms-python.python",
        "editor.codeActionsOnSave": {
            "source.organizeImports": true
        }   
    }

@brettcannon
Copy link
Member

Can people please try https://marketplace.visualstudio.com/items?itemName=ms-python.isort and let us know if it solves their issues?

@brettcannon brettcannon added the info-needed Issue requires more information from poster label Jun 29, 2022
@oizik
Copy link

oizik commented Aug 4, 2022

Just ran into the same problem where VSCode with isort would delete unused import statements even in a try/except block such as:

def ready(self):
    try:
        import pdj.users.signals
    except ImportError:
        pass

Extension as suggested by @brettcannon fixed the issue.

Thank you

@oizik
Copy link

oizik commented Aug 4, 2022

Actually my bad, I think I spoke to soon, still have the same issue even after installing the extension! There is a definitely a conflict somewhere with isort/black and formatting on save.

Upon further checking, I noticed that I had "source.fixAll": true settings. Setting this to false has now resolved the problem, and import statements are staying put and are being organized as expected.

@github-actions github-actions bot removed the needs PR label Aug 9, 2022
@karrtikr karrtikr added the needs PR Ready to be worked on label Aug 9, 2022
@brettcannon brettcannon removed the info-needed Issue requires more information from poster label Sep 22, 2022
@karthiknadig
Copy link
Member

@oizik The "source.fixAll": true will trigger "Remove All unused imports" code action from Pylance. As you can see here it is marked to run as a part of fixAll.

[Trace - 12:19:43 PM] Received response 'textDocument/codeAction - (85)' in 2ms.
Result: [
    {
        "title": "Remove all unused imports",
        "command": {
            "title": "Remove all unused imports",
            "command": "python.removeUnusedImport",
            "arguments": [
                "c:\\GIT\\LSP\\pygls\\.venv\\Lib\\site-packages\\lsprotocol\\converters.py"
            ]
        },
        "kind": "source.fixAll.unusedImports"
    },
    {
        "title": "Convert all import format",
        "command": {
            "title": "Convert all import format",
            "command": "pylance.convertImportFormat",
            "arguments": [
                "c:\\GIT\\LSP\\pygls\\.venv\\Lib\\site-packages\\lsprotocol\\converters.py",
                "absolute"
            ]
        },
        "kind": "source.fixAll.convertImportFormat"
    }
]

Closing this, since this is by design. You could file a bug on pylance to provide better control over what gets executed as a part of fix all. Hrere is their repo: https://github.com/microsoft/pylance-release

@karthiknadig karthiknadig closed this as not planned Won't fix, can't repro, duplicate, stale Sep 22, 2022
@Spenhouet
Copy link

@karthiknadig The issue raised by @oizik is independent of this issue and has nothing to do with it. Citing it as closing reason does not make sense. Please reopen this issue since it is not addressed yet.

@karthiknadig
Copy link
Member

karthiknadig commented Sep 22, 2022

@Spenhouet the resolution is to use https://marketplace.visualstudio.com/items?itemName=ms-python.isort . That will be the future home to import sorting support. That extension runs isort (equivalent to running python -m isort). If you continue to run into this issue even with that extension, please file issues there.

@karthiknadig
Copy link
Member

@Spenhouet Looks like the name that isort uses to support yapf style is google. so set "isort.args": ["--profile", "google"] is the equivalent for yapf.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-editor-* User-facing catch-all area-formatting bug Issue identified by VS Code Team member as probable bug needs PR Ready to be worked on
Projects
None yet
Development

No branches or pull requests

16 participants