👻

Windowsでtauriの開発を始めようとしたら、failed to get cargo metadata

に公開

少しはまったので共有します。
公式の手順通りに環境構築を行っていき、"pnpm run tauri dev"で開発サーバーを立ち上げようとしたところ、次のエラーとなりました。

failed to get cargo metadata: cargo metadata command exited with a non zero exit code: error: rustup could not choose a version of cargo to run, because one wasn't specified explicitly, and no default is configured.
help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.

       Error failed to get cargo metadata: cargo metadata command exited with a non zero exit code: error: rustup could not choose a version of cargo to run, because one wasn't specified explicitly, and no default is configured.
help: run 'rustup default stable' to download the latest stable release of Rust and set it as your default toolchain.

cargoが特定できずに使えないという話のようです。

cargo --version
rustup install stable
rustup default stable

などの結果は特に問題ありませんでした。生成AIに相談しても上記コマンドなどを試せで終わります。
Rustもアンインストールしてから再度インストールしなおしても特に解決せず、ちょっとハマっていたところ、カレントディレクトリに%XDG_DATA_HOME%というフォルダが作成されており、cargo.exeなどがそこに存在することに気づきました。
"rustup install stable"した際に、カレントディレクトリにインストールされたようです。当初はCドライブ直下のツールチェインが使われることを想定していましたが、カレントディレクトリのパスもWindowsがうまく読み取ってcargoなどを実行してくれると思っていたのですが、そうではなかったです。

echo $env:RUSTUP_HOME
%XDG_DATA_HOME%\rustup

と出力され、これではWindowsから読み取ることは不可能です。Linux用の設定が混じったようです。

パスの読み取りに問題があったので、次のコマンドからパスを削除すると、開発サーバーが立ち上がるようになりました。システム環境変数からも同じ設定を削除しておきましょう。

Remove-Item Env:RUSTUP_HOME

Discussion