Fixing a cryptic fetch error when installing a Ruby gem from a git repository.
A few days ago, I wanted to install a testing branch from a gem in a Ruby project. As Bundler’s How to install gems from git repositories guide documents, you do it by updating your Gemfile as the following:
# Gemfile
gem "shoulda-matchers", git: 'https://github.com/hexdevs/shoulda-matchers.git', branch: 'a-testing-branch'
That worked as expected when installing the shoulda-matchers gem. But when I tried to do the same with faker, I got this error:
Fetching https://github.com/faker-ruby/faker.git
Using cached git data because of network errors:
Git error: command git fetch --force --quiet --no-tags --depth 1 -- <https://github.com/faker-ruby/faker.git> refs/heads/stable:refs/heads/stable in directory /opt/homebrew/lib/ruby/gems/4.0.0/cache/bundler/git/faker-long-id-number has failed.
Revision does not exist in the repository https://github.com/faker-ruby/faker.git. Maybe you misspelled it?
Git error: command git clone --no-checkout --quiet /opt/homebrew/lib/ruby/gems/4.0.0/cache/bundler/git/faker-long-id-number /opt/homebrew/lib/ruby/gems/4.0.0/bundler/gems/faker-another-long-id-number in directory /Users/stefannibrasil/projects/project-repository has failed.
fatal: repository '/opt/homebrew/lib/ruby/gems/4.0.0/cache/bundler/git/faker-long-id-number' does not exist
What a terrible message error. I did not mispell it nor was I experiencing network errors.
It turns out the problem was that I first installed faker using SSH, and not HTTPS. So when I tried to fetch the branch like so:
# Gemfile
gem "faker", git: 'https://github.com/faker-ruby/faker.git', branch: 'a-testing-branch'
It didn’t work because I needed to fetch it using SSH. As the Bundler guide mentions, “Bundler can use HTTP(S), SSH, or git”. Fetching faker using SSH fixed my issue:
# Gemfile
gem "faker", git: 'git@github.com:faker-ruby/faker.git', branch: 'a-testing-branch'
And I was able to test my changes in a real world Ruby project. A happy outcome after being served a bad error message ;)
If you ever encounter a similar error, try fetching the gem using the same connection you used when you cloned it.