Skip to content

Conversation

@Edouard-chin
Copy link
Contributor

Always install the bundler gem in the system gems path

Fix #8106

Problem

Bundler autoswitch feature kicks in unexpectedly which ends up creating issue as described in #8106.

This patch solves this problem, but will also prevent Bundler from autoswitching most of the time, thanks to picking up the right bundler version from the start.

Details

When bundle install is ran, Bundler reads the Gemfile.lock andcheck the BUNDLED WITH section to see if the runtime bundler version matches. If not, Bundler install the version requested by the Gemfile.lock and put that new bundler gem inside the BUNDLE_PATH configured.

Installing the bundler gem inside the BUNDLE_PATH instead of system gems is the main reason the Bundler autoswitch feature exists.
In this patch, I propose that we always install bundler inside system gems.

By doing this, when a call to require 'bundler' is encountered, RubyGems will be able to detect all bundler version that exists on the user's machine and be able to pick the right bundler version, without doing this kernel exec gymnastic.

Right now, if the bundler version specified in the Gemfile.lock is installed outside system gems, then RubyGems will activate the latest bundler version found in system gems, then, when bundler is loaded, Bundler autoswitch kicks in which modifies GEM_HOME (with whatever the user configured in BUNDLE_PATH) and see that another bundler version matching the Gemfile.lock exists.
It then re-exec with Kernel.exec and override the GEM_HOME env, so that when the script is reexecuted and the require 'bundler' is encountered again, RubyGems will search for bundler in the correct folder.

- Fix ruby#8106
- ### Problem

  Bundler autoswitch feature kicks in unexpectedly which ends up
  creating issue as described in ruby#8106.

  This patch solves this problem, but will also prevent Bundler from
  autoswitching most of the time, thanks to picking up the right
  bundler version from the start.

  ### Details

  When `bundle install` is ran, Bundler reads the Gemfile.lock and
  check the `BUNDLED WITH` section to see if the runtime bundler
  version matches. If not, Bundler install the version requested
  by the Gemfile.lock and put that new `bundler` gem inside the
  `BUNDLE_PATH` configured.

  Installing the `bundler` gem inside the `BUNDLE_PATH` instead of
  system gems is the main reason the Bundler autoswitch feature
  exists. In this patch, I propose that we always install `bundler`
  inside system gems.

  By doing this, when a call to `require 'bundler'` is encountered,
  RubyGems will be able to detect *all* `bundler` version that exists
  on the user's machine and be able to pick the right bundler version,
  without doing this kernel exec gymnastic.

  Right now, if the bundler version specified in the Gemfile.lock is
  installed outside system gems, then RubyGems will activate the latest
  bundler version found in system gems, then, when bundler is loaded,
  Bundler autoswitch kicks in which modifies `GEM_HOME` (with whatever
  the user configured in `BUNDLE_PATH`) and see that another bundler
  version matching the Gemfile.lock exists.
  It then re-exec with `Kernel.exec` and override the `GEM_HOME` env,
  so that when the script is reexecuted and the `require 'bundler'` is
  encountered again, RubyGems will search for bundler in the
  correct folder.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bundler unexpectly Kernel.exec on require 'bundler/setup' and loses all command-line flags and CWD

1 participant