cargo: Crates.io index update failing due to conflict with ~/.gitconfig

EDIT: workaround solution at https://github.com/rust-lang/cargo/issues/8172#issuecomment-659066173. EDIT: more precise explanation of this bug at https://github.com/rust-lang/cargo/issues/8172#issuecomment-640973958.


While trying to install delta, I got this error:

cargo build --release

Updating crates.io index error: failed to get ansi_colours as a dependency of package git-delta v0.1.1 (/home/marcospb19/delta) Caused by: failed to fetch https://github.com/rust-lang/crates.io-index

Caused by: failed to authenticate when downloading repository attempted ssh-agent authentication, but none of the usernames git succeeded

Caused by: error authenticating: no auth sock variable; class=Ssh (23) make: *** [Makefile:2: build] Error 101

I know almost nothing about Rust or Cargo, sorry if this issue isn’t very clear. Why would cargo use ssh to make this update?

Notes

Cargo and Rust --version: 1.43.0

OS: Arch Linux

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Comments: 22 (8 by maintainers)

Most upvoted comments

If you want to have a global git configuration that tells git to always use SSH, then there isn’t any choice. All tools using git will use that configuration.

I think, if for whatever reason you really need to use https for Cargo’s index, I think an alternate solution would be to use this in gitconfig:

[url "git@github.com:"]
        insteadOf = https://github.com/

[url "https://github.com/rust-lang/crates.io-index"]
        insteadOf = https://github.com/rust-lang/crates.io-index

I’m not 100% certain that always works, but I did a quick test and it seems ok.

@marcospb19 I have a similar setup, for the same reason, but using pushInsteadOf instead; that way, things that clone/pull from github will not use SSH, but trying to push to github (which cargo never does) will use SSH.

Here’s the configuration you want:

[url "ssh://git@github.com/"]
        pushInsteadOf = "https://github.com/"
        pushInsteadOf = "git://github.com/"

That should solve your problem.

@ehuss’s fix works great for me. I agree with @rjhornsby that at the very least a better message should be printed about the use of insteadOf in gitconfig.

Perhaps adding a section on it to The Cargo Book and linking to it, along with the net.git-fetch-with-cli link, since the actual ‘Caused by’ was different. Maybe say “Possible causes: …” instead of caused by?

Ah, sorry about the confusion. Cargo needs to communicate with GitHub for the index, and the insteadOf git configuration is telling Cargo to use SSH for that purpose. However, Cargo’s SSH behavior doesn’t exactly match that of the git command-line client (it typically needs ssh-agent to be running). The net.git-fetch-with-cli Cargo configuration value tells Cargo to use the git command-line client for git interaction instead of its built-in git library. You can set that up by creating a file called ~/.cargo/config and add the text:

[net]
git-fetch-with-cli = true

Assuming the git CLI is able to communicate with GitHub over SSH, then this should work.