Example of VSCode Python debugger failing when tests requires custom PWD.
Steps to reproduce:
- Use
make init
to create venv in the venv/ directory. - Configure your VSCode to use it as an interpreter.
- At the moment, you cannot find any tests in the Test Explorer. Let's fix it by setting in VSCode
python.testing.cwd
to thesubproject/theproject
directory. - Now, you can open test explorer and run tests. It should pass.
- Let's try to debug the test. Apparently, you cannot, and in
output/Python
you see something like:[ERROR...] [object Object] [object Object]
Steps to debug the troubles:
-
Try run a test debug, then look into Output/Python window, find a line DAP Server launched with command ...
-
Basing on this path, open the folder pythonFiles in the extension's dir, and first do following:
-
in
lib/python/debugpy/adapter/__main__.py
find the line about 44, and force the log.log_dir to a chosen writable directory, -
in
visualstudio_py_testlauncher.py
look into the main() and in the main try/except block, before the finally add the following:except Exception as err: errorMessage = traceback.format_exc() print(errorMessage)
-
-
Now run tests once more, and in the selected logs directory, in the debugpy.adapter-xxx.log file, look for "category": "stdout". You will see outputs from the test runner.
-
Use your magic power and debug, add prints etc. until you discover the issue.
Now, let's try to fix it:
- In the
lib/python/debugpy/adapter/clients.py
let's find the line with parsingcwd
(around 375) and change it to the expected testing path (absolute path). - Run the debugging.
- It works!