Sim
Sim

Reputation: 13528

Dynamically preventing a gem in Gemfile from loading

I have an unusual situation where I need to prevent a specific gem in Gemfile from loading based on the value of an environment variable.

Modifying Gemfile is not a good solution at this time because bundler is used in --deployment mode, which means that any changes to the Gemfile require a git commit, generation of Gemfile.lock by bundle install --no-deployment and then a re-issuance of bundle install --deployment.

I am not familiar with the Bundler codebase and I welcome ideas on how to achieve this without over-hacking/patching Bundler.

Upvotes: 0

Views: 556

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

As far as I know there's not any way to accomplish this if you don't want to touch your Gemfile.

One solution would only require a one-time change to your Gemfile. You could add require: false to the line for the gem in question, making sure the gem's files don't get included by default:

gem 'gem_name', require: false

Then, you could add a file to config/initializers with something like this:

if ENV['MY_ENV_VARIABLE']
  require 'gem_name'
end 

Depending on what the gem does this may or may not work, but it's a possible solution.

Upvotes: 2

Related Questions