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

Use of PyPy crashed iSort server #291

Closed DrPaulFermor closed 1 year ago

DrPaulFermor commented 1 year ago

Type: Bug

I'm trying to evaluate different Python environments within VSCode, specifically CPython 3.11, CPython 3.9.16 and PyPy (v 7.3.11). The aim is to create desired environments in conda, and use these against a target code base within VSCode.

First, one can create a simple piece of test code which is anticipated to highlight the benefits of PyPy vs CPython:

from time import perf_counter from platform import python_version

print(python_version())

total = 0 st = perf_counter() for i in range(int(1E3)): for j in range(int(1E3)): total += (i + j)

fin = perf_counter()

Creating conda environments for CPython:

conda create --name python=

and subsequently selecting an interpreter for those environments in VSCode enables one to "Run..." and/or "Debug.." and hit set breakpoints in the test code and retrieve output in the terminal with each respective CPython environment.

(base) D:>C:/tools/miniconda3/python.exe d:/demo.py 3.9.16 Task took: 0.23066479999999998secs

(base) D:>C:/tools/miniconda3/envs/py-311/python.exe d:/demo.py 3.11.3 Task took: 0.19787699996959418secs

One can then create a PyPy environment following the advice from PyPy (https://www.pypy.org/posts/2022/11/pypy-and-conda-forge.html):

conda create -c conda-forge -n my-pypy-env pypy python=3.8

This creates a PyPy environment successfully.

Selecting the PyPy interpreter environment in VSCode immediately yields an error:

"The isort server crashed 5 times in the last 3 minutes. The server will not be restarted"

The iSort output yields:

[Info - 4:50:39 PM] Connection to server got closed. Server will restart. [Info - 4:50:39 PM] Connection to server got closed. Server will restart. [Info - 4:50:39 PM] Connection to server got closed. Server will restart. [Info - 4:50:39 PM] Connection to server got closed. Server will restart. [Error - 4:50:40 PM] Connection to server got closed. Server will not be restarted.

Subsequently trying to "Debug..." with the PyPy environment selected completely fails...literally nothing happens and seemingly nothing can be done to change this

If one tries to "Run..." when a CPython environment is active in the terminal, nothing happens:

(py-311) D:>C:/tools/miniconda3/envs/pypy-env/python.exe d:/demo.py

(py-311) D:>

If one subsequently forces activation of the pypy-env in the terminal, and invoke "Run...", the code in the PyPy environment executes:

(base) D:>conda activate pypy-env

(pypy-env) D:>C:/tools/miniconda3/envs/pypy-env/python.exe d:/demo.py 3.8.16 Task took: 0.0072821secs

(pypy-env) D:>

However, one can happily run CPython interpreters when a PyPy environment is activated in the terminal:

(pypy-env) D:>C:/tools/miniconda3/envs/py-311/python.exe d:/demo.py 3.11.3 Task took: 0.19779740006197244secs

(pypy-env) D:>

It seems apparent that VSCode and iSort server are operating correctly when CPython environments are used. Selecting PyPy environments clearly compromises the iSort server which then in turn appears to compromise VSCode "Run Python File..." and "Debug Python File..." behaviour.

I'd love to get to the bottom of this

Extension version: 2022.8.0 VS Code version: Code 1.79.2 (695af097c7bd098fbf017ce3ac85e09bbc5dda06, 2023-06-14T08:57:04.379Z) OS version: Windows_NT x64 10.0.19045 Modes:

System Info |Item|Value| |---|---| |CPUs|Intel(R) Core(TM) i7-3930K CPU @ 3.20GHz (12 x 3202)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
direct_rendering_display_compositor: disabled_off_ok
gpu_compositing: enabled
multiple_raster_threads: enabled_on
opengl: enabled_on
rasterization: enabled
raw_draw: disabled_off_ok
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|undefined| |Memory (System)|31.97GB (22.98GB free)| |Process Argv|--crash-reporter-id e4305c4e-7a51-451c-9c6e-afb5bd6a57a2| |Screen Reader|no| |VM|0%|
A/B Experiments ``` vsliv368:30146709 vsreu685:30147344 python383:30185418 vspor879:30202332 vspor708:30202333 vspor363:30204092 vslsvsres303:30308271 vserr242:30382549 pythontb:30283811 vsjup518:30340749 pythonptprofiler:30281270 vshan820:30294714 vstes263:30335439 vscorecescf:30445987 vscod805:30301674 binariesv615:30325510 bridge0708:30335490 bridge0723:30353136 vsaa593:30376534 pythonvs932:30410667 vsclangdf:30486550 c4g48928:30535728 dsvsc012:30540252 pynewext54:30695312 azure-dev_surveyone:30548225 3biah626:30602489 pyind779:30671433 f6dab269:30613381 pythonsymbol12:30671437 a9j8j154:30646983 showlangstatbar:30737416 vsctsb:30748421 pythonms35:30701012 a2ce3375:30757347 57b77579:30736110 pythonfmttext:30731395 pythoncmv:30756943 fixshowwlkth:30771522 pythongtdpath:30769146 dh2dc718:30770000 pythonidxpt:30772539 pythondjangots:30772535 h7j2d465:30772216 ```
mattip commented 1 year ago

My uneducated guess is that there is some incompatibility in the way the code is being serialized and passed to the isort-server process. Does anything else (like code completion) work after selecting PyPy as the interpreter?

DrPaulFermor commented 1 year ago

I just double checked - iSort fails but code completion still works. "Run Python..." works - "Debug Python..." categorically does not.

image

karthiknadig commented 1 year ago

This seems to be the error that we get when we attempt to write to the server:

Error: write EPIPE
    at afterWriteDispatched (node:internal/stream_base_commons:160:15)
    at writeGeneric (node:internal/stream_base_commons:151:3)
    at Socket._writeGeneric (node:net:874:11)
    at Socket._write (node:net:886:8)
    at writeOrBuffer (node:internal/streams/writable:391:12)
    at _write (node:internal/streams/writable:332:10)
    at Writable.write (node:internal/streams/writable:336:10)
    at c:\GIT\formatters\vscode-black-formatter\dist\extension.js:5485:29
    at new Promise (<anonymous>)
    at WritableStreamWrapper.write (c:\GIT\formatters\vscode-black-formatter\dist\extension.js:5475:16)
    at StreamMessageWriter.doWrite (c:\GIT\formatters\vscode-black-formatter\dist\extension.js:7160:33)
    at c:\GIT\formatters\vscode-black-formatter\dist\extension.js:7151:29
karthiknadig commented 1 year ago

I think I know what is the problem, if you run the server from a non activated environment you get this error: image

Can you try running VS Code from an activated environment?

  1. Navigate to your project from the terminal
  2. Activate conda environment
  3. Then run code . from the activated environment.
karthiknadig commented 1 year ago

This issue occurs because VS Code itself is not in an activated environment, only the VS Code terminal is activated by the python extension. A workaround is to run VS Code from an activated environment.

mattip commented 1 year ago

Ahh, so this is related to conda-forge/pypy3.6-feedstock#101. A quick and easy solution would be to copy the three DLLs into the same directory as the libpypy dll but the conda-forge maintainers didn't give me a green light to do that. Instead there is a complicated patch I could do to the feedstock. Could anyone interested comment on the issue so we can prioritized moving it forward?

karthiknadig commented 1 year ago

Closing this since this is external.