2020-02-16 15:52:41 -06:00
# Debugging VSCode plugin and the language server
2019-01-18 04:59:08 -06:00
2020-02-16 15:47:12 -06:00
## Prerequisites
2019-01-18 04:59:08 -06:00
2020-02-16 15:52:41 -06:00
- Install [LLDB ](https://lldb.llvm.org/ ) and the [LLDB Extension ](https://marketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb ).
2020-02-16 14:58:17 -06:00
- 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
2021-02-05 09:57:36 -06:00
npm ci
2020-02-16 14:58:17 -06:00
```
## Common knowledge
2020-02-16 15:52:41 -06:00
* All debug configurations open a new `[Extension Development Host]` VSCode instance
where **only** the `rust-analyzer` extension being debugged is enabled.
2020-02-16 14:58:17 -06:00
* 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-05-20 13:01:37 -05:00
- `Run Installed Extension` - runs the extension with the globally installed `rust-analyzer` binary.
- `Run Extension (Debug Build)` - runs extension with the locally built LSP server (`target/debug/rust-analyzer`).
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-05-03 14:14:56 -05: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 15:42:48 -06:00
- By default, the LSP server is built without debug information. To enable it, you'll need to change `Cargo.toml` :
2020-02-16 14:58:17 -06:00
```toml
[profile.dev]
debug = 2
```
2019-01-18 04:59:08 -06:00
2020-05-20 13:01:37 -05:00
- Select `Run Extension (Debug Build)` to run your locally built `target/debug/rust-analyzer` .
2019-01-18 04:59:08 -06:00
2020-02-16 15:42:10 -06:00
- In the original VSCode window once again select the `Attach To Server` debug configuration.
2019-01-18 04:59:08 -06:00
2020-02-18 05:33:16 -06:00
- A list of running processes should appear. Select the `rust-analyzer` from this repo.
2019-01-18 04:59:08 -06:00
2020-10-22 13:04:09 -05:00
- Navigate to `crates/rust-analyzer/src/main_loop.rs` and add a breakpoint to the `on_request` function.
2019-01-18 04:59:08 -06:00
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
2021-01-25 07:38:58 -06:00
If you need to debug the server from the very beginning, including its initialization code, you can use the `--wait-dbg` command line argument or `RA_WAIT_DBG` environment variable. The server will spin at the beginning of the `try_main` function (see `crates\rust-analyzer\src\bin\main.rs` )
```rust
let mut d = 4;
while d == 4 { // set a breakpoint here and change the value
d = 4;
}
```
2021-06-19 20:23:20 -05:00
However for this to work, you will need to enable debug_assertions in your build
```rust
RUSTFLAGS='--cfg debug_assertions' cargo build --release
```
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
2020-02-18 05:33:16 -06:00
### Can't find the `rust-analyzer` process
2019-01-18 04:59:08 -06:00
It could be a case of just jumping the gun.
2020-02-18 05:33:16 -06:00
The `rust-analyzer` is only started once the `onLanguage:rust` activation.
2019-01-18 04:59:08 -06:00
Make sure you open a rust file in the `[Extension Development Host]` and try again.
2020-02-18 05:33:16 -06:00
### Can't connect to `rust-analyzer`
2019-01-18 04:59:08 -06:00
Make sure you have run `echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope` .
2020-05-03 14:14:56 -05:00
By default this should reset back to 1 every time you log in.
2019-01-18 04:59:08 -06:00
### Breakpoints are never being hit
2020-05-03 14:14:56 -05:00
Check your version of `lldb` . If it's version 6 and lower, use the `classic` adapter type.
2019-01-18 04:59:08 -06:00
It's `lldb.adapterType` in settings file.
2020-05-03 14:14:56 -05:00
If you're running `lldb` version 7, change the lldb adapter type to `bundled` or `native` .