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

Wrong order in import when using sort imports in vscode #14254

Closed
lfrodrigues opened this issue Oct 4, 2020 · 14 comments
Closed

Wrong order in import when using sort imports in vscode #14254

lfrodrigues opened this issue Oct 4, 2020 · 14 comments
Assignees
Labels
area-formatting bug Issue identified by VS Code Team member as probable bug needs proposal Need to make some design decisions

Comments

@lfrodrigues
Copy link

lfrodrigues commented Oct 4, 2020

Sorting using the Python Refactor: Sort Imports doesn't generate a correct output.
When I run the same command in the console the output is correct.

Environment data

  • VS Code version: 1.49.0
  • Extension version (available under the Extensions sidebar): v2020.9.112786
  • OS and version: Ubuntu 18.04
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.6
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): virtualenv
  • Value of the python.languageServer setting: Pylance

Expected behaviour

When I sort in the console:

~/Development/virtual_env/bin/python ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/sortImports.py src/accounting/lms/utils/xero/book_gt_bank.py

I get:

from datetime import date, datetime
from typing import List

import pytz
from django.conf import settings
from django.utils import timezone

from accounting.lms.utils.xero.base import xero_execute_booking
from transaction.models import Transaction

Actual behaviour

When I sort using the vscode command Python Refactor: Sort Imports the I get:

from datetime import date, datetime
from typing import List

import pytz
from accounting.lms.utils.xero.base import xero_execute_booking
from django.conf import settings
from django.utils import timezone
from transaction.models import Transaction

Steps to reproduce:

Just try to sort imports as I explained above and it should happen.

Logs

Here are the logs from vscode

> ~/Development/virtual_env/bin/python ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/sortImports.py - --diff
cwd: ~/Development/backend/src/accounting/lms/utils/xero
> ~/Development/virtual_env/bin/python ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.112786/pythonFiles/sortImports.py - --diff
cwd: ~/Development/backend/src/accounting/lms/utils/xero

@lfrodrigues lfrodrigues added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Oct 4, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Oct 5, 2020
@ericsnowcurrently
Copy link
Member

@lfrodrigues, thanks for letting us know about this. We'll look into this as soon as we can. In the meantime, please provide the content of your settings.json file.

@flyte
Copy link

flyte commented Oct 8, 2020

I believe the cwd is wrong when isort is executed by VSCode, whereas it's using the correct one when executed in the console. This affects the implied PYTHONPATH, causing isort to be unable to find the local top-level modules.

The following example uses no workspace settings. My user settings that apply to Python follow:

{
  "python.formatting.blackArgs": ["-l 90"],
  "python.formatting.blackPath": "/home/flyte/.local/bin/black",
  "python.formatting.provider": "black",
  "python.linting.pylintArgs": ["--max-line-length=100"],
  "python.linting.flake8Args": ["--max-line-length=100"],
  "python.sortImports.args": [
    "--line-length=90",
    "--use-parentheses",
    "--trailing-comma",
    "--multi-line=3"
  ],
  "python.pythonPath": "/usr/bin/python3",
  "python.dataScience.askForKernelRestart": false
}

Directory Structure:

.
├── bar
│   └── __init__.py
└── foo
    └── __init__.py

bar/__init__.py:

import os
import requests
import foo

pass

Sorting imports from VSCode in bar/__init__.py causes isort to have a cwd of ./bar:

> ~/dev/isort-vscode-issue-14254/.ve/bin/python3.6 ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/sortImports.py - --diff --line-length=90 --use-parentheses --trailing-comma --multi-line=3
cwd: ~/dev/isort-vscode-issue-14254/bar

and subsequently sorts the imports as follows:

import os

import foo
import requests

pass

If we execute isort in the console, from the top-level of the package:

(.ve) flyte@demosthenes:~/dev/isort-vscode-issue-14254
$ ~/dev/isort-vscode-issue-14254/.ve/bin/python3.6 ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/sortImports.py bar/__init__.py --line-length=90 --use-parentheses --trailing-comma --multi-line=3
Fixing /home/flyte/dev/isort-vscode-issue-14254/bar/__init__.py

then the imports are sorted correctly:

import os

import requests

import foo

pass

If we cd to the bar directory first, then run the same command:

(.ve) flyte@demosthenes:~/dev/isort-vscode-issue-14254
$ cd bar/
(.ve) flyte@demosthenes:~/dev/isort-vscode-issue-14254/bar
$ ~/dev/isort-vscode-issue-14254/.ve/bin/python3.6 ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.9.114305/pythonFiles/sortImports.py __init__.py --line-length=90 --use-parentheses --trailing-comma --multi-line=3
Fixing /home/flyte/dev/isort-vscode-issue-14254/bar/__init__.py

then the imports are sorted wrong again:

import os

import foo
import requests

pass

@lfrodrigues
Copy link
Author

Here's my settings.json

{
  "files.exclude": {
    "**/.git": true,
    "**/.svn": true,
    "**/.hg": true,
    "**/CVS": true,
    "**/.DS_Store": true,
    "**/__pycache__": true
  },
  "editor.rulers": [120],
  "extensions.ignoreRecommendations": false,
  "window.zoomLevel": 0,
  "python.formatting.provider": "black",
  "python.formatting.blackArgs": [
    "--line-length",
    "120",
    "--skip-string-normalization"
  ],
  "editor.formatOnType": true,
  "editor.formatOnPaste": false,
  "editor.formatOnSave": true,
  "breadcrumbs.enabled": true,
  "python.linting.prospectorEnabled": true,
  "python.linting.enabled": true,
  "python.linting.pylintEnabled": false,
  "python.testing.unittestEnabled": false,
  "python.testing.pytestEnabled": false,
  "python.testing.nosetestsEnabled": false,
  "editor.suggestSelection": "first",
  "vsintellicode.modify.editor.suggestSelection": "automaticallyOverrodeDefaultValue",
  "vsintellicode.python.completionsEnabled": true,
  "[json]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[html]": {
    "editor.defaultFormatter": "vscode.html-language-features"
  },
  "workbench.iconTheme": "vscode-icons",
  "vsicons.dontShowNewVersionMessage": true,
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },

  "search.searchOnType": false,
  "python.languageServer": "Pylance",
  "python.analysis.extraPaths": [
    "a_path_here"
  ]
}

@chm123
Copy link

chm123 commented Oct 14, 2020

I encountered same problem. I found that the isort package itself behaves differently when I was using isort==4.3.8 vs sort==5.5.3. If I call isort==4.3.8 in command line, everything is as expected. I think VSCode bumped the isort version it internally uses, so you see this incorrect order of imports.

In your settings.json, try to add this and see if it works for you:

"python.sortImports.args": [
        "-p",
        "accounting",
        "-p",
        "transaction"
    ],

@mariadb-JeffBachtel
Copy link

I ran into this and tracked it down a bit. The problem seems to be at

cwd: path.dirname(uri.fsPath)
where the cwd is set to the directory the Python source file lives in, vice say ${workspaceFolder}

For me, a solution was setting as follows
image

However a patch to importSortProvider.ts to default cwd to workspaceFolder might work better (but I don't know other gotchas)

@Glyphack
Copy link

Glyphack commented Oct 14, 2020

@mariadb-JeffBachtel It worked for me. however this setting is no where documented and is a necessary for using isort in vscode(at first the behavior is different and you don't know why or In workspaces having code in src like my case setting to that folder worked). maybe documenting this for troubleshooting also helps(and less risky than the patch).

@mariadb-JeffBachtel
Copy link

Yeah, I'm curious what @ericsnowcurrently thinks is the best way forward, but I'm glad it worked for you as a workaround

@juliussimonelli
Copy link

For me, the solution by @mariadb-JeffBachtel partially solves my problem, but not completely. I wondering if the root of the problem is that I installed my modules with pip install -e .. Here's what I'm seeing. If I am inside a file of my_module and have no arguments in python.sortImports.args and try to sort my imports, I get:

import argparse

import numpy
from my_module.utils import func
from my_other_module import func2

from ..helpers import func3

If I add

    "python.sortImports.args": [
        "--src=${workspaceFolder}"
    ]

to my settings (per @mariadb-JeffBachtel 's suggestion ) and then try to sort imports, I get:

import argparse

import numpy
from my_other_module import func2

from my_module.utils import func

from ..helpers import func3

So now it has designed the module I'm in (my_module) from the third-party libraries, but not my_other_module. I still don't know how to get it to do that.

@ericsnowcurrently ericsnowcurrently added area-formatting investigating We are looking into the cause of the issue and removed triage labels Oct 26, 2020
@alexanderdevm
Copy link

As @juliussimonelli suggests:

"python.sortImports.args": [
        "--src=${workspaceFolder}"
    ]

by adding the following to vscode setting.json resolved the issue, at least for me.

@wardy3
Copy link

wardy3 commented Dec 2, 2020

the --src override didn't work for me. It still groups 3rd party and local imports together

however, setting the path to isort in my venv did work :-/
I'm not sure which isort it was using before

    "python.sortImports.path": "/Users/ME/.local/share/virtualenvs/PROJECT-kZt1N2hz/bin/isort"

@g0di
Copy link

g0di commented Dec 9, 2020

the --src override didn't work for me. It still groups 3rd party and local imports together

however, setting the path to isort in my venv did work :-/
I'm not sure which isort it was using before

    "python.sortImports.path": "/Users/ME/.local/share/virtualenvs/PROJECT-kZt1N2hz/bin/isort"

Same for me there. I think VSCode is not using the isort from the selected python interpreter. I also tried setting

    "python.sortImports.path": "${config:python.pythonPath} -m isort"
    "python.sortImports.path": "${config:python.pythonPath}/../bin/isort"

This work for pytest

But without better results ...

What I ended up doing is

    "python.sortImports.path": "${workspaceFolder}/.venv/bin/isort"

Assuming that all my projects use a venv in .venv folder... quick and dirty

@karthiknadig
Copy link
Member

There are a bunch of issues with how we handle import sorting. The plan going forward is to move import sorting via isort behind LSP. This gives us better control over where isort runs (currently we use the directory where the file we are sorting exists as the cwd). LSP also makes it easier for us to apply text edits. Lastly, VS Code uses organize imports code action, which we are not using at the moment, switching to LSP will give us that.

Marking this issue as needs proposal, so we can discus this with the team.

@karthiknadig karthiknadig added needs proposal Need to make some design decisions and removed investigating We are looking into the cause of the issue labels Oct 18, 2021
@karthiknadig karthiknadig removed their assignment Oct 18, 2021
@karrtikr
Copy link

karrtikr commented Jun 7, 2022

Hi everyone please try out https://marketplace.visualstudio.com/items?itemName=ms-python.isort extension and see if it fixes the issue.

@brettcannon brettcannon added the info-needed Issue requires more information from poster label Jun 29, 2022
@brettcannon brettcannon removed the info-needed Issue requires more information from poster label Sep 22, 2022
@karthiknadig
Copy link
Member

For anyone running into this problem please try: https://marketplace.visualstudio.com/items?itemName=ms-python.isort . Tat extension will soon replace the experience you see in python extension.

@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-formatting bug Issue identified by VS Code Team member as probable bug needs proposal Need to make some design decisions
Projects
None yet
Development

Successfully merging a pull request may close this issue.