microsoft / vscode-python

Python extension for Visual Studio Code
https://aka.ms/pvsc-marketplace
MIT License
4.28k stars 1.17k forks source link

Debugging test in VS Code does not work #10722

Open asipras opened 4 years ago

asipras commented 4 years ago

Environment data

Version: 1.43.1 (user setup) Commit: fe22a9645b44368865c0ba92e2fb881ff1afce94 Date: 2020-03-18T07:01:20.184Z Electron: 7.1.11 Chrome: 78.0.3904.130 Node.js: 12.8.1 V8: 7.8.279.23-electron.0 OS: Windows_NT x64 10.0.18363

Expected behaviour

Debugging test should work.

Actual behaviour

Debugging test does not work. VS code shows error dialog. See in reproduction steps for more details. If I run the test without debugger the tests runs as expected.

Steps to reproduce:

Try to debug tests using unit test library, Click on the small bug icon for an individual test to debug. I get a dialog box error saying Invalid message: Duplicate entries in "env". The two options i get are Open launch.json and Cancel.

I have checked my lauch.json and I do not have duplicate entries for any env variable.

Logs

ERR timeout after 500 ms: Error: timeout after 500 ms at t.RawDebugSession.handleErrorResponse (file:///C:/Users/AP/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:2954:952) at file:///C:/Users/AP/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:2954:250 at async t.RawDebugSession.shutdown (file:///C:/Users/AP/AppData/Local/Programs/Microsoft VS Code/resources/app/out/vs/workbench/workbench.desktop.main.js:2952:336)

### Settings.json

{
    "python.testing.pytestArgs": [
        "tests"
    ],
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true
}
lorenzo-cavazzi commented 4 years ago

Same Python extension version 2020.3.69010 and the same problem with debugging pytest tests. In my case, I can't see any error log even when selecting Python Test Log as output (Is there another way to see complete logs?). When running the tests without debugging, everything works fine and I can see something logged in the console. For some reason, trying to install a different version of the Python extension doesn't work. Everything used to work until a few days ago when I was using the extension version 2020.2.64397

My system is different, here are the details

Version: 1.42.1
Commit: c47d83b293181d9be64f27ff093689e8e7aed054
Date: 2020-02-17T09:32:31.598Z
Electron: 6.1.9
Chrome: 76.0.3809.146
Node.js: 12.4.0
V8: 7.6.303.31-electron.0
OS: Linux x64 5.5.8-1-MANJARO

I am using pipenv, so I am using venv (the python version selected by VScode is correct).

kimadeline commented 4 years ago

Hello @asipras and @lorenzo-cavazzi, thank you for reaching out.

Can I ask you both to provide the following:

Can you also confirm that if you downgrade to the previous release of the extension (2020.2.64397) you can debug tests correctly:

image

@lorenzo-cavazzi what do you mean by that:

For some reason, trying to install a different version of the Python extension doesn't work.

Thanks!

asipras commented 4 years ago

@kimadeline thanks for looking into this. I did downgrade to the previous version of python extension 2020.2.64397 and now i am able to debug the tests.

I also did some debugging with the latest version of python extension 2020.3.69010. I was able to debug a simple test (does not reference any 3rd party modules numpy, scipy, etc.) by invoking VS code from normal command prompt. However when I invoked VS code using Anaconda prompt/terminal I got the error message mentioned above.

The log from the python output panel.

> ~\Anaconda3\envs\mounthood-dev\python.exe -m site --user-site
cwd: c:\src\Test
> ~\Anaconda3\envs\mounthood-dev\python.exe -m site --user-site
cwd: c:\src\Test
> ~\Anaconda3\envs\mounthood-dev\python.exe completion.py
cwd: c:\Users\asif.prasla\.vscode\extensions\ms-python.python-2020.3.69010\pythonFiles
> ~\Anaconda3\envs\mounthood-dev\python.exe completion.py
cwd: c:\Users\asif.prasla\.vscode\extensions\ms-python.python-2020.3.69010\pythonFiles
> ~\Anaconda3\envs\mounthood-dev\python.exe -m pylint --disable=all --enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,E0001,E0011,E0012,E0100,E0101,E0102,E0103,E0104,E0105,E0107,E0108,E0110,E0111,E0112,E0113,E0114,E0115,E0116,E0117,E0118,E0202,E0203,E0211,E0213,E0236,E0237,E0238,E0239,E0240,E0241,E0301,E0302,E0303,E0401,E0402,E0601,E0602,E0603,E0604,E0611,E0632,E0633,E0701,E0702,E0703,E0704,E0710,E0711,E0712,E1003,E1101,E1102,E1111,E1120,E1121,E1123,E1124,E1125,E1126,E1127,E1128,E1129,E1130,E1131,E1132,E1133,E1134,E1135,E1136,E1137,E1138,E1139,E1200,E1201,E1205,E1206,E1300,E1301,E1302,E1303,E1304,E1305,E1306,E1310,E1700,E1701 --msg-template='{line},{column},{category},{symbol}:{msg}' --reports=n --output-format=text c:\src\Test\tests\test_one.py
cwd: c:\src\Test
> ~\Anaconda3\envs\mounthood-dev\python.exe -m pylint --disable=all --enable=F,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,unused-wildcard-import,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,E0001,E0011,E0012,E0100,E0101,E0102,E0103,E0104,E0105,E0107,E0108,E0110,E0111,E0112,E0113,E0114,E0115,E0116,E0117,E0118,E0202,E0203,E0211,E0213,E0236,E0237,E0238,E0239,E0240,E0241,E0301,E0302,E0303,E0401,E0402,E0601,E0602,E0603,E0604,E0611,E0632,E0633,E0701,E0702,E0703,E0704,E0710,E0711,E0712,E1003,E1101,E1102,E1111,E1120,E1121,E1123,E1124,E1125,E1126,E1127,E1128,E1129,E1130,E1131,E1132,E1133,E1134,E1135,E1136,E1137,E1138,E1139,E1200,E1201,E1205,E1206,E1300,E1301,E1302,E1303,E1304,E1305,E1306,E1310,E1700,E1701 --msg-template='{line},{column},{category},{symbol}:{msg}' --reports=n --output-format=text c:\src\Test\tests\test_one.py
cwd: c:\src\Test
> conda env list
##########Linting Output - pylint##########

--------------------------------------------------------------------

Your code has been rated at 10.00/10 (previous run: 10.00/10, +0.00)

> ~\Anaconda3\envs\mounthood-dev\python.exe c:\Users\asif.prasla\.vscode\extensions\ms-python.python-2020.3.69010\pythonFiles\testing_tools\run_adapter.py discover pytest -- --rootdir c:\src\Test -s --cache-clear tests
cwd: c:\src\Test
> ~\Anaconda3\envs\mounthood-dev\python.exe -c "import notebook"
> ~\Anaconda3\envs\mounthood-dev\python.exe -c "import jupyter"

image

lorenzo-cavazzi commented 4 years ago

@kimadeline thanks for taking the time to look into this.

First, I updated my system and VScode to be on the latest version, these are the info

Version: 1.43.1
Commit: fe22a9645b44368865c0ba92e2fb881ff1afce94
Date: 2020-03-21T21:58:41.223Z
Electron: 7.1.14
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Linux x64 5.5.11-1-MANJARO

This is my settings.json. The virtualenv is created through pipenv

{
    "python.linting.pylintEnabled": false,
    "python.linting.flake8Enabled": true,
    "python.linting.enabled": true,
    "python.pythonPath": "/home/lorenzo/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python",
    "python.testing.unittestEnabled": false,
    "python.testing.nosetestsEnabled": false,
    "python.testing.pytestEnabled": true,
    "python.formatting.provider": "yapf"
}

This is a repo that gives me issues when trying to debug tests https://github.com/SwissDataScienceCenter/renku-python Let's say I try to debug test_init_repository. When I click on Run Test, the test is marked as succesfull and the Python output panel is

> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -m pytest --rootdir ~/development/sdsc/renku-python --junitxml=/tmp/tmp-3682KwykJulSvoSV.xml ./tests/core/management/test_repository.py::test_init_repository
cwd: ~/development/sdsc/renku-python

When clicking on Debug Test with a breakpoint anywhere in the code, let's say at line 27, the debug shorty start (I can see the buttons "play", "stop", ... on the top) but it stops after 1 or 2 seconds without any warning. There is no output at all in the Python output panel.

Not sure if this is useful, but the initial output I see in the Python output panel after starting VScode is the following

User belongs to experiment group 'ShowPlayIcon - start'
User belongs to experiment group 'ShowExtensionSurveyPrompt - control'
User belongs to experiment group 'DebugAdapterFactory - experiment'
User belongs to experiment group 'PtvsdWheels37 - experiment'
User belongs to experiment group 'Reload - control'
User belongs to experiment group 'LS - control'
User belongs to experiment group 'UseTerminalToGetActivatedEnvVars - control'
User belongs to experiment group 'WebHostNotebook - control'
User belongs to experiment group 'CollectLSRequestTiming - control'
User belongs to experiment group 'CollectNodeLSRequestTiming - experiment'
> conda --version
> pyenv root
> python3.7 -c "import sys;print(sys.executable)"
> python3.6 -c "import sys;print(sys.executable)"
> python3 -c "import sys;print(sys.executable)"
> python2 -c "import sys;print(sys.executable)"
> python -c "import sys;print(sys.executable)"
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -c "import sys;print(sys.executable)"
> conda info --json
> pipenv --version
cwd: ~/development/sdsc/renku-python
> pipenv --version
cwd: ~/development/sdsc/renku-python
> pipenv --venv
cwd: ~/development/sdsc/renku-python
> pipenv --venv
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-notebooks-4n1SxYW5/bin/python -c "import notebook"
> ~/.local/share/virtualenvs/renku-notebooks-4n1SxYW5/bin/python -c "import jupyter"
> pipenv --py
cwd: ~/development/sdsc/renku-python
> pipenv --py
cwd: ~/development/sdsc/renku-python
Starting Jedi Python language engine.
> ~/.local/share/virtualenvs/renku-notebooks-4n1SxYW5/bin/python -m jupyter kernelspec --version
> conda --version
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python ~/.vscode-oss/extensions/ms-python.python-2020.3.69010/pythonFiles/testing_tools/run_adapter.py discover pytest -- --rootdir ~/development/sdsc/renku-python -s --cache-clear
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -c "import sys;print(sys.prefix)"
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -c "import sys;print(sys.prefix)"
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -c "import sys;print(sys.executable)"
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -c "import sys;print(sys.executable)"
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -m site --user-site
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -m site --user-site
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python completion.py
cwd: ~/.vscode-oss/extensions/ms-python.python-2020.3.69010/pythonFiles
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python completion.py
cwd: ~/.vscode-oss/extensions/ms-python.python-2020.3.69010/pythonFiles
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -m flake8 --format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s ~/development/sdsc/renku-python/tests/cli/test_init.py
cwd: ~/development/sdsc/renku-python
> ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -m flake8 --format=%(row)d,%(col)d,%(code).1s,%(code)s:%(text)s ~/development/sdsc/renku-python/tests/cli/test_init.py
cwd: ~/development/sdsc/renku-python
##########Linting Output - flake8##########

Even when downgrading the Python extension to 2020.2.64397, I get the very same behavior. Sorry for my previous statement, I was convinced it was related to the Python extension version since debugging tests stopped working the same day the extension was updated.

Breakpoints somewhere else in the code seem to work when debugging with the remote attach, so the issue seems to be limited to debugging tests. At this point, I believe this is related to my system, but any help in further understanding how to fix this would be greatly appreciated.

lorenzo-cavazzi commented 4 years ago

I have tried on another system (Ubuntu 18.04) and I initially got the very same result. After trying many things, I was able to debug by commenting out in pytest.ini the line addopts = --flake8 --pep8 [...] :raised_hands:

For some reason, this seems to work on Ubuntu but not on my initial system Manjaro :sob: . I also tried to uninstall VSCode and remove the config files ($HOME/.vscode and $HOME/.config/Code), but this didnt't help.

I am wondering if there is a detailed log I can look at to get extra infomation :thinking:

kimadeline commented 4 years ago

@asipras Can you add the following configuration to your launch.json:

        {
            "name": "Python: Test debug config",
            "type": "python",
            "request": "test",
            "console": "integratedTerminal",
            "logToFile": true
        }

Try debugging your tests, and copy here the content of the debugpy*.log file from the extension folder.

@lorenzo-cavazzi are you getting the same Duplicate entries in "env" message? If that's the case, there is something else going on in your setup that's not related to the extension, because it's a Windows-specific error message from the debugger:

https://github.com/microsoft/debugpy/blob/69db68a5f9aff6c1dfd955c8934c753212f94633/src/debugpy/launcher/handlers.py#L110-L119

lorenzo-cavazzi commented 4 years ago

No, I don't get that message. I guess my problem is unrelated to @asipras 's one

lorenzo-cavazzi commented 4 years ago

Other info: if I open the Developer Tools I get the following message when successfully running the tests

[Extension Host] Info Python Extension: 2020-03-25 11:24:26: getActivatedEnvironmentVariables, Class name = C, completed in 0ms, has a truthy return value, Arg 1: <Uri:/home/lorenzo/development/sdsc/renku-python>, Arg 2: undefined, Arg 3: undefined
console.ts:137 [Extension Host] Info Python Extension: 2020-03-25 11:24:26: > ~/.local/share/virtualenvs/renku-python-WcnE3QeY/bin/python -m pytest --rootdir ~/development/sdsc/renku-python --junitxml=/tmp/tmp-10951bUxBnRjlaHFD.xml ./tests/core/management/test_repository.py::test_init_repository
console.ts:137 [Extension Host] Info Python Extension: 2020-03-25 11:24:26: cwd: ~/development/sdsc/renku-python

and I get this error when trying to debugging the test

ERR timeout after 500 ms: Error: timeout after 500 ms
    at t.RawDebugSession.handleErrorResponse (file:///usr/lib/code/out/vs/workbench/workbench.desktop.main.js:2954:952)
    at file:///usr/lib/code/out/vs/workbench/workbench.desktop.main.js:2954:250
    at async t.RawDebugSession.shutdown (file:///usr/lib/code/out/vs/workbench/workbench.desktop.main.js:2952:336)
lorenzo-cavazzi commented 4 years ago

Here with more details from the Log (Window)

[2020-03-25 15:05:19.148] [renderer1] [error] timeout after 500 ms: Error: timeout after 500 ms
    at t.RawDebugSession.handleErrorResponse (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:2954:952)
    at file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:2954:250
    at async t.RawDebugSession.shutdown (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:2952:336)
[2020-03-25 15:05:20.859] [renderer1] [error] TreeError [CustomView] Data tree node not found: [object Object]: Error: TreeError [CustomView] Data tree node not found: [object Object]
    at H.getDataNode (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:1097:771)
    at H._updateChildren (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:1095:64)
    at H.updateChildren (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:1094:798)
    at file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:5773:30
    at Array.map (<anonymous>)
    at G.doRefresh (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:5773:21)
    at G.refresh (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:5772:111)
    at p.$refresh (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:3671:510)
    at g._doInvokeHandler (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:3681:363)
    at g._invokeHandler (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:3681:55)
    at g._receiveRequest (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:3679:739)
    at g._receiveOneMessage (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:3678:518)
    at file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:3676:754
    at c.fire (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:110:24)
    at v.fire (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:382:801)
    at t.PersistentProtocol._receiveMessage (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:387:276)
    at file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:384:321
    at c.fire (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:110:24)
    at g.acceptChunk (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:380:306)
    at file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:379:657
    at Socket.t (file:///opt/visual-studio-code/resources/app/out/vs/workbench/workbench.desktop.main.js:390:450)
    at Socket.emit (events.js:203:13)
    at addChunk (_stream_readable.js:295:12)
    at readableAddChunk (_stream_readable.js:276:11)
    at Socket.Readable.push (_stream_readable.js:210:10)
    at Pipe.onStreamRead (internal/stream_base_commons.js:166:17)

P.S. sorry for the spam, I am really mad about this, I tried so many things to fix... :man_facepalming:

kimadeline commented 4 years ago

@lorenzo-cavazzi since you said that you still can't debug tests with a previous version of the extension, that it suddenly stopped working, and that it worked on Ubuntu but not on Manjaro I suspect that it's something specific to your setup and not the extension. Have you tried asking on Stack Overflow?

In any case, since your problem is unrelated to the original one, please open a separate issue with all your comments merged in one.

lorenzo-cavazzi commented 4 years ago

I tried anything I could find by googling but I couldn't fix it. Maybe there is something else I can try in order to debug? I opened another issue here #10778

asipras commented 4 years ago
        {
            "name": "Python: Test debug config",
            "type": "python",
            "request": "test",
            "console": "integratedTerminal",
            "logToFile": true
        }

@kimadeline I did what you suggested. Below is my launch.json. What is amazing is now I can debug test using latest version of python extension 2020.03.69010. If I remove the configuration that you suggested from my launch,json then I get the same error.

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Current File",
            "type": "python",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal"
        },
        {
            "name": "Python: Test debug config",
            "type": "python",
            "request": "test",
            "console": "integratedTerminal",
            "logToFile": true
        }
    ]
}

debugpy.adapter-27144.log debugpy.launcher-29020.log debugpy.pydevd.24988.log debugpy.server-24988.log

kimadeline commented 4 years ago

Thanks for trying it!

What is amazing is now I can debug test using latest version of python extension 2020.03.69010. If I remove the configuration that you suggested from my launch,json then I get the same error.

This is puzzling 🤔 Somehow the explicit test config does (or doesn't do) something that the default config does, which doesn't add the duplicate value and solves your issue.

Can you try something else? Since getting the logs this way has the unintended effect of solving your issue, let's try retrieving the logs a different way:

This way we can compare the logs, and see what the default config sets behind the scenes.

Thanks!

cc @int19h

asipras commented 4 years ago

@kimadeline I did get the logs with latest python extension and removing the test configuration that you suggested earlier. I got the same error. I am uploading those log files Logs_with_error.zip.

I deleted all the log files added the test configuration to launch.json and debugger worked. I am attaching those log files Logs_without_error.zip Logs_without_error.zip Logs_with_error.zip

int19h commented 4 years ago

It seems that the default configuration has all environment variables copied into "env" by the extension. And then on top of that, somehow this ends up with two path variables - there's "Path" and "PATH" in JSON, both with the same value. This doubling is what's triggering the error.

I believe this is the code that's doing the copying: https://github.com/microsoft/vscode-python/blob/0ba42da331103036e7dab7b73e4585a028570fce/src/client/debugger/extension/configuration/resolvers/helper.ts#L50-L62

This is wrong for debugpy - the extension is only spawning the adapter, and "env" doesn't apply to it. Instead, the adapter itself will parse "env" and use it to spawn the debuggee correctly, so none of this code should be applicable.

Furthermore, this code also works with variables in a case-sensitive manner, even on Windows. Note how, after calling mergeVariables(), it tries to manually tweak PYTHONPATH and path, the latter retrieved via getPathVariableName(). This ends up here: https://github.com/microsoft/vscode-python/blob/0ba42da331103036e7dab7b73e4585a028570fce/src/client/common/platform/fs-paths.ts#L88-L90

This abstraction is pointless, because there's no guarantee whatsoever that Path is how it's always capitalized on Windows - indeed, in this case, it was not. Nor is there any guarantee that PYTHONPATH is spelled with all caps. The code that's dealing with either one of those just has to account for names being case-insensitive on Windows, and merge accordingly.

(This entire function is wrong for debugpy, actually - it shouldn't be setting any other specific environment variables like PYTHONIOENCODING or GEVENT. It should only be handling .envFile, reading entries from there, and adding them to "env".)

kimadeline commented 4 years ago

Thank you @asipras and @int19h!

@asipras this is an issue on our end, so until we fix it you'll have to use the test configuration in your launch.json as a workaround.

Further discussion with @int19h:

Path is spelled "PATH" on all platforms, so we don't really need a service for that. What we do need is a helper library to work with objects that are really collections of environment variables (like process.env), which enforce OS behavior - i.e. case insensitive keys on Windows.

BTW, apparently Node itself does it correctly for process.env: On Windows operating systems, environment variables are case-insensitive.

process.env.TEST = 1; console.log(process.env.test); // => 1

sawatsky commented 4 years ago

I had this exact same issue and, yes, after adding that configuration to my launch.json, I'm now able to debug my code. So the workaround... works.

matrix3d commented 4 years ago

2020.2.64397 can not work. 2019,can work

amandalishus commented 4 years ago

I'm having this same problem, and using the test configuration to my launch.json does not alleviate the issue. For what it's worth, I know that I'm in some kind of A/B test group from an experience a few months ago which prevented me from debugging unit tests. I can go through steps to provide logs if you'd like, but it seems like you've already found the source of the problem. I guess for now I will try to figure out how to downgrade to the previous version and see if that helps, but I wanted to add my report because the behavior differs slightly from the other reports, and because I'd like an update when the issue is fixed.

amandalishus commented 4 years ago

Worked around issue by downgrading Python extention to v2020.2.64397, reloading extension and then exiting and restarting VSCode. Curiously, it now reports a problem in test discovery, although it seems to be discovering tests successfully regardless of the error. At any rate, I'm hoping to hear that this issue is fixed in the next release. Please let me know if there's anything I can do to help. If it's useful to you at all, I've had the PTVSD_LOG_DIR environment set on my system since the last time I reported an issue, so I have plenty of logs from that which I can supply ;)

NicolaiLolansen commented 4 years ago

@asipras Can you add the following configuration to your launch.json:

        {
            "name": "Python: Test debug config",
            "type": "python",
            "request": "test",
            "console": "integratedTerminal",
            "logToFile": true
        }

Try debugging your tests, and copy here the content of the debugpy*.log file from the extension folder.

@lorenzo-cavazzi are you getting the same Duplicate entries in "env" message? If that's the case, there is something else going on in your setup that's not related to the extension, because it's a Windows-specific error message from the debugger:

https://github.com/microsoft/debugpy/blob/69db68a5f9aff6c1dfd955c8934c753212f94633/src/debugpy/launcher/handlers.py#L110-L119

This also worked for me.

int19h commented 4 years ago

@amandalishus Are you seeing the same error message about "Duplicate entries in "env""? If not, it is likely to be a different issue - please file it separately.

tony007yo commented 4 years ago

@int19h Catch the same bug in v2020.8.101144. My decision was to rename from "PATH" to "Path" in system environment variables.

Perhaps it will be useful to someone.

rdesroches commented 3 years ago

Just in case someone is having the same problems I was having and can't get this to work at all but needs it to, this is how i get it to work for me. I actually modify the handlers.py file located at

%HOMEPATH%\.vscode\extensions\ms-python.python-xxx.x.xxxx\pythonFiles\lib\python\debugpy\launcher\handlers.py

I comment out the lines that checks for duplicate entries:

Around line 89 you will see:

 env_change = { k.upper(): v for k, v in env_changes.items() }

Comment this line out:

 # env_change = { k.upper(): v for k, v in env_changes.items() }

I have to do this when I restart code, but this allows me to do debugging.

int19h commented 3 years ago

Note that the above change just turns it from a diagnosable error to (potential) silently wrong behavior, because you might end up with both "Path" and "PATH" in the environment, and it's non-deterministic as to which one will be actually used for the debuggee. The proper fix would be on the extension side (although you can't easily hot-patch it like you can with debugpy).

masmagit commented 3 years ago

Here are some observations while dealing with this issue, maybe this will help someone, Python v2021.5.842923320:

  1. The specified error appears when trying to debug a test, and there is no configuration specified at all, neither for launch nor for test.
  2. If you are using .code-workspace file to store your "launch" configurations, adding the mentioned fixing configuration (where "request": "test") does not help. You need to create a project level configuration launch.json and add the fixing configuration there.
  3. Tests debugger is ignoring the selected debugging configuration and using the one from launch.json in .vscode folder. If launch.json does not exist, our error appears: "Invalid message: Duplicate entries in "env" ".
lhotamir commented 2 years ago

I found out, that in my case the duplicate entries error occures when I launch VS Code vie terminal using Git Bash.

When I launch it by desktop shortcut everything works fine.

robinstauntoncollins commented 2 years ago

@lhotamir Same for me. Launching using code . from Git Bash terminal results in this error when trying to test: Invalid message: Found duplicate in "env": PATH.

Launching using Windows shortcut or adding:

        {
            "name": "Python: Test debug config",
            "type": "python",
            "request": "test",
            "console": "integratedTerminal",
            "logToFile": true
        }

to launch.json resolves the problem.

Amazing that this problem still exists nearly two years later.

chwiede commented 2 years ago

@robinstauntoncollins I can confirm this launch config is working.

Unfortunately then VS Code complains "test" isn't a valid value for request. Valid values are "launch" or "attach". Is there any solution/workaround for this?

Varchala commented 2 years ago

@robinstauntoncollins I can confirm this launch config is working.

Unfortunately then VS Code complains "test" isn't a valid value for request. Valid values are "launch" or "attach". Is there any solution/workaround for this?

I am facing the same issue.

reubster commented 2 years ago

What worked for me just now...

I found that there was a "PythonPath" env variable set on my user profile. I think, as per above, that case sensitivity on this was confusing vscode.

I've deleted it - rebooted - and now debug works just fine.

panos23kar commented 2 years ago

@kimadeline is there a fix? Im still facing the same issue! I have also opened a stackoverflow thread for it : https://stackoverflow.com/questions/72717904/visual-studio-code-vs-code-run-test-button-works-fine-but-debug-test-does-not

kimadeline commented 2 years ago

cc @karthiknadig

karthiknadig commented 2 years ago

@panos23kar Looking at the SO issue the module not found problem is often due to environment not being activated. Can you file a separate bug for your scenario with output logs and type of environment you are using.

synchro-- commented 2 years ago

Still the same issue here. It feels very frustrating cause the debug functionality is used by everyone at my company and we always need to downgrade the extension to the 8.1 version.

FeeeeK commented 1 year ago

For some reason this problem repeated in version 2023.3.10411009. In my case Path is simply PATH twice, PYTHONPATH also just duplicate of itself.

romanchyla commented 1 year ago

this might help someone, here is part of lanuch.json which finally worked with pytest

{
            "name": "PyTest",
            "type": "python",
            "request": "test",
            "stopOnEntry": false,
            "module": "pytest",
            "console": "integratedTerminal",
            "logToFile": true,
            "args": [
                "-v",
            ],
            "cwd": "${fileDirname}",
            "env": {
                "PYTEST_ADDOPTS": "--no-cov"
            },
        },

if using pytest and coverage, debugger won't break -- so py-cov needs deactivating

also, request needs to be test (even if vscode complains about it being invalid)

shaperilio commented 1 year ago

Not sure why this issue is just sitting here, but it's come up again. When I try to debug a test with the Python Extension version v2023.6.0, I get "Invalid message: Found duplicate in "env": PATH image

Version 2023.4.1 works just fine.

The error message is not helpful; it certainly makes it seem like I should know what to do with launch.json.

I tried to turn on logging as suggested above, but I can't find the log file... no idea if that setting is still valid.

eric-peacock commented 1 year ago

@shaperilio Same issue here. I was able to fix by adding the following to launch.json per instructions from a few years ago in thread above:

    {
        "name": "Python: Test debug config",
        "type": "python",
        "request": "test",
        "console": "integratedTerminal",
        "logToFile": true
    }

I just ignored the linting error on "request" type.

blakeNaccarato commented 1 year ago

The equivalent configuration to the deprecated way above looks like:

launch.json

    {
      "name": "Python: Test debug config",
      "type": "python",
      "request": "launch",
      "console": "integratedTerminal",
      "purpose": ["debug-test"],
      "logToFile": true
    }

See this debug recommendation...

https://github.com/microsoft/vscode-python/blob/5528e0efb9b8c91c83dfb356391085280fb63417/src/client/debugger/extension/configuration/debugConfigurationService.ts#L75

lpsmith-dev commented 1 year ago

Same issue here, no longer possible to debug my python tests suite after updating to v2023.6.0. None of the configuration metioned above solve the problem for me.

Back to work after downgrading to v2023.4.1

FirefoxMetzger commented 1 year ago

Adding to the buzz. Same issue, same solution with the workaround. My versions:

Version: 1.77.0 (user setup)
Commit: 7f329fe6c66b0f86ae1574c2911b681ad5a45d63
Date: 2023-03-29T10:02:16.981Z
Electron: 19.1.11
Chromium: 102.0.5005.196
Node.js: 16.14.2
V8: 10.2.154.26-electron.0
OS: Windows_NT x64 10.0.22000
Sandboxed: No
Alexvankooy-holman commented 1 year ago

Same issue, the "tests" fix did not work and I had to downgrade the extension

leyser7 commented 1 year ago

Same issue, @blakeNaccarato's solution works for version v2023.6.0 but i would have to add in all projects. I do not like that. I go back to version v2023.4.1 that does not have the problem

blakeNaccarato commented 1 year ago

Note that my config above probably fixes things as a side-effect, maybe by logging errors to a file instead of surfacing them and breaking the flow. The config is actually meant for generating logs to share with devs for fixing bugs such as these.

In a (probably) related issue the devs say they are working on fixing the bug as soon as possible. Since both of these issues cropped up in the latest release, and both are related to VSCode debugging, it is likely they share the root cause of duplicated environment variables with different values.

quammy commented 1 year ago

Same - downgrade Python extension to v2023. 4.1 solved the issue.

damienallen commented 1 year ago

This seems to be fixed in the pre-release version (I'm on v2023.7.11011538)

Snailedlt commented 1 year ago

This worked for me:

    {
      "name": "Python: Pytest",
      "type": "python",
      "request": "test",  // <----- this is for tests
      "args": [
        "-v" // verbose
      ],
      "console": "integratedTerminal"
      }

Now my full settings.json file looks like this:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Python: FastAPI",
      "type": "python",
      "request": "launch",
      "module": "uvicorn",
      "args": [
        "api.main:app"
      ],
      "jinja": true,
      "justMyCode": true
    },
    {
      "name": "Python: Pytest",
      "type": "python",
      "request": "test",  // <----- this is for tests
      "args": [
        "-v" // verbose
      ],
      "console": "integratedTerminal"
      }
  ]
}
JordanGardiner-enable commented 1 year ago

For me I'm using unittest not Pytest but by changing the settings.json to this fixed it for me:

{ "python.testing.unittestArgs": [ "-v", "-s", "${workspaceFolder}/", "-p", "test.py" ], "python.testing.pytestEnabled": false, "python.testing.unittestEnabled": true }

eleanorjboyd commented 1 year ago

Hello! We have just finished our testing rewrite and are beginning the rollout to users. It seems some of you have tried it and I am not sure if it was on the rewrite or not. You can confirm using the steps below. If everyone engaging on this post recently could try it on the rewrite it would be very helpful to know if your issues still exists on the rewrite! To try it yourself, you need to be on the most recent release of the Python extension and add this setting to your users settings.json "python.experiments.optInto": ["pythonTestAdapter"].

You can confirm you have the rewrite enabled by setting "python.analysis.logLevel": "Trace", in your user settings then check for Experiment 'pythonTestAdapter' is active in your python logs.

Let me know if the rewrite fixes your issue. Thanks!