With the Dev Containers extension for Visual Studio Code (VS Code), you can have access to VS Code features within a container, which includes debugging tools. Here is an overview to help you get started debugging code in your Tutor dev environment.
Initial Setup
-
Make sure you have the Dev Containers extension installed (
lms-vscode-remote.remote-containers). -
Open the VS Code Command Palette (Ctrl+Shift+P / Cmd+Shift+P) and run
Dev Containers: Attach to Running Container. -
Select the container (the LMS or CMS container) that you want to debug in.
-
VS Code will open a new window connected (inside) the selected container.
-
In the VS Code File Explorer click on “Open Folder” and select
/openedx/edx-platform. -
Ensure the
Pythonextensionms-python.pythonis installed inside the container. -
In the Command Palette run
Python: Select Interpreterand make sure~/venv/bin/python(more specifically/openedx/venv/bin/python) is selected. -
Open the Run and Debug panel on the left hand side, and the cog icon to create or open a launch.json file (this file by default will be created in
/openedx/edx-platform/.vscode).
Debugging running instances of the LMS and/or CMS
There is a solution that has been posted in the forums on this topic. This builds on that solution, but modifies it to take advantage of the Dev Containers extension.
-
Edit the
lmsandcmsservices in"$(tutor config printroot)/env/dev/docker-compose.yml". Change thecommandto be like this (usecmsaftermanage.pyfor thecmsservice):command: > sh -xc "python -m pip install debugpy && python -m debugpy --listen 0.0.0.0:5678 ./manage.py lms runserver 0.0.0.0:8000"
Note***: If you would like to debug the runserver command from the beginning, add
--wait-for-clientto the debugpy command. If you do this, the runserver command will not execute until a debugger is attached to it. This is helpful if you want to debug setup/configuration of the service. -
Run
tutor dev reboot lms cms -dto stop and start the development containers, while picking up the changes todocker-compose.yml. -
In your launch.json file add a configuration item like this:
{
"name": "attach",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"justMyCode": false
},
- Run the “attach” configuration. As you make HTTP requests to the LMS and CMS services, the debugger will suspend execution at any breakpoints along the execution path - allowing you to use the debugger tools in VS Code.
Debugging edx-platform tests
Add a configuration like this to your launch.json file:
{
"name": "pytest cms",
"type": "debugpy",
"request": "launch",
"module": "pytest",
"args": [
"-p",
"no:randomly",
"--ds",
"cms.envs.test",
"common/djangoapps/student/tests/test_models.py"
],
"justMyCode": false
},
Change the --ds argument to be the Django settings path you want to use. The last argument is a specifies which test(s) to run - update this as needed. Here are some examples:
# directory: run all tests under here
"common/djangoapps/student/"
# file: run all tests in this file
"common/djangoapps/student/tests/test_admin_views.py"
# class: run all tests in this test class
"common/djangoapps/student/tests/test_admin_views.py::AdminCourseRolesPageTest"
# test function/method: run exactly this test
"common/djangoapps/student/tests/test_admin_views.py::AdminCourseRolesPageTest::test_save_without_org_and_course_data"
Debugging edx-platform management commands
Add a configuration like this to your launch.json file for the command you want to run:
{
"name": "lms dump_settings",
"type": "debugpy",
"request": "launch",
"args": [
"lms",
"dump_settings"
],
"env": {
"DJANGO_SETTINGS_MODULE": "lms.envs.production",
"LMS_CFG": "lms/envs/mock.yml"
},
"django": true,
"autoStartBrowser": false,
"program": "${workspaceFolder}/manage.py",
"justMyCode": false
},
Notes
- The changes you make in
docker-compose.ymlin your Tutor environment will be overwritten on atutor config save,tutor dev launch, etc. - Set
"justMyCode"tofalseif you want to be able to step into dependency code.