Skip to content

Conversation

@matthew-li
Copy link
Collaborator

@matthew-li matthew-li commented Jun 24, 2025

Description

**** Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change. ****

This PR enables VSCode's native debugger to optionally be used with the web application. It installs debugpy, which implements the Debug Adapter Protocol, used by VSCode and other IDEs (but not PyCharm, apparently). The Docker container now exposes port 5678, which the IDE uses to talk to it.

Currently, only the web container has been augmented. The same can be done for the other containers if this approach is viable. EDIT: This has not been tested, but the other containers would need to have their debuggers listen on other ports (e.g., 5679, 5680), and then two new entries would have to be added to the configurations key in launch.json.

Also, a number of images were identical to the app-base image, with the only difference being that they specified CMD in their Dockerfiles. They have been removed in favor of specifying command in docker-compose.yml. The web service's command is a script that conditionally listens for a debugger only if DEBUGPY=1 is set.

Type of change

**** Please delete options that are not relevant. ****

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

**** Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration. ****

  • Add a new file launch.json to the .vscode folder:
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach to Docker Django",
      "type": "python",
      "request": "attach",
      "connect": {
        "host": "localhost",
        "port": 5678
      },
      "pathMappings": [
        {
          "localRoot": "${workspaceFolder}/coldfront",
          "remoteRoot": "/var/www/coldfront_app/coldfront"
        }
      ]
    }
  ]
}
  • Rebuild app-base and child images so that debugpy is installed.

  • Set export DEBUGPY=1 in the environment before bringing up Docker Compose.

  • Open the debugger in the left panel of VSCode. Select "Attach to Docker Django" if necessary. Click on the play button to start the web server and debugging process.

  • Set a breakpoint (e.g., in coldfront.core.user.views.UserProfile.get_context_data). Navigate to that page on the host (using the existing port 8080 or 8081), observe that execution stops, and observe that variable contents are displayed in VSCode.

    • Example image below.
  • See the updated Docker README.md for the same instructions.

vscode_debugger

PR Self Evaluation

Strikethrough things that don’t make sense for your PR.

  • My code follows the agreed upon best practices
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (if needed)
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in the appropriate modules
  • I have performed a self-review of my own code

* Wrap runserver in web.Dockerfile w/ debugpy, exposing port 5678 to
  VSCode and other DAP clients
@matthew-li matthew-li requested review from Hzaakk and helbashandy June 24, 2025 20:34
@matthew-li
Copy link
Collaborator Author

@helbashandy This isn't going to work for PyCharm, from what I can tell, so we'll still need another approach there.

@Hzaakk Do you use VSCode, and, if so, can you test this?

@matthew-li
Copy link
Collaborator Author

matthew-li commented Jun 24, 2025

There is this PyCharm plugin (or is it Intellij?): https://plugins.jetbrains.com/plugin/20934-debug-adapter-protocol, from a quick Google search.

* Avoid building unnecessary images
* Add script to run runserver or runserver wrapped w/ debugpy based on
  env. var
@matthew-li matthew-li marked this pull request as ready for review July 14, 2025 16:01
@matthew-li matthew-li changed the title [WIP] Integrate VSCode debugger into app Integrate VSCode debugger into app Jul 14, 2025
@matthew-li
Copy link
Collaborator Author

@helbashandy I've made it so that the debugger is optionally enabled (by setting an env. var. DEBUGPY=1 before running docker compose up). I've also updated the Docker README.md.

Please test + review, then approve, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants