Closed thargy closed 2 weeks ago
That is not something for this repository. You need to seek help elsewere.
OK, fair enough. After many hours of head scratching and searching, I found the issue and figured out a solution.
I hope you can figure me posting here because:
A discussion of the problem can be found here.
In short, this has been a known problem with HA's debugpy integration since the change to Python 3.12, which means that frozen modules now need to be explicitly turned off while debugging. Unfortunately, there's no 'easy' way to disable the frozen modules, at least until Python 3.13, which adds an environmental variable.
Further running the homeassistant
module directly from Python, with the -Xfrozen_modules=off
was not sufficient to use the debugpy integration, which would still complain of frozen modules in the console output:
2024-10-16 17:16:26.345 INFO (MainThread) [homeassistant.setup] Setting up debugpy
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
2024-10-16 17:16:26.868 WARNING (MainThread) [homeassistant.components.debugpy] Waiting for remote debug connection on localhost:5678
I found the best way to enable debugging was to add a scripts\debug
file to start homeassistant and debugpy directly without frozen modules:
#!/usr/bin/env bash
set -e
cd "$(dirname "$0")/.."
# Create config dir if not present
if [[ ! -d "${PWD}/config" ]]; then
mkdir -p "${PWD}/config"
hass --config "${PWD}/config" --script ensure_config
fi
# Set the path to custom_components
## This let's us have the structure we want <root>/custom_components/area_tree
## while at the same time have Home Assistant configuration inside <root>/config
## without resulting to symlinks.
export PYTHONPATH="${PYTHONPATH}:${PWD}/custom_components"
# Start Home Assistant
#
# See https://github.com/home-assistant/core/issues/110623#issuecomment-2266933072
# Also see https://docs.python.org/3/using/cmdline.html#envvar-PYTHON_FROZEN_MODULES for alternate fix when HA moves to Python 13, using env in launch.json
# Do not set debugpy in configuration.yaml if adding debugpy in command line here...
# To start and wait for remote debugger
python3 -Xfrozen_modules=off -m debugpy --listen localhost:5678 -m homeassistant --config ${PWD}/config
For consistency, I renamed scripts\develop
to scripts\run
and updated tasks.json
:
{
"version": "2.0.0",
"tasks": [
{
"label": "Run Home Assistant",
"type": "shell",
"command": "scripts/run",
"problemMatcher": []
},
{
"label": "Debug Home Assistant",
"type": "shell",
"command": "scripts/debug",
"isBackground": true,
"problemMatcher": [
{
"owner": "homeassistant",
"pattern": [
{
"regexp": "(.?)",
"file": 1,
"location": 1,
"message": 1
}
],
"background": {
"activeOnStart": true,
"beginsPattern": ".?",
"endsPattern": "^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3}",
}
}
]
},
{
"label": "Terminate Debug",
"command": "echo ${input:terminate}",
"type": "shell",
"problemMatcher": []
}
],
"inputs": [
{
"id": "terminate",
"type": "command",
"command": "workbench.action.tasks.terminate",
"args": "Debug Home Assistant"
}
]
}
Alongside the original Run Home Assistant
task, I added a Debug Home Assistant
which executes scripts\debug
as a background task, allowing the debugger to attach once the logs start to appear (see endsPattern
regex). I also added a Terminate Debug
task to kill the home assistant instance when the debugger disconnects.
The following launch.json
then allows F5
debugging - which runs home assistant and attaches a debugger, and cleans up after the debugger is detached:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Debug Home Assistant",
"type": "debugpy",
"request": "attach",
"connect": {
"host": "localhost",
"port": 5678
},
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "."
}
],
"preLaunchTask": "Debug Home Assistant",
"postDebugTask": "Terminate Debug",
"justMyCode": false
}
]
}
There's no reason for this launch.json
to be excluded from the repository; it doesn't change because it is run in a container.
When HA moves to Python 3.13, it may be possible to return to a single script for running/debugging that calls hass
directly and runs as a service in debug mode. This is guaranteed though, and even if there is a better solution at that time (including supporting the debugpy integration directly), the above launch settings and tasks would still allow F5
debugging out of the box.
Although a relatively minor set of changes, the time saved for anyone wanting to debug using vscode will be massive. I'd be more than willing to submit a PR if you would be willing to consider it for inclusion?
Not being able to debug on vs code was the first thing I noticed when I switched Home Assistant development environment. The .vscode folder has the launch.json and extensions.json.
System Health details
System Information
Home Assistant Cloud
logged_in | false -- | -- can_reach_cert_server | ok can_reach_cloud_auth | ok can_reach_cloud | okDashboards
dashboards | 2 -- | -- resources | 0 views | 0 mode | storageRecorder
oldest_recorder_run | October 16, 2024 at 4:03 PM -- | -- current_recorder_run | October 16, 2024 at 6:16 PM estimated_db_size | 0.50 MiB database_engine | sqlite database_version | 3.40.1Checklist
Describe the issue
Firstly, thank you for this repo which has been a real headstart.
I have managed to get HA running and added the integration. I have also managed to attach the remote debugger in VSCode, however I cannot get it to hit any breakpoints.
Reproduction steps
Reproduction steps
Start HA using Run Task -
tasks.json
:It will pause, with the following showing in the console
Attach debugger -
launch.json
:On attaching, the console will continue outputting messages, indicating a successful attachment.
Result
Reloading the integration will not hit the breakpoint, nor will any other breakpoints cause the debugger to pause. The debugger will show running threads.
Debug logs