编辑器中Rust宏展开异常:proc-macro server API版本高于rust-analyzer
Rust 1.77.1更新后rust-analyzer proc宏服务API版本不兼容问题
问题详情
通过rustup update将Rust工具链更新至1.77.1后,Neovim中出现rust-analyzer错误:
rust-analyzer: proc macro `main` not expanded: Failed to run proc-macro server from path /Users/user/.rustup/toolchains/.../libexec/rust-analyzer-proc-macro-srv, error: Custom { kind: Other, error: "proc-macro server's api version (4) is newer than rust-analyzer's (2)" }
该问题导致宏生成符号无代码补全,编辑器出现大量错误提示。当前工具链版本为1.77.1,rust-analyzer版本也显示为1.77.1,但rust-analyzer-proc-macro-srv无法通过--version直接查看版本,仅返回内部使用提示。
原因分析
错误本质是rust-analyzer核心与proc-macro服务的内部API版本不匹配。即使两者表面版本都是1.77.1,也可能因为以下原因出现差异:
- rust-analyzer安装来源不一致:比如通过Neovim插件(如mason)安装的rust-analyzer,与rustup toolchain自带的proc-macro-srv并非同一构建版本
- 缓存残留:旧版本的rust-analyzer或proc-macro-srv缓存未清理,导致实际运行版本不匹配
解决方案(优先不降级工具链)
1. 统一rust-analyzer安装来源
确保rust-analyzer与proc-macro-srv来自同一工具链:
- 卸载第三方插件安装的rust-analyzer(如mason中移除rust-analyzer)
- 通过rustup安装rust-analyzer组件:
rustup component add rust-analyzer - 在Neovim配置中指定rustup提供的rust-analyzer路径(以Apple Silicon为例):
vim.g.rust_analyzer_path = vim.fn.expand("~/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rust-analyzer")
2. 清理缓存并重装组件
- 清理Neovim中rust-analyzer的缓存目录:
rm -rf ~/.local/share/nvim/rust-analyzer ~/.cache/nvim/rust-analyzer - 清理rustup组件缓存并重装rust-analyzer:
rustup component clean rust-analyzer rustup component remove rust-analyzer rustup component add rust-analyzer
3. 验证proc-macro-srv版本
通过内部环境变量查看proc-macro-srv的实际版本:
RUST_ANALYZER_INTERNALS_DO_NOT_USE='this is unstable' ~/.rustup/toolchains/stable-aarch64-apple-darwin/libexec/rust-analyzer-proc-macro-srv --version
输出版本应与rust-analyzer完全一致。
4. 检查Neovim插件冲突
- 确保仅启用一套rust-analyzer相关配置(如避免同时使用
rust-tools.nvim和原生nvim-lspconfig的rust_analyzer配置) - 禁用插件中自动更新rust-analyzer的功能,改用rustup统一管理版本
内容的提问来源于stack exchange,提问作者K Kafara
相关产品推荐
相关产品推荐

