Vscode-python: ${workspaceFolder} and ${env:PYTHONPATH} in PYTHONPATH in env file not resolved during test discovery

Created on 2 Sep 2020  路  2Comments  路  Source: microsoft/vscode-python

Environment data

  • VS Code version: 1.47.0
  • Extension version (available under the Extensions sidebar): v2020.6.91350
  • OS and version: Windows_NT x64 10.0.18362
  • Python version (& distribution if applicable, e.g. Anaconda): 3.6.10 from miniconda
  • Type of virtual environment used (N/A | venv | virtualenv | conda | ...): conda
  • Relevant/affected Python packages and their versions: pytest
  • Relevant/affected Python-related VS Code extensions and their versions: Test
  • Value of the python.languageServer setting: Jedi

Expected behaviour

Placeholders for environmental variables as well as VS Code variables are substituted by their values.

Assume the wide-spread setup with modules in src and tests in tests:

my_project
|- src
|-- module1.py
|- tests
|-- test1.py

Assume the env file (on windows, or same with / and : on linux- also tested there) like

PYTHONPATH = ${workspaceFolder}\src;${env:PYTHONPATH}

This way seems to work in the Debugger.

Expect that the Test Explorer will get the PYTHONPATH properly configured

Actual behaviour

Test discovery fails, while modules can not be found.
trying to debug it and putting the local module import into the test itself the discovery succeeds, but the tests naturally fail on execution due to not found modules in import. Adding a printout of PYTHONPATH before import shows, that the variables are not substituted:

import os
def test_foo():
    print(f"PYTHONPATH = {os.environ['PYTHONPATH']}")
    from module1 import foo
    # dummy assert to always see the printout
    assert foo() != 1

```
---------------------------- Captured stdout call -----------------------------
PYTHONPATH = ${workspaceFolder}src;${env:PYTHONPATH}


if one uses `src` directly in the env file, e.g.:

PYTHONPATH = src;${workspaceFolder}src;${env:PYTHONPATH}
```
, then it works however, this is a different behaviour from the debugger, it also prevents from appending system-defined PYTHONPATH at the end.

Steps to reproduce:

see above

area-testing needs PR reason-preexisting type-bug

Most helpful comment

Hello @mlisovyi, thank you for reaching out.

It is a known issue with test discovery (https://github.com/microsoft/vscode-python/issues/6891#issuecomment-541586895), but we didn't seem to have an open issue to track it, so thank you for opening it! We'll have to fix it when we consolidate how we process environment files.

Thank you!

All 2 comments

Hello @mlisovyi, thank you for reaching out.

It is a known issue with test discovery (https://github.com/microsoft/vscode-python/issues/6891#issuecomment-541586895), but we didn't seem to have an open issue to track it, so thank you for opening it! We'll have to fix it when we consolidate how we process environment files.

Thank you!

@kimadeline thanks for the review of the issue.

I'm not 100% sure it is the same issue as reported in the comment that you linked. I got an impression that there it was reported that .env file is not discovered and that is the root of the problems that the person sees.

In my case i define a custom windows.env and configure VSCode to use it via python.envFile in settings.json. And i see that it is picked up at the test discovery, because in the example printout the PYTHONPATH appears as ${workspaceFolder}\src;${env:PYTHONPATH}.

The problem that i intended to report was that the extension did not do variable substitute before assignment and treated the content of windows.env as plain text.

Was this page helpful?
0 / 5 - 0 ratings