microsoft / vscode-python

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

Use cwd value in settings #20711

Closed fensoft closed 1 year ago

fensoft commented 1 year ago

Type: Bug

Hello,

When I click on "Run Test", it works but "Debug Test" wont.

I have a very simple settings.json:

{
    "python.analysis.typeCheckingMode": "basic",
    "python.testing.cwd": "${workspaceFolder}/fw-python",
    "python.testing.unittestArgs": [
        "-v",
        "-s",
        "tests",
        "-p",
        "test_*.py"
    ],
    "python.testing.unittestEnabled": true
}

Extension version: 2023.1.10042123 VS Code version: Code 1.75.1 (441438abd1ac652551dbe4d408dfcec8a499b8bf, 2023-02-08T21:32:34.589Z) OS version: Windows_NT x64 10.0.22621 Modes: Sandboxed: No Remote OS version: Linux x64 5.15.79.1-microsoft-standard-WSL2

System Info |Item|Value| |---|---| |CPUs|AMD Ryzen 5 4600H with Radeon Graphics (12 x 2994)| |GPU Status|2d_canvas: enabled
canvas_oop_rasterization: disabled_off
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_renderer: enabled_on
video_decode: enabled
video_encode: enabled
vulkan: disabled_off
webgl: enabled
webgl2: enabled
webgpu: disabled_off| |Load (avg)|undefined| |Memory (System)|19.37GB (6.00GB free)| |Process Argv|--crash-reporter-id 2691f011-2355-4f25-9d98-fca7cf580e9a| |Screen Reader|no| |VM|0%| |Item|Value| |---|---| |Remote|WSL: Ubuntu| |OS|Linux x64 5.15.79.1-microsoft-standard-WSL2| |CPUs|AMD Ryzen 5 4600H with Radeon Graphics (12 x 2994)| |Memory (System)|9.41GB (7.94GB free)| |VM|0%|
A/B Experiments ``` vsliv368:30146709 vsreu685:30147344 python383cf:30185419 vspor879:30202332 vspor708:30202333 vspor363:30204092 vslsvsres303:30308271 pythonvspyl392:30443607 vserr242:30382549 pythontb:30283811 vsjup518:30340749 pythonptprofiler:30281270 vshan820:30294714 vstes263cf:30335440 vscorecescf:30445987 pythondataviewer:30285071 vscod805cf:30301675 binariesv615:30325510 bridge0708:30335490 bridge0723:30353136 cmake_vspar411:30581797 vsaa593:30376534 pythonvs932:30410667 cppdebug:30492333 vsclangdc:30486549 c4g48928:30535728 dsvsc012cf:30540253 azure-dev_surveyone:30548225 vscccc:30610679 pyindex848:30662994 nodejswelcome1cf:30587006 282f8724:30602487 pyind779:30662992 89544117:30613380 pythonsymbol12:30657548 vsccsb:30662443 vscodedisable:30660115 ```
karthiknadig commented 1 year ago

Can you share the logs from Output > Python? what is the version of python and type of environment (conda, venv , poetry, etc)? when you say it doesn't work what exactly happens, as in do you see any errors or exceptions, debugger does not start at all.

fensoft commented 1 year ago

I'm using python3.8.10 bundled with Ubuntu 20.04.4 LTS inside WSL2. VSCode is running in windows 11. "Doesn't work" means when I click on the Debug Test button but nothing happens.

The test file can be pretty simple:

import unittest

class TestDebug(unittest.TestCase):
    def test_addition(self):
        print('test')
        self.assertEqual(2, 1+1)

When I click Run Test, here is Python Test Log:

test_addition (test_debug.TestDebug) ... ok

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK
test

And Python Log:

> /bin/python3 ~/.vscode-server/extensions/ms-python.python-2023.1.10042123/pythonFiles/visualstudio_py_testlauncher.py --us=tests --up=test_*.py --uvInt=2 --result-port=45385 -ttest_debug.TestDebug.test_addition --testFile=~/project/fw-python/tests/test_debug.py
cwd: ~/project/fw-python

And with Debug Test, Python Test Log is empty and here is Python Log:

DAP Server launched with command: /bin/python3 /home/fensoft/.vscode-server/extensions/ms-python.python-2023.1.10042123/pythonFiles/lib/python/debugpy/adapter
karthiknadig commented 1 year ago

If you set a breakpoint inside test_addition does it stop at the breakpoint when debugging?

fensoft commented 1 year ago

It wont run anything. I have nothing in the logs without breakpoints and any breakpoint I set can't be reached.

karthiknadig commented 1 year ago

This feels like a debugger issue, moving this to debugpy.

fensoft commented 1 year ago

I have tested with pure python debugger and it works, with this launch.json config:

        {
            "name": "test_debug",
            "type": "python",
            "request": "launch",
            "program": "-m",
            "args": [ "unittest", "${workspaceFolder}/fw-python/tests/test_debug.py" ],
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}/fw-python",
            "justMyCode": true
        }

So, this looks like it's a bug inside the test python debugger.

karthiknadig commented 1 year ago

They are the same, What might help is getting logs for the test debug scenario. You can get that by setting logToFile. Like this:

        {
            "name": "Python Test",
            "type": "python",
            "request": "launch",
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}/fw-python",
            "purpose": ["debug-test"],
            "justMyCode": true,
            "logToFile": true,
        }

Add the above configuration to your launch.json file, then run the test debug from the test explorer. That should generate logs in the following location /home/fensoft/.vscode-server/extensions/ms-python.python-2023.1.10042123. look for files ending with *.log and share them all here.

FYI, if you want to run something as a module, you should really be using the module field, like this:

        {
            "name": "test_debug",
            "type": "python",
            "request": "launch",
            "module": "unittest",
            "args": ["${workspaceFolder}/fw-python/tests/test_debug.py"],
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}/fw-python",
            "justMyCode": true
        }
fensoft commented 1 year ago

{ "name": "Python Test", "type": "python", "request": "launch", "console": "integratedTerminal", "cwd": "${workspaceFolder}/fw-python", "purpose": ["debug-test"], "justMyCode": true, "logToFile": true, }

adding this to my launch.json file makes the debuging works now. are you still interested in log files ?

karthiknadig commented 1 year ago

If it is working then we won't need it. The issue I think is cwd was not set when running from the test explorer. Moving this back to vscode-python. This is a bug in the debug configuration generation for test.

karthiknadig commented 1 year ago

@eleanorjboyd @paulacamargo25 This looks like we are not setting the cwd field in the debug configuration for test when running tests under the debugger. We should be setting cwd if user has set it in python.testing.cwd.

int19h commented 1 year ago

I think part of the problem might be that when you use "program" without "cwd", debugpy will use the parent directory of the program file as "cwd" by default. However, with "module", it can't do that as module name is not a filename, so there's no path to extract from it; the current directory will thus be inherited from the parent process (usually, the shell in the integrated terminal).

fensoft commented 1 year ago

woah, i'm glad you found the issue so quickly !

paulacamargo25 commented 1 year ago

Verification steps:

ulugbekna commented 1 year ago

I was trying to verify this (following the verification steps), but neither running nor debugging tests works for me. I'm not too familiar with python extension, so am I doing something incorrectly?

I have the following workspace:

.
├── __pycache__
│   ├── main.cpython-311.pyc
│   ├── main_test.cpython-311-pytest-7.4.0.pyc
│   └── main_test.cpython-311.pyc
├── main.py
└── tests
    ├── __pycache__
    │   └── main_test.cpython-311.pyc
    └── main_test.py

following settings in settings.json:

  "python.testing.cwd": "${workspaceFolder}/tests",
  "python.testing.unittestEnabled": true,

I open this workspace in vscode, invoke command "Python: Configure Tests", from the quick pick I pick 'unittest', then from the next quick pick, I pick 'tests' directory, then *_tests.py. I have all tests discovered

image

Neither running nor debugging the tests works for me with this setup. I get this in Output pane for Python:

2023-06-28 11:48:44.145 [info] Running UNITTEST execution for the following test ids: main_test.TestMyModule.test_add
2023-06-28 11:48:44.146 [info] Server listening on port 57023
2023-06-28 11:48:44.150 [info] Discovering unittest tests with arguments: /Users/ulugbekna/.vscode-insiders/extensions/ms-python.python-2023.11.11781018/pythonFiles/unittestadapter/execution.py,--port,56844,--uuid,d0d4172a-84a1-48e0-a117-0518bb6922cd,--udiscovery,-v,-s,./tests,-p,*_test.py

2023-06-28 11:48:44.150 [info] > /opt/homebrew/bin/python3 ~/.vscode-insiders/extensions/ms-python.python-2023.11.11781018/pythonFiles/unittestadapter/execution.py --port 56844 --uuid d0d4172a-84a1-48e0-a117-0518bb6922cd --udiscovery -v -s ./tests -p *_test.py
2023-06-28 11:48:44.150 [info] cwd: ./tests
2023-06-28 11:48:44.210 [info] Client disconnected

Here's the screencast:

https://github.com/microsoft/vscode-python/assets/16353531/e7eb771c-219b-4888-81d6-9976e09e3174

joaomoreno commented 1 year ago

Same. I also don't understand how this correlates with the strange Python: Configure tests command, since that requires that I pick a directory to find tests in. Which one would take precedence here? Related feedback.

rzhao271 commented 1 year ago

My Python logs still seem to say cwd: . even when I set the setting in my workspace. Reopening for now.

My workspace showing the output log not recognizing the setting, my workspace settings file, and the file explorer on the left.
eleanorjboyd commented 1 year ago

Hi! Yes, I can see why this is an issue. It looks like the extension is not eferencing that testing cwd setting correctly when configuring the testing command. From what I am seeing in the logs you are on the rewrite of the python test adapter. Could you put this in your settings and retry: "python.experiments.optOutFrom": ["pythonTestAdapter"],? If it works with this in the settings then I can confirm it is an issue introduced in my rewrite and I will open another issue in relation to that bug.

rzhao271 commented 1 year ago

After opting out, the correct cwd is used! I noticed, though, that the test still isn't discovered in the test explorer. Do I need to press "Configure Python Tests", still? Ref verification steps in https://github.com/microsoft/vscode-python/issues/20711#issuecomment-1608513608

eleanorjboyd commented 1 year ago

Yeah I am seeing the same thing right now- mine works with these settings:

 "python.testing.unittestArgs": ["-p", "test_*.py"],
  // "python.testing.cwd": ".",

and doesn't work with these:

 "python.testing.unittestArgs": ["-p", "test_*.py"],
 "python.testing.cwd": ".",

@paulacamargo25 any idea on this? Seems like it isn't getting overridden since changing the setting matters?

I will open the other issue now.

eleanorjboyd commented 1 year ago

looping in @karthiknadig

paulacamargo25 commented 1 year ago

Thanks @rzhao271 could you try with this project? test-cwd.zip

  1. In order to verify this issue, add a breakpoint in the test, when the test are discovered, click on debug test. Make sure that you can hit the brakpoint and continue with the execution.
  2. You can try commenting the cwd value, and the debug will not work.