Skip to content

Use cwd value in settings #20711

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
fensoft opened this issue Feb 16, 2023 · 21 comments · Fixed by #21437
Closed

Use cwd value in settings #20711

fensoft opened this issue Feb 16, 2023 · 21 comments · Fixed by #21437
Assignees
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team verification-needed Verification of issue is requested verified Verification succeeded
Milestone

Comments

@fensoft
Copy link

fensoft commented Feb 16, 2023

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
Copy link
Member

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
Copy link
Author

fensoft commented Feb 16, 2023

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
Copy link
Member

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

@fensoft
Copy link
Author

fensoft commented Feb 16, 2023

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

@karthiknadig
Copy link
Member

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

@karthiknadig karthiknadig transferred this issue from microsoft/vscode-python Feb 16, 2023
@fensoft
Copy link
Author

fensoft commented Feb 16, 2023

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
Copy link
Member

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
        }

@int19h int19h added the bug Issue identified by VS Code Team member as probable bug label Feb 16, 2023
@fensoft
Copy link
Author

fensoft commented Feb 16, 2023

{
"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
Copy link
Member

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 karthiknadig transferred this issue from microsoft/debugpy Feb 16, 2023
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Feb 16, 2023
@karthiknadig
Copy link
Member

@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
Copy link

int19h commented Feb 16, 2023

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
Copy link
Author

fensoft commented Feb 16, 2023

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

@paulacamargo25 paulacamargo25 added area-debugging and removed triage-needed Needs assignment to the proper sub-team labels Apr 3, 2023
@brettcannon brettcannon added this to the June 2023 milestone Jun 8, 2023
paulacamargo25 added a commit that referenced this issue Jun 15, 2023
@paulacamargo25 paulacamargo25 added the verification-needed Verification of issue is requested label Jun 26, 2023
@paulacamargo25
Copy link

paulacamargo25 commented Jun 27, 2023

Verification steps:

  • Create a simple unit test example.
  • Update the settings, to use your own value "python.testing.cwd": "${workspaceFolder}/fw-python",
  • Run debug tests, and make sure this works.

@paulacamargo25 paulacamargo25 changed the title Unit test won't debug Use cwd value in settings Jun 27, 2023
@ulugbekna ulugbekna added verified Verification succeeded and removed verified Verification succeeded labels Jun 28, 2023
@ulugbekna
Copy link

ulugbekna commented Jun 28, 2023

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:

Screen.Recording.2023-06-28.at.11.43.32.mov

@joaomoreno
Copy link
Member

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.

@alexr00 alexr00 added the verification-steps-needed Steps to verify are needed for verification label Jun 28, 2023
@rzhao271
Copy link

rzhao271 commented Jun 28, 2023

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.

@rzhao271 rzhao271 reopened this Jun 28, 2023
@rzhao271 rzhao271 added verification-found Issue verification failed and removed verification-steps-needed Steps to verify are needed for verification labels Jun 28, 2023
@github-actions github-actions bot added the triage-needed Needs assignment to the proper sub-team label Jun 28, 2023
@eleanorjboyd
Copy link
Member

eleanorjboyd commented Jun 28, 2023

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.

@github-actions github-actions bot added the info-needed Issue requires more information from poster label Jun 28, 2023
@rzhao271
Copy link

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 #20711 (comment)

@eleanorjboyd
Copy link
Member

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
Copy link
Member

looping in @karthiknadig

@paulacamargo25
Copy link

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.

@paulacamargo25 paulacamargo25 removed the verification-found Issue verification failed label Jun 28, 2023
@rzhao271 rzhao271 added the verified Verification succeeded label Jun 28, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-debugging bug Issue identified by VS Code Team member as probable bug info-needed Issue requires more information from poster triage-needed Needs assignment to the proper sub-team verification-needed Verification of issue is requested verified Verification succeeded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants