BUNDLE_WITHOUT not working with development group

10 Aug 2021 in TIL

tl;dr: Make sure that your dependency isn't in multiple groups

Given the following Gemfile which installs rspec as both a test and development dependency:

ruby
source "https://rubygems.org"
group :test, :development do
gem "rspec"
end

Running BUNDLE_WITHOUT="test" bundle install will still install rspec - why?

bash
❯ BUNDLE_WITHOUT="test" bundle install
Using bundler 1.17.2
Using diff-lcs 1.4.4
Using rspec-support 3.10.2
Using rspec-core 3.10.1
Using rspec-expectations 3.10.1
Using rspec-mocks 3.10.2
Using rspec 3.10.0
Bundle complete! 1 Gemfile dependency, 7 gems now installed.
Gems in the group test were not installed.
Bundled gems are installed into `./vendor/bundle`

Setting the BUNDLE_WITHOUT environment variable is equivalent to running bundle install --without test

It turns out that BUNDLE_WITHOUT must match all groups, not any groups. You can provide multiple groups by adding a colon between them like so:

bash
❯ BUNDLE_WITHOUT="test:development" bundle install
Using bundler 1.17.2
Bundle complete! 1 Gemfile dependency, 1 gem now installed.
Gems in the groups test and development were not installed.
Bundled gems are installed into `./.vendor/bundle`

The solution here is to provide both groups to BUNDLE_WITHOUT or only put your dependencies in a single group.