概要
VSCode の拡張機能である、rust-analyzer が反応しない、という事態に遭遇しました。
この解決策が地味に大変だったので記事化しました。
rust-analyzer はどんな拡張機能?
VSCode 上でプログラミング言語 Rust を使った開発をする際に利用できる、自動 linter / formatter 的な役割のツールです。
とても便利です。
動作環境
tool | version |
---|---|
Ubuntu | 22.04.5 LTS |
vscode | 1.95.3 |
cargo | 1.75.0 |
rust-analyzer | v0.3.2457 |
rust-src, libssl-dev, rustup は install されていなかった。
エラーメッセージ例
いろんなエラーメッセージに遭遇したので、とりあえず全部載せておきます。
- Cargo.toml が見つからないことによるっぽいエラー
- rust-src が無いことによるエラー
- libssl-dev が無いことによるエラー
- 原因不明のエラー
- cargo / toolchain が 1.78.0 以上じゃないと rust-analyzer は動かないというエラー
1. Cargo.toml が見つからないことによるっぽいエラー
rust analyzer failed to find any projects in [AbsPathBuf(
rust-analyzer failed to fetch workspace
ERROR notification handler failed handler=textDocument/didSave error=file not found:
2. rust-src が無いことによるエラー
2025-05-12T08:44:16.967362518Z ERROR can't load standard library, try installing `rust-src` sysroot_path=/usr
3. libssl-dev が無いことによるエラー
--- stderr
thread 'main' panicked at /home/ubuntu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/openssl-sys-0.9.88/build/find_normal.rs:190:5:
Could not find directory of OpenSSL installation, and this `-sys` crate cannot
proceed without this knowledge. If OpenSSL is installed and this crate had
trouble finding it, you can set the `OPENSSL_DIR` environment variable for the
compilation process.
Make sure you also have the development packages of openssl installed.
For example, `libssl-dev` on Ubuntu or `openssl-devel` on Fedora.
If you're in a situation where you think the directory *should* be found
automatically, please open a bug at https://github.com/sfackler/rust-openssl
and include information about your system as well as this message.
$HOST = x86_64-unknown-linux-gnu
$TARGET = x86_64-unknown-linux-gnu
openssl-sys = 0.9.88
stack backtrace:
0: rust_begin_unwind
at /build/rustc-kAv1jW/rustc-1.75.0+dfsg0ubuntu1~bpo0/library/std/src/panicking.rs:645:5
1: core::panicking::panic_fmt
at /build/rustc-kAv1jW/rustc-1.75.0+dfsg0ubuntu1~bpo0/library/core/src/panicking.rs:72:14
2: core::panicking::panic_display
3: build_script_main::find_normal::find_openssl_dir
4: build_script_main::find_normal::get_openssl::{{closure}}
5: core::option::Option<T>::unwrap_or_else
6: build_script_main::find_normal::get_openssl
7: build_script_main::find_openssl
8: build_script_main::main
9: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
4. 原因不明のエラー
2025-05-12T08:56:05.160994344Z ERROR FetchBuildDataError: error: failed to run custom build command for `yeslogic-fontconfig-sys v3.2.0`
note: To improve backtraces for build dependencies, set the CARGO_PROFILE_DEV_BUILD_OVERRIDE_DEBUG=true environment variable to enable debug information generation.
Caused by:
process didn't exit successfully: `/home/ubuntu/hoge/target/debug/build/yeslogic-fontconfig-sys-ce8a3e14a7869e7f/build-script-build` (exit status: 101)
--- stdout
cargo:rerun-if-env-changed=RUST_FONTCONFIG_DLOPEN
cargo:rerun-if-env-changed=FONTCONFIG_NO_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG
cargo:rerun-if-env-changed=PKG_CONFIG
cargo:rerun-if-env-changed=FONTCONFIG_STATIC
cargo:rerun-if-env-changed=FONTCONFIG_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_STATIC
cargo:rerun-if-env-changed=PKG_CONFIG_ALL_DYNAMIC
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_PATH_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_PATH
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_LIBDIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64-unknown-linux-gnu
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR_x86_64_unknown_linux_gnu
cargo:rerun-if-env-changed=HOST_PKG_CONFIG_SYSROOT_DIR
cargo:rerun-if-env-changed=PKG_CONFIG_SYSROOT_DIR
--- stderr
thread 'main' panicked at /home/ubuntu/.cargo/registry/src/index.crates.io-6f17d22bba15001f/yeslogic-fontconfig-sys-3.2.0/build.rs:8:48:
called `Result::unwrap()` on an `Err` value: "`PKG_CONFIG_ALLOW_SYSTEM_CFLAGS=\"1\" PKG_CONFIG_ALLOW_SYSTEM_LIBS=\"1\" \"pkg-config\" \"--libs\" \"--cflags\" \"fontconfig\"` did not exit successfully: exit status: 1\nerror: could not find system library 'fontconfig' required by the 'yeslogic-fontconfig-sys' crate\n\n--- stderr\nPackage fontconfig was not found in the pkg-config search path.\nPerhaps you should add the directory containing `fontconfig.pc'\nto the PKG_CONFIG_PATH environment variable\nNo package 'fontconfig' found\n"
stack backtrace:
0: rust_begin_unwind
at /build/rustc-kAv1jW/rustc-1.75.0+dfsg0ubuntu1~bpo0/library/std/src/panicking.rs:645:5
1: core::panicking::panic_fmt
at /build/rustc-kAv1jW/rustc-1.75.0+dfsg0ubuntu1~bpo0/library/core/src/panicking.rs:72:14
2: core::result::unwrap_failed
at /build/rustc-kAv1jW/rustc-1.75.0+dfsg0ubuntu1~bpo0/library/core/src/result.rs:1653:5
3: core::result::Result<T,E>::unwrap
4: build_script_build::main
5: core::ops::function::FnOnce::call_once
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
5. toolchain 1.78.0 以上じゃないと rust-analyzer は動かないというエラー
Workspace /home/ubuntu/repo_name/hoge/Cargo.toml is using an outdated toolchain version 1.75.0 but rust-analyzer only supports 1.78.0 and higher. Consider using the rust-analyzer rustup component for your toolchain or upgrade your toolchain to a supported version.
解決策
settings.json 内の "rust-analyzer.linkedProjects"
の値に、実際に存在する Cargo.toml
のパスを書く。以下のように書く。
.vscode/settings.json
{
"rust-analyzer.linkedProjects": [
"/home/ubuntu/repo_name/hoge/Cargo.toml"
],
}
以下のコマンドを実行して一つずつエラー原因を潰していく。
vscode
sudo apt update
# この辺がないと動かない
sudo apt install cargo
sudo apt install rust-src
# これも必要らしい
sudo apt-get install openssl libssl-dev
# cargo のバージョン確認 自分は 1.75.0 だった
cargo --version
# これで rustup を install する
# rustup を入れることによって cargo とかをアップデートできるようになる
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env
# これで cargo がアップデートされる 自分は 1.86.0 になった
rustup update
この後、 VSCode を一度終了して再度立ち上げると良い。
関連記事
Cargo.toml が見つからないことの対策
libssl-dev が無いことの対策