Rust:lgit拉取代码失败: if the git CLI succeeds then `net.git-fetch-with-cli` may h

164 阅读1分钟

当我们在做rust开发,编译代码的时候,可能会出现某些依赖无法下载,并且git报错,如下:

Caused by:
  failed to authenticate when downloading repository

  * 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:
  no authentication methods succeeded

问题产生的原因是:

Cargo 默认用内置的 libgit2 拉 Git 依赖。失败的依赖对 libgit2 的 SSH-Auth 不兼容,只要让 Cargo 走 git CLI 而不是 libgit2,此问题就会消失。

解决方式:

永久解决

# ① 全局生效
mkdir -p ~/.cargo
cat >> ~/.cargo/config.toml <<'EOF'
[net]
git-fetch-with-cli = true
EOF

# ② 清一次失败的缓存(可选)
rm -rf ~/.cargo/git/db/mpc-enclave-*

# ③ 重新编译
cargo build            # 或 cargo c

只生效一次

from Pomelo_刘金

CARGO_NET_GIT_FETCH_WITH_CLI=true cargo build
# 或
cargo --config net.git-fetch-with-cli=true build