ruby / ruby/rubygems

When installing bin stubs, enable --env-shebang if File.dirname(Gem.ruby) is in $PATH

Open
#1,049 9 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

category: install status: ready type: feature request
Dominant language
Ruby
Stars
4k
Forks
1.9k
Avg merge
1d 2h
Merged PRs (30d)
81

Description

Problem

Generating absolute #! for rubygem's bin stubs makes it problematic for ruby switchers such as rbenv and chruby. Whenever a ruby is updated or perhaps moved/named, users must run gem pristin --only-executables to re-generating the gem bin stubs with the new path to the ruby executable.

Proposal

Too accommodate both users of ruby switchers and those who directly invoke ruby (ex: /path/to/ruby -S gem install ...), I propose the following behavior when generating bin stubs for newly installed gems:

  1. Check if File.dirname(Gem.ruby) exists within ENV['PATH'].split(File::PATH_SEPARATOR).

    a. If so: enable --env-shebang behavior by default.
    b. If not: use "#!#{Gem.ruby}#{opts}" like normally.

Rationale

If the ruby's bin/ directory exists in $PATH it can be assumed this was intentional and that #!/usr/bin/env ruby would be equivalent to #!/usr/bin/ruby. If the ruby's bin/ directory is not in $PATH, then the user has likely executed it directly by calling /path/to/bin/ruby -S gem install ....

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in lib/rubygems/installer.rb around the referenced bin-stub shebang logic. Trace how Gem.ruby and ENV['PATH'] are used when installing executables, then identify the existing installer coverage to update. Done means PATH detection selects env-style shebangs while direct Ruby paths retain absolute shebangs.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.