0

My library uses a Gem that loads libraries dynamically. For example, it loads 'sqlite3' if I choose an sqlite adapter.

The problem is, if I write in my library

require 'bundler'
Bundler.setup

it stops seeing the gems installed with rubygems and only sees the ones that are installed for this particular lib using Bundler. Thus

require 'sqlite3'

causes an Exception:

`require': LoadError: cannot load such file -- sqlite3

Is there a way to fix this without adding sqlite3 into my own Gemfile?

I should point out, this problem has nothing to do with sqlite3, it seems, but rather with Bundler behavior. sqlite3 was chosen simply because that was the gem that I required that helped me discover this problem.

orion3
  • 9,797
  • 14
  • 67
  • 93

1 Answers1

2

When you use Bundler.setup it explicitly changes your load path so you only use the gems specified in your Gemfile. It's a desired behaviour to ensure you do not use anything you didn't specify in your Gemfile and to prevent unexpected dependency conflicts.

when dynamically loading gems you should use the require false option:

gem "sqlite3", :require => false

This will cause bundler to install sqlite3 but only require it when needed.

See Bundler Gemfile manual

dfherr
  • 1,633
  • 11
  • 24