-
Notifications
You must be signed in to change notification settings - Fork 767
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 is severely bugged with Pylance language server #23
Comments
Transferring this over; do you have We may want to disable our organize import functionality (at least the one that's used on save and such) given we require the core extension. |
This could be something along the lines of:
Or similar race conditions. |
Is this a dup of microsoft/vscode-python#10579? In which case it may not be a pylance issue. |
Could be, yeah. My thought is that Pylance also potentially doing import sorting itself could be triggering the race much more often (rather than trying to make it race by hand). |
@jakebailey Yes I have isort activated in the Python extension (it's the default and only import sorter used by the extension I believe). Does Pylance do its own import sorting as well ? Because in the logs I pasted, it's only isort being called twice : This is the command being called twice at a time in the logs :
And this is the sortImports.py file that is called, which in turns calls isort : # Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import io
import os
import os.path
import sys
isort_path = os.path.join(os.path.dirname(__file__), "lib", "python")
sys.path.insert(0, isort_path)
# Work around stdin buffering issues on windows (https://bugs.python.org/issue40540)
# caused in part by isort seeking within the stdin stream by replacing the
# stream with something which is definitely seekable.
try:
# python 3
stdin = sys.stdin.buffer
except AttributeError:
# python 2
stdin = sys.stdin
sys.stdin = io.BytesIO(stdin.read())
# End workaround
import isort.main
isort.main.main() |
Yes, Pylance has its own. |
I have not seen any setting or bullet point in Pylance's features list about the import sorter ; maybe it could be helpful clarifying that it exists, and also adding an option to enable/disable it and use the Python extension's one instead ? 😃 That would be awesome ! |
I think we may just disable the one in Pylance for now, and allow the core extension to continue to offer this using |
That would be great ! Do you think this will be a hotfix or should we users wait the next planned release ? |
We're still working on what "next release" or "hotfix" will mean, but I expect that this will be disabled in the next version (whichever that is) and we can see if that fixes the race. |
It’s probably better to disable isort if possible because I think that’s
where the problem is.
…On Thu, Jul 2, 2020 at 12:15 PM Jake Bailey ***@***.***> wrote:
We're still working on what "next release" or "hotfix" will mean, but I
expect that this will be disabled in the next version (whichever that is)
and we can see if that fixes the race.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#23 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAVCPCHH576T5YFUURNPKP3RZTMDNANCNFSM4OOGNNHQ>
.
|
The race in the core extension will likely be fixed in microsoft/vscode-python#12728, but we didn't intend to enable our own import sorting at release, so it'll be disabled in the next release until we can better work out the dynamic between the different sorting providers. |
Although this is similar to microsoft/vscode-python#10579, 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. |
Is there a way to disable Pylance import sorting? I cannot find the option in vscode as I could with Pyright. |
In our next release, it will be disabled. |
This issue has been fixed in version 2020.7.0, which we've just released. You can find the changelog here: https://github.com/microsoft/pylance-release/blob/master/CHANGELOG.md#202070-9-july-2020 |
May I ask what's the reason for reinventing the wheel with import sorting instead of using isort under the hood? This used to be a huge mess when I was still using PyCharm since it was pretty much impossible to get the same logic in the editor and in isort. Also, in CI and pre-commit hooks I obviously use isort (because neither have access to pylance), so unless it's 100% compatible and can reuse the isort config file, it means I now have to put the config on how to sort imports in two places... |
Note the history of this code; Pylance is mostly pyright, which is its own extension that has existed for a year+. It worked without the Python extension, and we inherited many of its behaviors which are implemented via the language server protocol, "organize imports" included. We are well aware of The actual import sorting code still needs to be around; we need to be able to calculate a semantically correct text change to add imports for auto-import fixes and completions, but you should definitely be able to use |
As for why not use The Python extension and PTVS have invested great amounts of time in getting good support for installing packages into the slew of different types of Python environments, the UI experience to manage them, as well as the special casing needed for package managers like The language server cannot (at the moment anyway) offer prompts to ask for permission to install things, and the extension and PTVS will still need to retain their package installing code for other reasons, so it turns into a code duplication problem. Our current setup doesn't change this, and continues to require the extension / PTVS to manage and run these external tools. |
How can vscode be set up to sort module level import only at top of file with pylance (ver 2020.7.3) ? The setting:
...does not work |
Environment data
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 : microsoft/vscode#83586), 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 :
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
but what actually happens is
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](https://user-images.githubusercontent.com/35762435/86282523-45deba80-bb94-11ea-94cd-2355a6b19c59.gif)
Logs
Output for
Python
in theOutput
panel (View
→Output
, change the drop-down the upper-right of theOutput
panel toPython
)Output from
Console
under theDeveloper Tools
panel (toggle Developer Tools on underHelp
; turn on source maps to make any tracebacks be useful by runningEnable source map support for extension debugging
)The logs seem to indicate that
sortImports.py
is actually called twice at a time for some reason, maybe the two executions conflict with one another ?The text was updated successfully, but these errors were encountered: