Mac 上 vscode 的 rust-analyzer 扩展无法追踪/跟踪方法定义

解决方法

  • 首先提供一个解决办法,其实比较粗暴。直接卸载 rust-analyzer 然后重新搜索安装该扩展。可以恢复

分析

当我重新安装了 rust-analyzer 之后,我看了开发机上有这样一个进程 xxx/matklad.rust-analyze/rust-analyzer-mac

所以我猜测,应该是要想正常使用 rust-analyzer 扩展,机器上一定要将 server 端(也就是 rust-analyzer-mac)运行起来

因此,如果 rust-analyzer 突然没法使用了,可以尝试主动启动 rust-analyzer-mac 程序,该可执行文件在 Mac 上位于 ~/Library/Application Support/Code/User/globalStorage/matklad.rust-analyzer 目录下。因此只需在命令行执行命令 ~/Library/Application Support/Code/User/globalStorage/matklad.rust-analyzer/rust-analyzer-mac

如果你的系统不是 OSX,是 Win/Linux,对应的 server 程序文件路径也在 rust-analyzer 官方文档中有指出,可以查看官方文档手册

试验

为了验证我的猜想,我先找到机器上运行着的 rust-analyzer-mac 进程,将其 kill 掉。我再去 vscode 上看看,Rust 相关的 ide 功能是否可用。

结果是符合预期,ide 功能无法继续使用。说明 rust-analyzer-mac 是正常使用 Rust ide 功能的关键所在。

此时,我在主动去启动 rust-analyzer-mac 程序。命令行输出如下内容:

Error: ProtocolError("expected initialize request, got Err(RecvError)")

随后,我再去试试 Rust ide 的功能,恢复正常。查看机器上的进程,rust-analyzer-mac 处于正在运行的状态。别着急,这只是表面现象。

在后续的数次使用 vscode 编写 Rust 代码时, 发现主动启动 rust-analyzer-mac 可执行程序,并不能帮助我正确地启动 language server。

但是我发现,当我用 vscode 打开我的 Rust 仓库时,使用 ps -A | grep analyzer 查看进程是否存在,如果没有,我关掉当前的 vscode 窗口,重新打开那个 Rust 仓库,会发现 language server 启动了。

结论

如此看来,rust-analyzer-mac 程序是 rust-analyzer 工具使用的关键,一旦,无法正常使用

  • 1.尝试关掉 vscode 窗口,重新用 vscode 打开 Rust 仓库。
  • 2.可以尝试自己去启动 rust-analyzer-mac 程序。
原文地址:https://www.cnblogs.com/ishenghuo/p/13228778.html