Debug a gem or use a local version
It happens to me all the time. I am working with a library and and I want to debug what it does aor try to modify the way it works. I don't mean debug like going in with pry or a debugger, but I just want to see how it works and if I can make it work my way.
There are a few things you can do which we'll explore in this article.
1. Use bundle open
The simplest thing you can do is to use bundle open GEM_NAME
(like bundle open avo
). This will tell your default set editor to open the directory where the gem is installed.
I'm on a mac using chruby
and that path is /Users/adrian/.gem/ruby/3.3.1/gems/GEM_NAME
. That path may differ, but using this command you ensure that you're opening the right gem.
This will enable you to go in and toy around with the code and see the changes in your codebase.
This is great if you want to make a small tweak, but it will be a toil when you have many changes in multiple files.
The second approach might be better for you.
2. Clone the repo and use that
The second approach is a bit more sustainable if you plan to PR those changes you made to the main repo.
You'd go to a repo on GitHub, get its path, and clone it to your machine.
git clone https://github.com/avo-hq/marksmith
If you cd
into that repo and run pwd
to get that absolute path.
▶ cd marksmith
avocado/gems/marksmith
▶ pwd
/Users/adrian/work/avocado/gems/marksmith
Now take that path, go into your project, and update the Gemfile
like so:
# remove this
gem "marksmith"
# add this
gemspec path: "/Users/adrian/work/avocado/gems/marksmith"
# or this
gem "marksmith", path: "/Users/adrian/work/avocado/gems/marksmith"
There are a few differences between these approaches, but I recommend using the gemspec
approach.
This is how we approach debugging everyday at Avo.