microsoft / vscode-isort

Import sorting for python using the isort library.
https://marketplace.visualstudio.com/items?itemName=ms-python.isort
MIT License
91 stars 22 forks source link

bug: lines being deleted when setting organizeImports to explicit #391

Closed alexdashly closed 9 months ago

alexdashly commented 9 months ago

Hi,

I have a very basic python file I was able to replicate this on. Here it is in its base state:

from abc import abstractmethod

from pydantic import BaseModel

test = "abc"

def func() -> None:
    return None

Here is my settings.json file:

{
  "editor.formatOnSaveMode": "file",
  "editor.formatOnSave": true,
  "git.decorations.enabled": false,
  "[python]": {
    "editor.defaultFormatter": "ms-python.black-formatter",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.organizeImports": "explicit"
    }
  },
  "isort.args": ["--profile", "black"],
  "editor.rulers": [88],
  "files.exclude": {
    "**/.venv": true,
    "**/reports": true
  }
}

If I add lines above test = ... (in an unsaved state looks like this)

from abc import abstractmethod

from pydantic import BaseModel

test = "abc"

def func() -> None:
    return None

then when I save and everything runs, lines end up getting deleted

from abc import abstractmethod

from pydantic import BaseModel

    return None

I can fix this behavior by commenting out "source.organizeImports": "explicit".

I appear to be on the latest version of the extension but I don't know where to go to show the specific version info.

karthiknadig commented 9 months ago

@alexdashly Do you have any other import organizer installed like ruff ? This happens if you have more than one installed. In that case you have to be more explicit in your settings like "source.organizeImports.isort": "explicit" (to use isort) or "source.organizeImports.ruff": "explicit" (to use ruff)

alexdashly commented 9 months ago

Ah, that would do it! Makes total sense. All fixed now. Thanks.