Created VSCode Debugging for Frappe Python (markdown)

Shivam Mishra
2018-12-19 17:16:33 +05:30
parent 785832bb85
commit cc0be8c90f

@@ -0,0 +1,93 @@
![screen shot 2018-10-08 at 12 52 10 pm](https://user-images.githubusercontent.com/8528887/46596236-6aaa1900-caf9-11e8-8fe7-88e9be48ba24.png)
Checklist for proper functioning.
- [ ] Update `Procfile`
- [ ] Get Visual Studio Code (duh!)
- [ ] Install [Python Extension for VS Code](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
- [ ] Update `launch.json`
- [ ] Start Debugging
### Caveats
1. Disables Auto Reload Feature (However You can achieve the same results by manual reload (⌘⇧F5))
2. Disables Werkzeug Multithreading
### How To
#### 1. Update `Procfile`
**Caution: This modifies the behaviour of `bench start`**
Comment out a line (prepend # to it) from the `Procfile` (located in the bench directory) that looks like this.
```
web: bench serve --port 8000
```
We will run this process from VS code instead of running it with `bench start`.
#### 2. Update `launch.json`
Add a configuration to your `launch.json` in VS Code that should look something like this (This more or less does exactly what the removed line from Procfile does).
```
{
"name": "Bench",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/frappe/frappe/utils/bench_helper.py",
"args": [
"frappe", "serve", "--port", "8000", "--noreload", "--nothreading"
],
"pythonPath": "${workspaceFolder}/../env/bin/python",
"cwd": "${workspaceFolder}/../sites",
"env": {
"DEV_SERVER": "1"
}
}
```
Paths mentioned in given configuration assumes that you have `apps` directory as your workspace directory (The directory you open `code` with). `workspaceFolder` is a vs code variable that points to (if it's not obvious from its name) workspace directory.
You are not forced to use `apps` as your workspace directory, however do remember to change `workspaceFolder`, `pythonPath` and `cwd` accordingly.
#### 3. Execute `bench start`
This should be kept running as usual.
#### 4. Start debugging
VS Code -> Debug Panel (⌘⇧D) -> Start Debugging or With a keyboard shortcut(⌘⇧F5).
### Explanation
#### 1. `program` and `args`
```
"program": "${workspaceFolder}/frappe/frappe/utils/bench_helper.py",
"args": ["frappe", "serve", "--port", "8000", "--noreload", "--nothreading"],
```
Does exact same thing as `bench serve --port 8000 --noreload --nothreading` which is same as
```
cd sites
../env/bin/python ../apps/frappe/frappe/utils/bench_helper.py frappe serve --port 8000 --noreload --nothreading
```
`--noreload` diasbles werkezeug's autoreload fetaure and `--nothreading` disables multithreading.
#### 2. `cwd`
```
"cwd": "${workspaceFolder}/../sites",
```
Above command must be executed from `sites` directory.
#### 3. `env`
```
"env": {
"DEV_SERVER": "1"
}
```
`bench start` creates an environment variable `DEV_SERVER` and set it to `1`. Socket.io doesn't work correctly without this (long story).
Running only `bench serve` doesn't set this variable so you need to explicitly set it.
### Details
Currently, frappe runs with `use_reloader=True` and `threaded=True`, VS Code Debugger for some reason doesn't play well with these features, [Django and Flask](https://code.visualstudio.com/docs/python/debugging#_debugging-specific-app-types) also have this problem.