Skip to content

pytest discovery does not use specified environment #25718

@wdawson

Description

@wdawson

Type: Bug

Behaviour

In a monorepo with a Python project in a subdirectory that has its own venv, the Python extension fails to discover tests because the pytest module cannot be found. This appears to be because the test discovery process is attempting to use the system Python interpreter instead of the one specified and activated in the extensions.

Based on the output logs (see Diagnostic Data below), I suspect this is caused by some sort of race between the Environments extension and the Python extension, but I am not sure.

I would have expected test discovery to use the environment specified in each project and aggregate them, but it appears to use only the root-level project and error when it fails.

Steps to reproduce:

  1. Have some system python installed and available on your PATH WITHOUT pytest installed
  2. Clone the example repo
  3. Create a python environment in the apps/example-py directory (e.g. cd apps/example-py && uv sync)
  4. Install the Python extension and Python Environments extension (and opt-in to the latter)
  5. Open a python file like main.py from the repo in the editor
  6. Ensure you see the selected interpreter as the venv one — you may need to configure the apps/example-py directory as a Python Project for the environments extension, though that should be taken care of in the settings.json
  7. Expected test panel loads correctly | Actual error logs
Image Image

Environments

Image

Diagnostic data

Output for Python in the Output panel (ViewOutput, change the drop-down the upper-right of the Output panel to Python)

2026-01-08 09:32:24.724 [info] Native locator: Refresh started
2026-01-08 09:32:24.801 [info] > pyenv which python
2026-01-08 09:32:24.801 [info] cwd: .
2026-01-08 09:32:24.858 [info] Active interpreter [/Users/wils/dev/monorepo-example]:  /Users/wils/dev/monorepo-example/apps/example-py/.venv/bin/python
2026-01-08 09:32:24.866 [info] Native locator: Refresh finished in 142 ms
2026-01-08 09:32:24.997 [info] Active interpreter [/Users/wils/dev/monorepo-example]:  /opt/homebrew/bin/python3
2026-01-08 09:32:24.997 [info] Editor support is inactive since language server is set to None.
2026-01-08 09:32:25.091 [info] Environment variables set for pytest discovery: PYTHONPATH=/Users/wils/.vscode/extensions/ms-python.python-2025.20.1-darwin-arm64/python_files, TEST_RUN_PIPE=/var/folders/bc/h0znscsx3xlf1qhy1sd73hhc0000gn/T/python-test-discovery-040a5a86b8a624012118
2026-01-08 09:32:25.091 [info] Using environment extension for pytest discovery in workspace /Users/wils/dev/monorepo-example
2026-01-08 09:32:25.092 [info] Started pytest discovery subprocess (environment extension) for workspace /Users/wils/dev/monorepo-example
2026-01-08 09:32:25.096 [info] Editor support is inactive since language server is set to None.
2026-01-08 09:32:25.096 [info] Editor support is inactive since language server is set to None.
2026-01-08 09:32:25.368 [info] Active interpreter [/Users/wils/dev/monorepo-example]:  /Users/wils/dev/monorepo-example/apps/example-py/.venv/bin/python
2026-01-08 09:32:25.369 [error] Test discovery already in progress, not starting a new one.
2026-01-08 09:32:25.390 [error] /opt/homebrew/opt/python@3.14/bin/python3.14: No module named pytest

2026-01-08 09:32:25.394 [error] pytest discovery failed with exit code 1 and signal null for workspace /Users/wils/dev/monorepo-example. Creating error payload.
2026-01-08 09:32:25.394 [error] pytest test discovery error for workspace:  /Users/wils/dev/monorepo-example 
  
 The python test process was terminated before it could exit on its own, the process errored with: Code: 1, Signal: null for workspace /Users/wils/dev/monorepo-example
2026-01-08 09:32:25.394 [info] Pytest discovery completed for workspace /Users/wils/dev/monorepo-example
2026-01-08 09:34:03.303 [info] Editor support is inactive since language server is set to None.
2026-01-08 09:34:03.550 [error] Unable to find workspace for given file

Extension version: 2025.20.1
VS Code version: Code 1.107.1 (Universal) (994fd12f8d3a5aa16f17d42c041e5809167e845a, 2025-12-17T14:15:14.850Z)
OS version: Darwin arm64 24.6.0
Modes:

  • Python version (& distribution if applicable, e.g. Anaconda): 3.14.2.final.0
  • Type of virtual environment used (e.g. conda, venv, virtualenv, etc.): System
  • Value of the python.languageServer setting: None
User Settings


languageServer: "None"

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

Installed Extensions
Extension Name Extension Id Version
ayu tea 1.1.7
better-kill-ring wda 1.0.0
biome bio 1.6.2
code-spell-checker str 4.4.0
copilot-chat Git 0.35.3
debugpy ms- 2025.18.0
EditorConfig Edi 0.17.4
js-debug ms- 1.105.0
js-debug-companion ms- 1.1.3
makefile-tools ms- 0.12.17
prettier-vscode esb 11.0.2
python ms- 2025.20.1
ruff cha 2025.32.0
strict-whitespace sid 0.2.0
ty ast 2026.2.0
vscode-js-profile-table ms- 1.0.10
vscode-python-envs ms- 1.14.0
vscode-yaml red 1.19.1
System Info
Item Value
CPUs Apple M4 Max (14 x 2400)
GPU Status 2d_canvas: enabled
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: enabled_on
trees_in_viz: disabled_off
video_decode: enabled
video_encode: enabled
webgl: enabled
webgl2: enabled
webgpu: enabled
webnn: disabled_off
Load (avg) 4, 4, 4
Memory (System) 36.00GB (0.07GB free)
Process Argv --crash-reporter-id 4f906965-5506-45c9-b6ac-f758854435af
Screen Reader no
VM 0%
A/B Experiments
vsliv368:30146709
binariesv615:30325510
nativeloc1:31344060
dwcopilot:31170013
dwoutputs:31242946
copilot_t_ci:31333650
e5gg6876:31282496
pythonrdcb7:31342333
6518g693:31436602
aj953862:31281341
b6b4d950:31327385
6abeh943:31336334
envsdeactivate2:31353495
cloudbuttont:31379625
3efgi100_wstrepl:31403338
use-responses-api:31390855
anonusage:31403420
je187915:31401257
ec5jj548:31422691
terminalsuggestenabled:31431119
cmp-ext-treat:31426748
cp_cls_c_966_ss:31442353
c0683394:31419495
3gif2411:31435631
478ah919:31426797
ge8j1254_inline_auto_hint_haiku:31431912
a5gib710:31434435
5j92g670_sonnet:31434465
no_request_with_suggest:31435826
rename_enabled:31436409
7a04d226_do_not_restore_last_panel_session:31438103
anthropic_thinking_t:31432745
cp_cls_t_1081:31443828
copilot-nes-oct-trt:31432596
nes-jump-on-with-edit:31442062
nes-slash-models-on:31441832
nes-set-to-on:31441941
a43f0576c:31442824

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIssue identified by VS Code Team member as probable bugimportantIssue identified as high-priorityneeds PRReady to be worked on

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions