How to make gitconfig's InsteadOf work with Cargo?

Viewed 2046

I have the following in my ~/.gitconfig:

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

Unfortunately today I discovered that this prevents me from making cargo build download dependencies for me:

    Updating crates.io index
error: failed to get `libc` as a dependency of package `hilbert-c2rust v0.1.4 (/home/d33tah/workspace/profound/hilbert-c2rust)`

Caused by:
  failed to load source for dependency `libc`

Caused by:
  Unable to update registry `https://github.com/rust-lang/crates.io-index`

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:
  no authentication available

Is there a workaround for this problem? Commenting out the lines temporarily helps, but I'd like to keep them because they solve a problem related to Github for me.

4 Answers

Set env var CARGO_NET_GIT_FETCH_WITH_CLI to true.

Try this (from here):

[url "ssh://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

For some reason creating an ssh-agent and adding your identities to the agent did it for me.

eval `ssh-agent -s`
ssh-add
cargo build

I believe this fix is particular to cargo because you can still pull and push to private repositories and yet the authentication will fail on cargo update.

This also occurs when the user's git is configured with ssh and cargo is unable to find the key configured.

To solve this navigate to '.ssh' file and run ssh-add id_rsa and then try again.

This is the only solution that worked for me after hours of head hunting.

Related