Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support GEM_HOME, add limited support for user-installed gems #1436

Merged
merged 2 commits into from
Sep 26, 2022

Conversation

mislav
Copy link
Member

@mislav mislav commented Sep 26, 2022

The rehash operation will now discover and create shims for executables in additional locations:

  • ~/.gem/ruby/<version>/bin/*
  • $GEM_HOME/bin

The rbenv which (and thus rbenv exec) command will also search these locations when looking up a command. This enables shims to dispatch calls to executables added by GEM_HOME=/my/path gem install or gem install --user-install.

Note that this support is limited:

  • It will only work with C Ruby, as it's difficult to guess the ~/.gem/<engine>/<version> directory for other Rubies without actually invoking the Ruby interpreter, which is slow;
  • It will only work for RBENV_VERSION values in the format X.Y.Z and not "system".

Fixes #1232, fixes #1382
Closes #639
Supersedes https://github.com/mislav/rbenv-user-gems

The rehash process will now discover executables in additional locations:
- `~/.gem/ruby/<version>/bin/*`
- `$GEM_HOME/bin`

The `rbenv which` (and thus `rbenv exec`) command will also search these locations when looking up a command. This enables shims to dispatch calls to executables added by `gem install --user-install`.

Note that this support is limited:
- It will only work with C Ruby, as it's difficult to guess the `~/.gem/<engine>/<version>` directory for other Rubies without actually loading Ruby;
- It will only work for RBENV_VERSION values in the format `X.Y.Z` and not "system".
@mislav mislav merged commit 959968c into master Sep 26, 2022
@mislav mislav deleted the user-gems-support branch September 26, 2022 00:57
@hsbt
Copy link
Member

hsbt commented Sep 26, 2022

👍

@konsolebox
Copy link

Any thoughts on just moving the default user directory to another directory instead, like ~/.rbenv/user? Using ~/.gem may conflict with gems installed using system.

@konsolebox
Copy link

With the following code I am able to define a different user directory, and in this case the user directory becomes the default directory itself, but having this new feature that discovers ~/.gem will make avoiding use of ~/.gem pointless.

/etc/rbenv.d/install/custom_defaults/operating_system.rb:

# frozen_string_literal: true

module Gem
  module OperatingSystemDefaults
    def user_dir
      default_dir
    end

    def default_path
      path = []
      path << default_dir
      path << vendor_dir if vendor_dir && File.directory?(vendor_dir)
      path
    end
  end

  self.singleton_class.prepend OperatingSystemDefaults
end

/etc/rbenv.d/install/custom_defaults.bash:

function _after_install_hook {
	local defaults_rb_file defaults_dir_path
	[[ ${PREFIX} && -d ${PREFIX} ]] || return
	defaults_rb_file=$(find "${PREFIX}" -path '*/rubygems/defaults.rb')
	[[ ${defaults_rb_file} == */rubygems/defaults.rb ]] || return
	defaults_dir_path=${defaults_rb_file%.rb}
	mkdir -p "${defaults_dir_path}" || return
	cp "${BASH_SOURCE%.*}/operating_system.rb" "${defaults_dir_path}/"
}

after_install _after_install_hook

I hope there's a way to disable the functionality at least if it will be kept.

@mislav
Copy link
Member Author

mislav commented Oct 1, 2022

Any thoughts on just moving the default user directory to another directory instead, like ~/.rbenv/user? Using ~/.gem may conflict with gems installed using system.

How does the conflict happen in a real-world scenario, can you elaborate a bit more please? Also, feel free to open an issue if this change concerns you.

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.

Add a warning or error if GEM_HOME is set cannot find --user-installd gem executable
3 participants