Ask questionsSort imports is severely bugged with Pylance language server

Environment data

  • VS Code version: 1.46.1
  • Extension version (available under the Extensions sidebar): 2020.6.91350
  • OS and version: macOS 10.14.6
  • Python version (& distribution if applicable, e.g. Anaconda): 3.7
  • Relevant/affected Python-related VS Code extensions and their versions: Pylance 2020.6.1
  • Value of the python.languageServer setting: "Pylance"

Expected behaviour

"Sort imports on save" functionality works correctly.

Actual behaviour

When using the new Pylance language server (which is, apart from this bug, absolutely incredible, thanks a lot !), the "sort imports" functionality is bugged, doesn't respect the sortImports settings of VSCode, and produces invalid Python code. I have looked at other similar issues (ex :, but an important difference is that the issue I describe here is present even when there is no formatter (other than the import sorter) activated for Python code.

Steps to reproduce:

Open a new folder, create a Python file, write the following code in it :

import os
import logging

print(os.path.pathsep)"hello world")

Activate the "source.organizeImports" functionality on save, without any additional args, deactivate any other formatter, linter, etc. Activate the Pylance language server.

Save the current file. The imports should be transformed to

import logging
import os

but what actually happens is

import logging
import logging

2020-07-01 12 05 08

If you disable Pylance and use Jedi instead, the imports are sorted correctly.

The bug can become much more severe with more complex imports if you save several times in a row, generating completely incorrect code with no obvious pattern : 2020-07-01 12 12 07



<summary>Output for <code>Python</code> in the <code>Output</code> panel (<code>View</code>→<code>Output</code>, change the drop-down the upper-right of the <code>Output</code> panel to <code>Python</code>) </summary>


> python3.7 ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ -c "import sys;print(sys.executable)"
> python3.6 ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ -c "import sys;print(sys.executable)"
> python2 ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ -c "import sys;print(sys.executable)"
> python3 ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ -c "import sys;print(sys.executable)"
> python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ -c "import sys;print(sys.executable)"
> ~/Library/Caches/pypoetry/virtualenvs/covia-py3.6/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ -c "import jupyter"
> ~/Library/Caches/pypoetry/virtualenvs/covia-py3.6/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ -c "import notebook"
> ~/Library/Caches/pypoetry/virtualenvs/covia-py3.6/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ jupyter kernelspec --version
Starting Pylance language server.
Python interpreter path: ~/.pyenv/versions/3.7.2/Python.framework/Versions/3.7/bin/python
> conda --version
> ~/.pyenv/versions/3.7.2/Python.framework/Versions/3.7/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ - --diff
cwd: ~/Documents/dev/other_test
> ~/.pyenv/versions/3.7.2/Python.framework/Versions/3.7/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ - --diff
cwd: ~/Documents/dev/other_test

</p> </details>

<br />


<summary> Output from <code>Console</code> under the <code>Developer Tools</code> panel (toggle Developer Tools on under <code>Help</code>; turn on source maps to make any tracebacks be useful by running <code>Enable source map support for extension debugging</code>) </summary>


[Extension Host] Info Python Extension: 2020-07-01 12:09:21: > ~/.pyenv/versions/3.7.2/Python.framework/Versions/3.7/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ - --diff
console.ts:137 [Extension Host] Info Python Extension: 2020-07-01 12:09:21: cwd: ~/Documents/dev/other_test
console.ts:137 [Extension Host] Info Python Extension: 2020-07-01 12:09:21: > ~/.pyenv/versions/3.7.2/Python.framework/Versions/3.7/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/ - --diff
console.ts:137 [Extension Host] Info Python Extension: 2020-07-01 12:09:21: cwd: ~/Documents/dev/other_test

</p> </details>

The logs seem to indicate that is actually called twice at a time for some reason, maybe the two executions conflict with one another ?


Answer questions karrtikr

Although this is similar to, this isn't a dup.

The cause here was that both Pylance and the extension initialized the isort process to get the diff. The result (which is the diff/workspace edit) from both the processes is then applied almost simultaneously, leading to this configuration.

So the correct solution would be to ensure we're using either the extension, or Pylance for import sorting, but not both.


Related questions

No questions were found.
Github User Rank List