Nani!? Oct 11
Yes, your approach should work, as Ruby's gem resolution system will prefer a locally vendored gem if it is available before falling back to fetching and installing the remote version. Here's a step-by-step guide on how to accomplish this:
Step 1: Create an Empty Gem
You can create an empty gem with the same name as the gem you want to "erase." Here's how to do it:
-
Generate the empty gem:
bash bundle gem erased_gem_name --skip-test --skip-git --skip-coc --skip-ci
Replace
erased_gem_name
with the name of the gem you want to override. -
Update the gemspec file:
- Edit
erased_gem_name.gemspec
to have only the minimal requirements.
ruby Gem::Specification.new do |spec| spec.name = "erased_gem_name" spec.version = "0.0.1" spec.summary = "An empty gem to override the real one." spec.authors = ["Your Name"] spec.email = ["[email protected]"] spec.files = [] spec.license = "MIT" end
- Edit
-
Build the gem:
bash gem build erased_gem_name.gemspec
-
Vendor the gem into your project:
- Create a
vendor/gems
directory (or similar) in your project, and copy the generated gem file into this directory.
- Create a
Step 2: Modify the Gemfile
Update your Gemfile
to point to your vendored gem:
ruby
gem 'erased_gem_name', path: 'vendor/gems/erased_gem_name-0.0.1.gem'
Step 3: Override the Dependency
In your Gemfile
, specify your "erased" gem as a direct dependency so it satisfies the constraints of the transitive dependency.
For example, if you have a gem expensive_gem
which is included as a transitive dependency and has a constraint like >= 1.0
, make sure your vendored erased_gem_name
meets that constraint. You can adjust the version number in the gemspec if necessary.
ruby
gem 'expensive_gem', '>= 1.0', path: 'vendor/gems/erased_gem_name-0.0.1.gem'
Step 4: Install and Test
Run bundle install
to make sure the dependency is correctly overridden:
bash
bundle install
Then verify that bundle show
or bundle list
points to your vendored gem instead of the remote gem.
Important Considerations
- Dependency Version Conflicts: Ensure that your "erased" gem’s version satisfies all the constraints of its dependents. If not, Bundler may still try to install the remote gem.
- Name Clashes: Be cautious if there are other gems or parts of your application that depend on the original gem’s functionality. Your empty gem won’t provide any methods or modules, which might cause
LoadError
orNoMethodError
exceptions in such cases.
This approach is suitable if the goal is simply to skip installing the remote gem and its dependencies while avoiding breaking the dependency graph.
Will it work? Oct 11
Neat!
Test 2 Oct 11
Sup?
Test Oct 11
A b c de fghi jklmnop!
html
This is some HTML
<em>test?</em>
Exemplary Jan 1
This is an example article. You probably want to delete it and write your own articles!