microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
163.49k stars 28.97k forks source link

Test discovery fails in testing plugin when cairosvg is imported #221262

Closed johanolsen closed 3 months ago

johanolsen commented 3 months ago

Type: Bug

0) Install cairo using brew

brew install cairo

1) Create a blank project with a virtual env and install cairosvg

$ mkdir testfail
$ cd testfail
$ python -m venv .venv
$ source .venv/bin/activate
$ pip install cairosvg

2) Create a module foo.py and import from cairosvg

from cairosvg import svg2png

def bar():
    print("hello world")

3) Create a test module

from foo import bar

def test_bar():
    assert bar() is None

4) Configure testing plugin (I used the UI, but settings.json ended up looking like below)

{
    "python.testing.pytestArgs": [
        "."
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.pytestEnabled": true
}

Result: Testing plugin shows "Error discovering pytest tests (see Output > Python):"

Note, that commenting out from cariosvg import svg2png makes everything work fine. Also note that running pytest from the integrated terminal (with or without the import statement) works fine.

I had a similar issue with the debugger which I was able to fix by adding a launch.json with the following configuration:

{
            "name": "Python Debugger: Current File",
            "python": "/Users/jpko/repos/testfail/.venv/bin/python3",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "env": {
                "DYLD_LIBRARY_PATH": "/opt/homebrew/lib"
}

I would assume that this could be fixed by adding an environment variable to the testing plugin environment, but I have been unable to figure out how to do so.

VS Code version: Code 1.91.0 (Universal) (ea1445cc7016315d0f5728f8e8b12a45dc0a7286, 2024-07-01T18:54:18.541Z) OS version: Darwin arm64 23.5.0 Modes:

System Info |Item|Value| |---|---| |CPUs|Apple M2 (8 x 2400)| |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
webgl: enabled
webgl2: enabled
webgpu: enabled| |Load (avg)|2, 1, 1| |Memory (System)|24.00GB (0.78GB free)| |Process Argv|--crash-reporter-id 80eca0c6-e682-4dd4-8442-1d8440cd4049| |Screen Reader|no| |VM|0%|
Extensions (33) Extension|Author (truncated)|Version ---|---|--- vscode-sqlite|ale|0.14.1 markdown-mermaid|bie|1.23.1 gitlens|eam|15.1.0 copilot|Git|1.211.0 copilot-chat|Git|0.17.0 gitlab-workflow|Git|4.19.0 rainbow-csv|mec|3.12.0 vscode-docker|ms-|1.29.1 autopep8|ms-|2024.0.0 black-formatter|ms-|2024.2.0 debugpy|ms-|2024.6.0 isort|ms-|2023.10.1 python|ms-|2024.10.0 vscode-pylance|ms-|2024.7.1 jupyter|ms-|2024.6.0 jupyter-keymap|ms-|1.1.2 jupyter-renderers|ms-|1.0.18 vscode-jupyter-cell-tags|ms-|0.1.9 vscode-jupyter-slideshow|ms-|0.1.6 remote-containers|ms-|0.375.1 remote-ssh|ms-|0.112.0 remote-ssh-edit|ms-|0.86.0 remote-wsl|ms-|0.88.2 vscode-remote-extensionpack|ms-|0.25.0 remote-explorer|ms-|0.4.3 remote-server|ms-|1.5.2 sqltools|mtx|0.28.3 autodocstring|njp|0.6.1 vscode-xml|red|0.27.1 win-ca|uko|3.5.1 jmp-scripting|Vin|0.17.0 vim|vsc|1.27.3 markdown-pdf|yza|1.5.0 (1 theme extensions excluded)
jpkolsen commented 3 months ago

Forgot to add the traceback from the Python output:

024-07-09 12:48:39.050 [info] Discover tests for workspace name: testfail - uri: /Users/jpko/repos/testfail
2024-07-09 12:48:41.718 [info] > pixi --version
2024-07-09 12:48:41.719 [error] Error: spawn pixi ENOENT
    at Process.ChildProcess._handle.onexit (node:internal/child_process:286:19)
    at onErrorNT (node:internal/child_process:484:16)
    at processTicksAndRejections (node:internal/process/task_queues:82:21) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'spawn pixi',
  path: 'pixi',
  spawnargs: [ '--version' ]
}
2024-07-09 12:48:41.719 [warning] could not find a pixi interpreter for the interpreter at /Users/jpko/repos/testfail/.venv/bin/python
2024-07-09 12:48:41.725 [info] > . ./.venv/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/printEnvVariables.py
2024-07-09 12:48:41.725 [info] shell: bash
2024-07-09 12:48:41.757 [warning] could not find a pixi interpreter for the interpreter at /Users/jpko/repos/testfail/.venv/bin/python
2024-07-09 12:48:41.758 [info] > ./.venv/bin/python -c "import pytest"
2024-07-09 12:48:42.393 [info] Discover tests for workspace name: testfail - uri: /Users/jpko/repos/testfail/.vscode/settings.json
2024-07-09 12:48:42.394 [warning] could not find a pixi interpreter for the interpreter at /Users/jpko/repos/testfail/.venv/bin/python
2024-07-09 12:48:42.396 [info] > . ./.venv/bin/activate && echo 'e8b39361-0157-4923-80e1-22d70d46dee6' && python ~/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/printEnvVariables.py
2024-07-09 12:48:42.396 [info] shell: bash
2024-07-09 12:48:42.417 [warning] could not find a pixi interpreter for the interpreter at /Users/jpko/repos/testfail/.venv/bin/python
2024-07-09 12:48:42.418 [info] > ./.venv/bin/python ~/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear .
2024-07-09 12:48:42.418 [info] cwd: .
2024-07-09 12:48:42.637 [error] Error discovering pytest tests:
 n [Error]: ============================= test session starts ==============================
platform darwin -- Python 3.12.3, pytest-8.2.2, pluggy-1.5.0
rootdir: /Users/jpko/repos/testfail
collected 0 items / 1 error

==================================== ERRORS ====================================
_________________________ ERROR collecting test_foo.py _________________________
test_foo.py:1: in <module>
    from foo import bar
foo.py:1: in <module>
    from cairosvg import svg2png
.venv/lib/python3.12/site-packages/cairosvg/__init__.py:26: in <module>
    from . import surface  # noqa isort:skip
.venv/lib/python3.12/site-packages/cairosvg/surface.py:9: in <module>
    import cairocffi as cairo
.venv/lib/python3.12/site-packages/cairocffi/__init__.py:60: in <module>
    cairo = dlopen(
.venv/lib/python3.12/site-packages/cairocffi/__init__.py:57: in dlopen
    raise OSError(error_message)  # pragma: no cover
E   OSError: no library called "cairo-2" was found
E   no library called "cairo" was found
E   no library called "libcairo-2" was found
E   cannot load library 'libcairo.so.2': dlopen(libcairo.so.2, 0x0002): tried: 'libcairo.so.2' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo.so.2' (no such file), '/usr/lib/libcairo.so.2' (no such file, not in dyld cache), 'libcairo.so.2' (no such file), '/usr/local/lib/libcairo.so.2' (no such file), '/usr/lib/libcairo.so.2' (no such file, not in dyld cache).  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.so.2'
E   cannot load library 'libcairo.2.dylib': dlopen(libcairo.2.dylib, 0x0002): tried: 'libcairo.2.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo.2.dylib' (no such file), '/usr/lib/libcairo.2.dylib' (no such file, not in dyld cache), 'libcairo.2.dylib' (no such file), '/usr/local/lib/libcairo.2.dylib' (no such file), '/usr/lib/libcairo.2.dylib' (no such file, not in dyld cache).  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.2.dylib'
E   cannot load library 'libcairo-2.dll': dlopen(libcairo-2.dll, 0x0002): tried: 'libcairo-2.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo-2.dll' (no such file), '/usr/lib/libcairo-2.dll' (no such file, not in dyld cache), 'libcairo-2.dll' (no such file), '/usr/local/lib/libcairo-2.dll' (no such file), '/usr/lib/libcairo-2.dll' (no such file, not in dyld cache).  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo-2.dll'
=========================== short test summary info ============================
ERROR test_foo.py - OSError: no library called "cairo-2" was found
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
===================== no tests collected, 1 error in 0.11s =====================

Traceback (most recent call last):
  File "/Users/jpko/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/Users/jpko/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/testing_tools/adapter/__main__.py", line 97, in main
    parents, result = run(toolargs, **subargs)
                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jpko/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/testing_tools/adapter/pytest/_discovery.py", line 47, in discover
    raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 2)

    at ChildProcess.<anonymous> (/Users/jpko/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/out/client/extension.js:2:228878)
    at Object.onceWrapper (node:events:629:26)
    at ChildProcess.emit (node:events:514:28)
    at maybeClose (node:internal/child_process:1105:16)
    at Socket.<anonymous> (node:internal/child_process:457:11)
    at Socket.emit (node:events:514:28)
    at Pipe.<anonymous> (node:net:337:12)
2024-07-09 12:48:42.698 [info] Discover tests for workspace name: testfail - uri: /Users/jpko/repos/testfail
2024-07-09 12:48:42.698 [warning] could not find a pixi interpreter for the interpreter at /Users/jpko/repos/testfail/.venv/bin/python
2024-07-09 12:48:42.699 [info] > ./.venv/bin/python ~/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/testing_tools/run_adapter.py discover pytest -- --rootdir . -s --cache-clear .
2024-07-09 12:48:42.699 [info] cwd: .
2024-07-09 12:48:42.903 [error] Error discovering pytest tests:
 n [Error]: ============================= test session starts ==============================
platform darwin -- Python 3.12.3, pytest-8.2.2, pluggy-1.5.0
rootdir: /Users/jpko/repos/testfail
collected 0 items / 1 error

==================================== ERRORS ====================================
_________________________ ERROR collecting test_foo.py _________________________
test_foo.py:1: in <module>
    from foo import bar
foo.py:1: in <module>
    from cairosvg import svg2png
.venv/lib/python3.12/site-packages/cairosvg/__init__.py:26: in <module>
    from . import surface  # noqa isort:skip
.venv/lib/python3.12/site-packages/cairosvg/surface.py:9: in <module>
    import cairocffi as cairo
.venv/lib/python3.12/site-packages/cairocffi/__init__.py:60: in <module>
    cairo = dlopen(
.venv/lib/python3.12/site-packages/cairocffi/__init__.py:57: in dlopen
    raise OSError(error_message)  # pragma: no cover
E   OSError: no library called "cairo-2" was found
E   no library called "cairo" was found
E   no library called "libcairo-2" was found
E   cannot load library 'libcairo.so.2': dlopen(libcairo.so.2, 0x0002): tried: 'libcairo.so.2' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo.so.2' (no such file), '/usr/lib/libcairo.so.2' (no such file, not in dyld cache), 'libcairo.so.2' (no such file), '/usr/local/lib/libcairo.so.2' (no such file), '/usr/lib/libcairo.so.2' (no such file, not in dyld cache).  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.so.2'
E   cannot load library 'libcairo.2.dylib': dlopen(libcairo.2.dylib, 0x0002): tried: 'libcairo.2.dylib' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo.2.dylib' (no such file), '/usr/lib/libcairo.2.dylib' (no such file, not in dyld cache), 'libcairo.2.dylib' (no such file), '/usr/local/lib/libcairo.2.dylib' (no such file), '/usr/lib/libcairo.2.dylib' (no such file, not in dyld cache).  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo.2.dylib'
E   cannot load library 'libcairo-2.dll': dlopen(libcairo-2.dll, 0x0002): tried: 'libcairo-2.dll' (no such file), '/System/Volumes/Preboot/Cryptexes/OSlibcairo-2.dll' (no such file), '/usr/lib/libcairo-2.dll' (no such file, not in dyld cache), 'libcairo-2.dll' (no such file), '/usr/local/lib/libcairo-2.dll' (no such file), '/usr/lib/libcairo-2.dll' (no such file, not in dyld cache).  Additionally, ctypes.util.find_library() did not manage to locate a library called 'libcairo-2.dll'
=========================== short test summary info ============================
ERROR test_foo.py - OSError: no library called "cairo-2" was found
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
===================== no tests collected, 1 error in 0.11s =====================

Traceback (most recent call last):
  File "/Users/jpko/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/testing_tools/run_adapter.py", line 22, in <module>
    main(tool, cmd, subargs, toolargs)
  File "/Users/jpko/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/testing_tools/adapter/__main__.py", line 97, in main
    parents, result = run(toolargs, **subargs)
                      ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/jpko/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/python_files/testing_tools/adapter/pytest/_discovery.py", line 47, in discover
    raise Exception("pytest discovery failed (exit code {})".format(ec))
Exception: pytest discovery failed (exit code 2)

    at ChildProcess.<anonymous> (/Users/jpko/.vscode/extensions/ms-python.python-2024.10.0-darwin-arm64/out/client/extension.js:2:228878)
    at Object.onceWrapper (node:events:629:26)
    at ChildProcess.emit (node:events:514:28)
    at maybeClose (node:internal/child_process:1105:16)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:305:5)
eleanorjboyd commented 3 months ago

Hi! We are actively working on this problem to create a way to add in environment variables to testing. You can see our proposal here: https://github.com/microsoft/vscode-python/issues/21845. This means your issue is a duplicate of https://github.com/microsoft/vscode-python/issues/17171 so I am going to close it against that issue but know this is a area in development! Thanks