Skip to content

feat: Added VS Code debugging support.#140

Draft
toastdriven wants to merge 3 commits intomainfrom
toastdriven/debugpy-support
Draft

feat: Added VS Code debugging support.#140
toastdriven wants to merge 3 commits intomainfrom
toastdriven/debugpy-support

Conversation

@toastdriven
Copy link

🎯 What needed to be done and why?

Adds support for interactive debugging in VS Code (among other editors, e.g. Neovim & emacs).

Quick summary of the issue

We have a stub in utils/debugger.py:vscode_debugger. This fleshes that out.

🆕 What is changed by this PR?

This handles a similar "import debug library & bind to port" pattern as the existing utils.debugger.py:pycharm_debugger, but for debugpy.

This enables anything that works with DAP protocols to talk to the backend process & interactively debug. This means support for:

  • VS Code
  • Neovim
  • emacs

📋 Code Review Cheatsheet

What the reviewer should check
Check Description
🚚 Diff size Is the PR small and focused, or should it be broken into smaller PRs?
🧪 Unit tests Are new features covered with appropriate unit tests?
🥼 Acceptance Criteria met Are the Acceptance Criteria listed in the issue met?
🧐 Code clarity Is the code easy to understand? Are variable and function names meaningful?
🧩 Code organization Are files, modules, and functions structured logically?
🪚 Conciseness Is the code free of unnecessary complexity or redundant code?
💬 Comments & documentation Are code comments explaining why and not how? Is the documentation up-to-date?
🧮 Code consistency Does the code follow existing patterns and conventions in the project?
☣️ Function length Are functions too long? Should they be broken into smaller, more focused functions?
☢️ Class design Are classes well-structured and not overly large or doing too much?
🐾 Logging and debugging statements Are print/debug statements removed or replaced with proper logging?

@toastdriven
Copy link
Author

This is almost enough to make things work. However, we need to expose an additional port (default: 5678) on the backend service to enable connecting the debugger. I thought I had something close by adding the following to k8s/base/django.yaml:

  ports:
    - port: 8000
      targetPort: http-server
      name: wsgi
    - port: 5678
      targetPort: 5678
      name: debug

However, while the server still responded on 8000, the debugger was unable to connect on the expected 5678. Help/feedback would be appreciated on exposing a debugging port.

@rochecompaan
Copy link
Contributor

@toastdriven you need to add the port to the resource in Tilt to forward to the debug port:

k8s_resource(workload='backend', port_forwards=[5678,8000])

@toastdriven
Copy link
Author

I have VS Code debugging fully working as of the latest commit. However, there are a couple small things to discuss/address:

  • I'm opening up an extra port (5678) regardless w/ the Kubernetes & Tilt configs, which maybe affects non-local environments? Not sure if there's a better place for that... (maybe a patch or just a different config file)
  • I'm hard-coding 0.0.0.0 in the utils.vscode_debugger. I tried using the DOCKER_GATEWAY_IP, but couldn't get the debugger to connect when that was used. Perhaps I'm doing something wrong? IMO, the hard-coded address is fine for development, but open to thoughts/feedback.
  • I've added a new environment variable (DEBUGGER_PORT), but currently only the Python code uses it. Unsure if Kube/Tilt can read that, & make use of it for the port to open?
  • I've added a (commented-out) command variant to django.yml. This makes interactive debugging work without needing to add breakpoint() to the code. Unsure how we feel about this (leave it & add comments? just add documentation? enable it as default, since many editors use DAP & hence debugpy?)...

@rochecompaan
Copy link
Contributor

rochecompaan commented Sep 12, 2025

We don't want to make VSCode-specific changes in the base layer since all environments will inherit them. They must either go into k8s/local as patches, or, as I recommend, we patch them into k8s/dev if one starts Tilt with an argument like --debugpy using this approach. I took the liberty of doing a quick refactor to demonstrate how this would work.

Comment on lines +5 to +6
* PyCharm
* VS Code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a note saying any DAP-supported IDE?

Comment on lines +25 to +26
PYTHONBREAKPOINT: "" # "utils.pycharm_debugger" for pycharm, "utils.vscode_debugger" for VS Code
DEBUGGER_PORT: "" # "6400" for pycharm, "5678" for VS Code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying it in the repo will require you to git exclude the file, which is not ideal. We can patch in these options using Tilt as well.

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