Skip to content

VSCode pytest data from WSL generate an error when processed #24811

Closed as not planned

Description

@KinkosPouet

Type: Bug

Behaviour

Pytesting through UI doesn't work with WSL extension. An error occures when processing data sent from WSL to VScode :

Steps to reproduce:

  1. Create environnement with hatch (
    hatch env run -e dev test)
  2. Select interpreter in VSCode to point on the venv dir ( hatc
    h env find dev)
  3. Go to testing in VSCode, and play the test.
  4. Test pass without error, but the data is not sent back
Output for Python in the Output panel

Running pytest with args: ['-p', 'vscode_pytest', '--rootdir=/home/user/git/', '/home/user/git/app/tests/test_main.py::test_load_config', '/home/user/git/app/tests/test_main.py::test_get_db_connection_postgresql', '/home/user/git/app/tests/test_main.py::test_get_db_connection_mysql', '/home/user/git/app/tests/test_main.py::test_is_database_empty_postgresql', '/home/user/git/app/tests/test_main.py::test_is_database_empty_mysql', '/home/user/git/app/tests/test_main.py::test_execute_sql_file', '/home/user/git/app/tests/test_main.py::test_process_database_when_empty', '/home/user/git/app/tests/test_main.py::test_process_database_exception', '/home/user/git/app/tests/test_main.py::test_main_process_multiple_databases']
============================= test session starts ==============================
platform linux -- Python 3.11.2, pytest-7.4.3, pluggy-1.5.0
rootdir: /home/user/git/
plugins: mock-3.14.0
collected 9 items

app/tests/test_main.py Plugin error, exception thrown while attempting to send data[vscode-pytest]: unsupported operand type(s) for +=: 'int' and 'NoneType' 
[vscode-pytest] data: 
{"jsonrpc": "2.0", "params": {"cwd": "/home/user/git/", "status": "success", "result": {"/home/user/git/app/tests/test_main.py::test_load_config": {"test": "/home/user/git/app/tests/test_main.py::test_load_config", "outcome": "success", "message": null, "traceback": null, "subtest": null}}, "not_found": null, "error": null}}

(X9)

.           [100%]

============================== 9 passed in 0.05s ===============================
Finished running tests!

Extension version: 2025.0.0
VS Code version: Code 1.97.0 (33fc5a94a3f99ebe7087e8fe79fbe1d37a251016, 2025-02-04T22:41:26.688Z)
OS version: Windows_NT x64 10.0.26100
Modes:
Remote OS version: Linux x64 5.15.167.4-microsoft-standard-WSL2
Remote OS version: Linux x64 5.15.167.4-microsoft-standard-WSL2
Remote OS version: Linux x64 5.15.167.4-microsoft-standard-WSL2

  • Python version (& distribution if applicable, e.g. Anaconda): 3.11.2
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): Venv
  • Value of the python.languageServer setting: Default
User Settings


languageServer: "Pylance"

testing
• pytestArgs: "<placeholder>"
• pytestEnabled: true

Installed Extensions
Extension Name Extension Id Version
debugpy ms- 2025.0.0
gitlens eam 16.3.0
js-debug ms- 1.97.0
python ms- 2025.0.0
vscode-js-profile-table ms- 1.0.10
vscode-pylance ms- 2025.2.1
System Info
Item Value
CPUs Intel(R) Core(TM) Ultra 9 185H (22 x 3072)
GPU Status 2d_canvas: enabled
canvas_oop_rasterization: enabled_on
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
skia_graphite: disabled_off
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) undefined
Memory (System) 31.46GB (12.42GB free)
Process Argv --crash-reporter-id bb4a69d2-a4d3-44a5-b62b-19c8f9b82470
Screen Reader no
VM 0%
Item Value
Remote WSL: Debian
OS Linux x64 5.15.167.4-microsoft-standard-WSL2
CPUs Intel(R) Core(TM) Ultra 9 185H (22 x 0)
Memory (System) 15.35GB (13.51GB free)
VM 0%
Item Value
Remote WSL: Debian
OS Linux x64 5.15.167.4-microsoft-standard-WSL2
CPUs Intel(R) Core(TM) Ultra 9 185H (22 x 0)
Memory (System) 15.35GB (13.51GB free)
VM 0%
Item Value
Remote WSL: Debian
OS Linux x64 5.15.167.4-microsoft-standard-WSL2
CPUs Intel(R) Core(TM) Ultra 9 185H (22 x 0)
Memory (System) 15.35GB (13.51GB free)
VM 0%
A/B Experiments
vsliv368cf:30146710
vspor879:30202332
vspor708:30202333
vspor363:30204092
vscod805cf:30301675
binariesv615:30325510
py29gd2263:31024239
c4g48928:30535728
azure-dev_surveyone:30548225
962ge761:30959799
2e7ec940:31000449
pythontbext0:30879054
cppperfnew:31000557
dwnewjupytercf:31046870
nativerepl1:31139838
pythonrstrctxt:31112756
nativeloc1:31192215
iacca1:31171482
5fd0e150:31155592
dwcopilot:31170013
stablechunks:31184530
6074i472:31201624
9064b325:31222308
copilot_t_ci:31222730
jda6j935:31233686

Activity

  1. added
    triage-needed
    on Feb 12, 2025
  2. eleanorjboyd commented on Feb 14, 2025

    @eleanorjboyd

    hm looks like it is this line causing the issue bytes_written += __writer.write(segment) from here. Not sure why looks like there is just a problem writing to the named pipe that is open for communication. Karthik Nadig (@karthiknadig) any ideas here specifically since this is coming from WSL? I can also add in some more logging, potentially the name-pipe vs use of fifo is being incorrectly selected on WSL?

  3. added
    info-needed
    on Feb 14, 2025
  4. martin-s-a commented on Feb 18, 2025

    @martin-s-a

    I'm having a very similar issue since fairly recently. I haven't isolated the specific plugin version, but, in my specific case, seems to happen when running (individually and from UI) unitary tests that make use of mock_open, which makes sense if the involved lines contain an open() call.

    Some time ago I opened issue #24399, but the behaviour described in that issue seems to have been replaced by the one described here.

  5. KinkosPouet commented on Feb 18, 2025

    @KinkosPouet
    Author

    FYI i did this on a brand new environnement on a new computer.
    My code didn't change between my migration from old computer to new one.
    I Installed WSL + got my code from my git repo without any changes.

  6. removed
    info-needed
    on Feb 18, 2025
  7. karthiknadig commented on Feb 18, 2025

    @karthiknadig
    Member

    Eleanor Boyd (@eleanorjboyd) We need to repro this locally to see what is the error. It is likely a path issue, if the paths are /mnt/c/ style then the vs /tmp then there could be a difference in how we communicate with those. We need to see the logs on how the paths look like. we need to make sure that in WSL scenarios the path does not use Windows mounted folders for pipes.

  8. added
    info-needed
    on Feb 18, 2025
  9. KinkosPouet commented on Feb 18, 2025

    @KinkosPouet
    Author

    Karthik Nadig (@karthiknadig) To add some information, in my case, every python files used were in /home/user/, nothing was put in /mnt/c.

  10. removed
    info-needed
    on Feb 18, 2025
  11. karthiknadig commented on Feb 18, 2025

    @karthiknadig
    Member

    The issue is not with the python files themselves, we use pipes to communicate between VS Code and pytest. When we create the pipes, in WSL they should be created in /tmp and not in any location that is /mnt/. My concern is that it might be creating it in the wrong location, leading to a bad pipe stream.

  12. added
    info-needed
    on Feb 18, 2025
  13. github-actions commented on Mar 21, 2025

    @github-actions

    Because we have not heard back with the information we requested, we are closing this issue for now. If you are able to provide the info later on, then we will be happy to re-open this issue to pick up where we left off.

    Happy Coding!

  14. 1 remaining item

  15. added
    bug
    needs spike
    and removed
    info-needed
    triage-needed
    on Mar 21, 2025
  16. Matheus920 commented on Mar 22, 2025

    @Matheus920

    I'm having the same issue, and in my case, I'm not using WSL but a Devcontainer that's running on my MacOS.

  17. zvonimir-delas commented on Mar 24, 2025

    @zvonimir-delas

    I'm having the same issue, and in my case, I'm not using WSL but a Devcontainer that's running on my MacOS.

    +1. Can also confirm removing mock_open circumvents the issue

  18. eleanorjboyd commented on Mar 31, 2025

    @eleanorjboyd

    Could others speak on if changing mock_open fixes there issue? The way we communicate uses open to write to a named-pipe so the way the communication is done could be conflicting with your intended test behavior

  19. technimakri commented on Apr 8, 2025

    @technimakri

    I had the same error message on macOS.: send data[vscode-pytest]: unsupported operand type(s) for +=: 'int' and 'NoneType'.

    It was caused by mocking the standard library like so:

    mock.patch(
            "builtins.open", mock.mock_open()
        ) as mock_open:
    

    I resolved this by scoping the mock to the file being tested:

    mock.patch(
            "my_module.my_file.open", mock.mock_open()
        ) as mock_open:
    
  20. BallsyWalnuts commented on Oct 31, 2025

    @BallsyWalnuts

    I had the same error message on macOS.: send data[vscode-pytest]: unsupported operand type(s) for +=: 'int' and 'NoneType'.

    It was caused by mocking the standard library like so:

    mock.patch(
            "builtins.open", mock.mock_open()
        ) as mock_open:
    

    I resolved this by scoping the mock to the file being tested:

    mock.patch(
            "my_module.my_file.open", mock.mock_open()
        ) as mock_open:
    

    Confirmed that this worked for me as well.

  21. mackenzie-grimes-noaa commented on Nov 8, 2025

    @mackenzie-grimes-noaa

    Not a problem specific to VSCode (although maybe VSCode could handle it a bit more gracefully). It's a pytest quirk: pytest-dev/pytest#2180

    It seems mocking builtins.open on a module imported by your unit tests causes pytest to throw errors. The above issue was closed by pytest maintainers in 2018, saying it's inherent to how Python imports work and there's nothing they can do about it.

    Honestly my workaround was to replace any builtins.open() activities with a simple custom wrapper function. Then it was easier to assert my wrapper function was called as expected, and I trust open() will work right without testing it.

  22. eleanorjboyd commented on Nov 14, 2025

    @eleanorjboyd

    This is great to know, thank you for explaining! I think with this information and given pytest closed it upstream it is not something we will support at this time and suggest a work-around like what is listed above. Thanks

  23. removed
    needs spike
    on Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-testingbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions