| 1 |
# Debug via VS Code { #debug-vscode } |
| 2 |
|
| 3 |
You can use [Visual Studio Code](https://code.visualstudio.com) to debug a development app via `briefcase dev`, or a bundled app that is built via `briefcase build` and run via `briefcase run` - including mobile apps. |
| 4 |
|
| 5 |
## Development |
| 6 |
|
| 7 |
During development on your host system, you can configure Visual Studio Code to start Briefcase. The "Run" menu in VS Code has an "Open Configurations" option; if that option is enabled, select it, and modify the `configurations` key so that it contains an entry like the following: |
| 8 |
|
| 9 |
```json |
| 10 |
{ |
| 11 |
... |
| 12 |
"configurations": [ |
| 13 |
... |
| 14 |
{ |
| 15 |
"name": "Briefcase: Dev", |
| 16 |
"type": "debugpy", |
| 17 |
"request": "launch", |
| 18 |
"module": "briefcase", |
| 19 |
"args": [ |
| 20 |
"dev", |
| 21 |
], |
| 22 |
"justMyCode": false |
| 23 |
} |
| 24 |
] |
| 25 |
} |
| 26 |
``` |
| 27 |
If you need to specify other options to `briefcase dev`, you can specify them in the `args` value. |
| 28 |
|
| 29 |
If "Open Configurations" isn't enabled, select "Add Configuration" instead, and then enter the following details: |
| 30 |
|
| 31 |
* Select "Python Debugger" as the debugger |
| 32 |
* Select "Module" as the debug configuration |
| 33 |
* Enter `briefcase` as the name of the module to run. |
| 34 |
|
| 35 |
This will give you a stub configuration that is mostly correct; edit it to match the example above. The configuration will be saved in a `.vscode/launch.json` file in your project. |
| 36 |
|
| 37 |
To start a debug session, open the debug view in VS Code using the sidebar, select the "Briefcase: Dev" configuration from the list at the top of the sidebar, and select "Start Debugging" from the "Run" menu. This will run `briefcase dev` in a debug session; you can [set breakpoints](https://code.visualstudio.com/docs/debugtest/debugging#_breakpoints), use the [data inspection](https://code.visualstudio.com/docs/debugtest/debugging#_data-inspection), use the [debug console REPL](https://code.visualstudio.com/docs/debugtest/debugging#_debug-console-repl) and all other debugging features of VS Code. For more details about the VS Code configurations in the `.vscode/launch.json` file, see the [VS Code documentation](https://code.visualstudio.com/docs/python/debugging). |
| 38 |
|
| 39 |
## Bundled App |
| 40 |
|
| 41 |
/// warning | Experimental feature |
| 42 |
|
| 43 |
This is currently an **experimental feature** that is only supported on Windows, macOS, iOS and Android. |
| 44 |
|
| 45 |
/// |
| 46 |
|
| 47 |
To debug a bundled app, the app must be modified to include a bootstrap that will connect to the VS Code debugger. This is done by passing the `--debug debugpy` option to `briefcase build`: |
| 48 |
|
| 49 |
```console |
| 50 |
$ briefcase build --debug debugpy |
| 51 |
``` |
| 52 |
|
| 53 |
To build a mobile app, include the platform name in the `build` command - for example: |
| 54 |
|
| 55 |
```console |
| 56 |
$ briefcase build iOS --debug debugpy |
| 57 |
``` |
| 58 |
|
| 59 |
This will build your app in debug mode and add [debugpy](https://code.visualstudio.com/docs/debugtest/debugging#_debug-console-repl), and a package that automatically starts `debugpy` at the startup of your bundled app. |
| 60 |
|
| 61 |
Then, select "Open Configurations" from the "Run" menu, and add a new debug configuration to your `.vscode/launch.json` file: |
| 62 |
|
| 63 |
```json |
| 64 |
{ |
| 65 |
"version": "0.2.0", |
| 66 |
"configurations": [ |
| 67 |
... |
| 68 |
{ |
| 69 |
"name": "Briefcase: Attach", |
| 70 |
"type": "debugpy", |
| 71 |
"request": "attach", |
| 72 |
"connect": { |
| 73 |
"host": "localhost", |
| 74 |
"port": 5678 |
| 75 |
}, |
| 76 |
"justMyCode": false |
| 77 |
} |
| 78 |
] |
| 79 |
} |
| 80 |
``` |
| 81 |
|
| 82 |
You can now start your app in debug mode: |
| 83 |
|
| 84 |
```console |
| 85 |
$ briefcase run --debug debugpy |
| 86 |
``` |
| 87 |
|
| 88 |
To run a mobile app, include the platform name in the `run` command - for example: |
| 89 |
|
| 90 |
```console |
| 91 |
$ briefcase run iOS --debug debugpy |
| 92 |
``` |
| 93 |
|
| 94 |
Running the app in debug mode will automatically start the `debugpy` debugger and listen for incoming connections. By default it will listen on `localhost` and port `5678`. The app will pause at the start of execution, waiting for the debugger to attach. |
| 95 |
|
| 96 |
To attach the debugger, select the Debug sidebar, select the "Briefcase: Attach" configuration, and then select "Start Debugging" from the "Run" menu. The app will then resume, and the debugger will be enabled. You can then use any debugger features you require. |
| 97 |
|
| 98 |
## Debugging quirks |
| 99 |
|
| 100 |
The VS Code debugger has some notable behavior when used to debug a Briefcase app: |
| 101 |
|
| 102 |
- The "Restart" debug control will not work. To restart the app, you need to disconnect the debugger, restart the app manually, running it with `briefcase run --debug debugpy`, and then re-connecting the debugger. |
| 103 |
- `justMyCode` must be set to `false`. When setting it to `true`, or not defining it at all, breakpoints are missed on some platforms (e.g., Windows). The reason for this is currently unknown. |
| 104 |
- `pathMappings` should not be set manually in the `launch.json`. The path mappings will be set by Briefcase programmatically and if setting it manually too the manual setting will overwrite settings by Briefcase. |
| 105 |
- VS Code will not honor any breakpoint in code that is part of a file that is ignored by `.gitignore`. |