2019-01-18 04:59:08 -06:00
# Debugging vs Code plugin and the Language Server
2020-02-16 14:58:17 -06:00
## Prerequsities
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
- Install [LLDB ](https://lldb.llvm.org/ ) and the [LLDB Extension ](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb ) VSCode extensions.
- Open the root folder in VSCode. Here you can access the preconfigured debug setups.
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
< img height = 150px src = "https://user-images.githubusercontent.com/36276403/74611090-92ec5380-5101-11ea-8a41-598f51f3f3e3.png" alt = "Debug options view" >
- Install all TypeScript dependencies
```bash
cd editors/code
2020-02-16 15:36:16 -06:00
npm install
2020-02-16 14:58:17 -06:00
```
## Common knowledge
* All debug configurations open new `[Extension Development Host]` VSCode instance
where **only** your `rust-analyzer` extension is enabled.
* To activate the extension you need to open any Rust project folder in `[Extension Development Host]` .
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
## Debug TypeScript VSCode extension
2019-01-18 04:59:08 -06:00
2020-02-16 15:33:23 -06:00
- `Run Extension` - runs the extension with the globally installed `ra_lsp_server` binary.
2020-02-16 15:35:10 -06:00
- `Run Extension (Dev Server)` - runs extension with the locally built LSP server (`target/debug/ra_lsp_server`).
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
TypeScript debugging is configured to watch your source edits and recompile.
2020-02-16 15:38:25 -06:00
To apply changes to an already running debug process press < kbd > Ctrl+Shift+P</ kbd > and run the following command in your `[Extension Development Host]`
2020-02-16 14:58:17 -06:00
```
> Developer: Reload Window
```
## Debug Rust LSP server
2020-02-16 15:40:31 -06:00
- When attaching a debugger to an already running `rust-analyzer` server on Linux you might need to enable `ptrace` for unrelated processes by running:
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
```
echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
```
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
This enables ptrace on non-forked processes
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
- Enable debug symbols in `Cargo.toml` :
```toml
[profile.dev]
debug = 2
```
2019-01-18 04:59:08 -06:00
2020-02-16 15:21:11 -06:00
- Select `Run Extension (Dev Server)` to run your locally built `target/debug/ra_lsp_server` .
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
- In the original VSCode window once again select `Attach To Server` debug configuration.
2019-01-18 04:59:08 -06:00
2020-02-16 14:58:17 -06:00
- A list of running processes should appear. Select the `ra_lsp_server` from this repo.
2019-01-18 04:59:08 -06:00
- Navigate to `crates/ra_lsp_server/src/main_loop.rs` and add a breakpoint to the `on_task` function.
2020-02-16 15:35:41 -06:00
- Go back to the `[Extension Development Host]` instance and hover over a Rust variable and your breakpoint should hit.
2019-01-18 04:59:08 -06:00
## Demo
2020-02-16 14:58:17 -06:00
- [Debugging TypeScript VScode extension ](https://www.youtube.com/watch?v=T-hvpK6s4wM ).
- [Debugging Rust LSP server ](https://www.youtube.com/watch?v=EaNb5rg4E0M ).
2019-01-18 04:59:08 -06:00
## Troubleshooting
### Can't find the `ra_lsp_server` process
It could be a case of just jumping the gun.
The `ra_lsp_server` is only started once the `onLanguage:rust` activation.
Make sure you open a rust file in the `[Extension Development Host]` and try again.
### Can't connect to `ra_lsp_server`
Make sure you have run `echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope` .
By default this should reset back to 1 everytime you log in.
### Breakpoints are never being hit
Check your version of `lldb` if it's version 6 and lower use the `classic` adapter type.
It's `lldb.adapterType` in settings file.
If you're running `lldb` version 7 change the lldb adapter type to `bundled` or `native` .