Cargo Error: failed to get `x` as a dependency of package解决方法
在使用Cargo运行或编译有依赖的程序时,可能会出现类似下面这种错误:
PS D:\hocn\Desktop\rust\a1> cargo run Updating crates.io index error: failed to get `image` as a dependency of package `a1 v0.1.0 (D:\hocn\Desktop\rust\a1)` Caused by: failed to load source for dependency `image` Caused by: Unable to update registry `crates-io` Caused by: failed to fetch `https://github.com/rust-lang/crates.io-index` Caused by: failed to authenticate when downloading repository: git@github.com:rust-lang/crates.io-index * attempted ssh-agent authentication, but no usernames succeeded: `git` if the git CLI succeeds then `net.git-fetch-with-cli` may help here https://doc.rust-lang.org/cargo/reference/config.html#netgit-fetch-with-cli Caused by: error authenticating: unable to connect to agent pipe; class=Ssh (23)
原因
默认情况下,Cargo是通过内置的git库来下载依赖的,有时会因为验证原因导致失败;
解决办法
- 找到.cargo目录,以Windows系统为例:C:\Users\hocn\.cargo,注意用户名;
- 进入.cargo目录,编辑文件config.toml,如没有则创建;
- 写入以下内容:
[net]
git-fetch-with-cli = true
git-fetch-with-cli
默认为false
,表示使用内置的git库来下载依赖;为true
时,使用git程序来下载依赖,这样就不存在验证失败的问题;
请确保你安装了git程序并正确配置;