From: eregontp@... Date: 2021-05-25T16:20:10+00:00 Subject: [ruby-core:104040] [Ruby master Feature#17883] Load bundler/setup earlier to make `bundle exec ruby -r` respect Gemfile Issue #17883 has been updated by Eregon (Benoit Daloze). nobu (Nobuyoshi Nakada) wrote in #note-15: > Not true for `-I` at least. > Paths by `-I` in `RUBYOPT` (and `RUBYLIB`) are placed after paths by `-I` in command-line. > ```sh > $ RUBYOPT=-I/c ruby -I/a -I/b -e 'p $:[0,3]' > ["/a", "/b", "/c"] > ``` Mmh, so it's more complex due to unshift'ing into `$:`, but still: ``` $ RUBYOPT=-Ia ruby -Ib -e 'puts $:' /home/eregon/b /home/eregon/a /home/eregon/.rubies/ruby-3.0.1/lib/ruby/site_ruby/3.0.0 ... ``` So "a" is added/processed first, then "b". In your example, it behaves like `$:.unshift("/c")` (from RUBYOPT) and then `$:.unshift("/a", "/b")`. Interesting that the order of `"/a"` and `"/b"` is preserved like that, but still /a & /b are before /c in $LOAD_PATH, and /c is processed first, so it is expected behavior and consistent with `-W`. The important thing in this case is that the `-I` from command line has priority, and that is the case, /a & /b are before /c in `$LOAD_PATH`. Isn't it natural that `-r` in RUBYOPT is processed before `-r` on the command-line? I feel the vast majority of users would expect that. ---------------------------------------- Feature #17883: Load bundler/setup earlier to make `bundle exec ruby -r` respect Gemfile https://bugs.ruby-lang.org/issues/17883#change-92178 * Author: mame (Yusuke Endoh) * Status: Open * Priority: Normal ---------------------------------------- To reproduce the issue, prepare a Gemfile and run `bundle install --path=vendor/bundle`. ``` $ cat Gemfile source "https://rubygems.org" gem "activesupport" $ bundle install --path=vendor/bundle ``` `Kernel#require` respects the Gemfile correctly. ``` $ bundle exec ruby -e 'require "active_support"' ``` However, `bundle exec ruby -ractive_support -e ''` does not. ``` $ bundle exec ruby -ractive_support -e '' :85:in `require': cannot load such file -- active_support (LoadError) from :85:in `require' ``` We can work around the issue by explicitly passing `-rbundler/setup` before `-ractive_support`, but this is very confusing to me. The same issue was discussed in StackOverflow: https://stackoverflow.com/questions/59623068/correct-way-to-combine-bundle-exec-and-ruby-r --- Here is my analysis. `bundle exec` sets `RUBYOPT=-rbundler/setup` which replaces `Kernel#require` with bundler's own definition. `-e 'require "active_support"'` correctly triggers bundler's definition. However, `-ractive_support` is evaluated before `RUBYOPT=-rbundler/setup` is evaluated, so it triggers rubygems' require definition which does not know `vendor/bundle` directory. This is caused by the interpretation order of `RUBYOPT` and command-line arguments. ``` $ RUBYOPT=-r./a ruby -r./b -e '' :b :a ``` For compatibility, I don't think that changing the order is a good idea. IMO, it would be good for ruby interpreter to provide Bundler something special, because Bundler is now bundled with Ruby. My naive idea is to make the interpreter load `ENV["BUNDLE_BIN_PATH"] + "../../lib/bundler/setup"` before any other `-r` options if `BUNDLE_BIN_PATH` is defined. Or another new dedicated environment variable that `bundle exec` sets may work (for example, `RUBY_BUNDLER_SETUP` or something). @deivid @nobu What do you think? -- https://bugs.ruby-lang.org/ Unsubscribe: