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

Python refactor: Sort imports does not work with setuptools 49.2 #12949

Closed
zgoda opened this issue Jul 14, 2020 · 20 comments
Closed

Python refactor: Sort imports does not work with setuptools 49.2 #12949

zgoda opened this issue Jul 14, 2020 · 20 comments
Assignees
Labels
area-editor-* User-facing catch-all bug Issue identified by VS Code Team member as probable bug important Issue identified as high-priority

Comments

@zgoda
Copy link

zgoda commented Jul 14, 2020

Environment data

  • VS Code version: 1.47.0
  • Extension version (available under the Extensions sidebar): 2020.6.91350
  • OS and version: linux Ubuntu 20.04
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.2
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): venv
  • Relevant/affected Python packages and their versions: isort
  • Value of the python.languageServer setting: Jedi

Expected behaviour

Calling "Python refactor: Sort imports" from command palette sorts import in active document

Actual behaviour

Nothing happens

Steps to reproduce:

  • install setuptools >= 49.2 in workspace venv
  • launch "Python refactor: Sort imports" from command palette

Logs

Devtools console:

Error: /home/me/work/proj/venv/lib/python3.8/site-packages/setuptools/distutils_patch.py:25: UserWarning: Distutils was imported before Setuptools. This usage is discouraged and may exhibit undesirable behaviors or errors. Please use Setuptools' objects directly or at least import Setuptools first.
  warnings.warn(

	at i (/home/me/.vscode/extensions/ms-python.python-2020.6.91350/out/client/extension.js:1)
	at Socket.<anonymous> (/home/me/.vscode/extensions/ms-python.python-2020.6.91350/out/client/extension.js:1)
	at Socket.emit (events.js:203)
	at addChunk (_stream_readable.js:295)
	at readableAddChunk (_stream_readable.js:276)
	at Socket.Readable.push (_stream_readable.js:210)
	at Pipe.onStreamRead (internal/stream_base_commons.js:166)
$onExtensionRuntimeError @ workbench.desktop.main.js:sourcemap:4312

Output pane:

> ~/work/proj/venv/bin/python ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/pyvsc-run-isolated.py ~/.vscode/extensions/ms-python.python-2020.6.91350/pythonFiles/sortImports.py - --diff
cwd: ~/work/proj

This warning comes from isort that is installed in venv, both versions 4.3.21 and 5.0.9 (latest) produce this warning. Downgrading setuptools to <49.2 makes "Sort imports" command to work ok again - and this warning disappears. I believe this should not behave this way since even with the warning isort exit code is 0.

@zgoda zgoda added triage-needed Needs assignment to the proper sub-team bug Issue identified by VS Code Team member as probable bug labels Jul 14, 2020
@ghost ghost removed the triage-needed Needs assignment to the proper sub-team label Jul 14, 2020
@zgoda
Copy link
Author

zgoda commented Jul 15, 2020

The issue with isort has been fixed in 5.1.0 but I believe the extension ships with some older version. Setting isort path to installed in workspace venv produces this traceback in dev console:

Error: Traceback (most recent call last):
  File "venv/bin/isort", line 8, in <module>
    sys.exit(main())
  File "/home/me/work/proj/venv/lib/python3.8/site-packages/isort/main.py", line 693, in main

	at i (/home/me/.vscode/extensions/ms-python.python-2020.6.91350/out/client/extension.js:1)
	at Socket.<anonymous> (/home/me/.vscode/extensions/ms-python.python-2020.6.91350/out/client/extension.js:1)
	at Socket.emit (events.js:203)
	at addChunk (_stream_readable.js:295)
	at readableAddChunk (_stream_readable.js:276)
	at Socket.Readable.push (_stream_readable.js:210)
	at Pipe.onStreamRead (internal/stream_base_commons.js:166)
$onExtensionRuntimeError @ mainThreadExtensionService.ts:66
_doInvokeHandler @ rpcProtocol.ts:402
_invokeHandler @ rpcProtocol.ts:387
_receiveRequest @ rpcProtocol.ts:303
_receiveOneMessage @ rpcProtocol.ts:230
(anonymous) @ rpcProtocol.ts:105
fire @ event.ts:587
fire @ ipc.net.ts:453
_receiveMessage @ ipc.net.ts:733
(anonymous) @ ipc.net.ts:592
fire @ event.ts:587
acceptChunk @ ipc.net.ts:239
(anonymous) @ ipc.net.ts:200
t @ ipc.net.ts:28
emit @ events.js:203
addChunk @ _stream_readable.js:295
readableAddChunk @ _stream_readable.js:276
Readable.push @ _stream_readable.js:210
onStreamRead @ internal/stream_base_commons.js:166

Running isort filename.py --diff in terminal works fine. Unfortunately the Python traceback is cut off before anything interesting is printed.

@jleclanche
Copy link

Maybe run isort with PYTHONWARNINGS="ignore"? As it stands this will reappear any time a warning is output by isort or any of its dependencies.

@zgoda
Copy link
Author

zgoda commented Jul 16, 2020

Things did not change with isort 5.1.1 - isort itself runs fine without issues (both with file name and stdin), fails when called from extension. The only way to have this command working is to downgrade setuptools.

@OutOfFocus4
Copy link

I ran into this problem.

I was able to “fix” it by editing pythonFiles/sortImports.py

sys.stdin = io.BytesIO(stdin.read())
# End workaround

import setuptools # <— insert this line
import isort.main

isort.main.main()

@jahan01
Copy link

jahan01 commented Jul 17, 2020

None of workaround specified so far has worked for me. i.e, downgrading setuptools to <49.2 or adding import statement in sortImports.py file.

@GilShoshan94
Copy link

@zgoda

The issue with isort has been fixed in 5.1.0 but I believe the extension ships with some older version.

True, isort >5.1.0 fix it.
The issue is if someone use Pylint. Pylint is not yet compatible wiht isort 5 and thus lists in its dependencies isort <5.
That's why we are stuck at isort 4.3.21 .
pylint-dev/pylint#3722

@OutOfFocus4
I first tried your solution, to edit pythonFiles/sortImports.py but sadly it did not work out for me...
Then I check and the reason why is becuse I am using my env's isort "python.sortImports.path": "C:\\Users\\PC-Gil\\Miniconda3\\envs\\forge\\Scripts\\isort.exe" . So I am not reaching the sortImports.py script. I prefer to use my env's isort so I can control the version I use.

@zgoda @jahan01
I downgraded setuptools to 49.1.3, which remove the error from isort 4.3.21.

Now, I am having a weird behavior:
When "python.sortImports.args" is set (I tried a combination of one or more arguments from this list :
[ "--multi-line=3", "--trailing-comma", "--force-grid-wrap=0", "--use-parentheses", "--line-length=119", ] ),
then the sort import is broken (doesn't work, and if set in "editor.codeActionsOnSave", prevents saving with infinite message Saving 'filename.py': Applying code action 'Sort imports'.). And it is broken with both isort, from vscode and from my env.

Here the Output pane when I sort with one args set for isort:
-With vscode's isort

> ~\Miniconda3\envs\forge\python.exe c:\Users\PC-Gil\.vscode\extensions\ms-python.python-2020.7.94776\pythonFiles\pyvsc-run-isolated.py c:\Users\PC-Gil\.vscode\extensions\ms-python.python-2020.7.94776\pythonFiles\sortImports.py - --diff --line-length=119
cwd: c:\Users\PC-Gil\Documents\test

-With my env's isort

> ~\Miniconda3\envs\forge\Scripts\isort.exe - --diff --line-length=119
cwd: c:\Users\PC-Gil\Documents\test

If "python.sortImports.args" is not set, then it work perfectly, (with vscode's isort as well as with my env's isort) but with the default isort's settings....

I read in another issue that we can set isort args in a config file (#5840 (comment)).
I tried "python.sortImports.args": ["--settings-path=${workspaceFolder}\\setup.cfg"] and for now it works for me.

@karrtikr
Copy link

@zgoda I am able to reproduce this issue with the version you specified, thanks. However with the latest version, I no longer see this issue, please upgrade to the latest extension and let me know.

Can you please try setting PYTHONWARNINGS="ignore" in environment file and see if you still see this warning?

I believe this should not behave this way since even with the warning isort exit code is 0.

That's apparently not true. Quoting the docs: "If any warning is found, the command will exit with an error code"

@karrtikr karrtikr added the info-needed Issue requires more information from poster label Jul 20, 2020
@zgoda
Copy link
Author

zgoda commented Jul 20, 2020

Extension version v2020.7.94776

Vendored isort still throws an exception and does not work if setuptools 49.2 is installed in PYTHONPATH.

Custom provided isort 5.1.4 works fine.

@manmartgarc
Copy link

Extension version v2020.7.94776

Vendored isort still throws an exception and does not work if setuptools 49.2 is installed in PYTHONPATH.

Custom provided isort 5.1.4 works fine.

can confirm. downgraded setuptools to 49.1 and it works again

@karrtikr karrtikr removed the info-needed Issue requires more information from poster label Jul 20, 2020
@karrtikr
Copy link

For some reason, I am unable to repro the issue reliably - but I do see it. Seems to me that we can either go with @OutOfFocus4 's approach or pinning setuptools, as Pylint is not yet compatible with isort 5.

@karrtikr karrtikr added area-editor-* User-facing catch-all needs PR and removed triage labels Jul 20, 2020
@karrtikr karrtikr removed their assignment Jul 20, 2020
@karrtikr karrtikr added the important Issue identified as high-priority label Jul 21, 2020
@karrtikr
Copy link

karrtikr commented Jul 21, 2020

We're going to upgrade isort and make sure it doesn't break pylint.

Pylint is installed within the user environment itself, which will have it's own copy of isort. My guess is we pick that isort instead of the one shipped with the extension when using the pylint command, in my case:

~\AppData\Local\Programs\Python\Python37\python.exe c:\Users\karraj\.vscode-insiders\extensions\ms-python.python-2020.6.91350\pythonFiles\pyvsc-run-isolated.py pylint --msg-template='{line},{column},{category},{symbol}:{msg}' --reports=n --output-format=text c:\Users\karraj\Desktop\imports\a.py

but this is something to double check as well.

@karrtikr karrtikr mentioned this issue Jul 30, 2020
10 tasks
@karrtikr
Copy link

karrtikr commented Jul 31, 2020

Some concerns with upgrading isort: #12932 (comment)

@sukhejai
Copy link

sukhejai commented Aug 8, 2020

Downloading isort-5.3.2-py3-none-any.whl (93 kB)
|████████████████████████████████| 93 kB 166 kB/s
ERROR: pylint 2.5.3 has requirement isort<5,>=4.2.5, but you'll have isort 5.3.2 which is incompatible.

@uksaha
Copy link

uksaha commented Aug 11, 2020

Import, install , isort many commands do not work. Anaconda3 prompt does not recognize all those commands and I can not open Jupyter notebook and others, Recently I installed conda3, my previous version was working fine which I delete because some files was missing. I tried lots of command did not work. Anaconda Doc I Read all, no solution I have seen.
Please any advice for fix

@karrtikr
Copy link

Upgraded isort. Should be fixed in the next release

@ghost ghost removed the needs PR label Aug 13, 2020
@uksaha
Copy link

uksaha commented Aug 14, 2020 via email

@karrtikr
Copy link

It'll be fixed in the NEXT RELEASE which is scheduled in a month.

@uksaha
Copy link

uksaha commented Sep 8, 2020 via email

@karrtikr
Copy link

karrtikr commented Sep 8, 2020

Fix is already out on insiders. Try the Switch to insiders weekly channel command, which installs the insiders version of the extension.

@madqueendany
Copy link

Removing python.sortImports.path from settings.json fixed the issue for me

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

No branches or pull requests