ruby / ruby/rubygems

[feature request] Introduce a way to set environment for subprocess only when running `bundle exec`

Open
#8,824 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bundler
Dominant language
Ruby
Stars
4k
Forks
1.9k
Avg merge
1d 2h
Merged PRs (30d)
81

Description

Describe the problem as clearly as you can

When running bundle exec ruby ..., I would like to set RUBYOPT=-rlogger environment variable only for the launched subprocess, in order to workaround some issue with older gems that assume logger is loaded by default.

If I run RUBYOPT=-rlogger bundle exec ruby ... it also export the variable for bundler, which can easily cause an expected crash for bundle exec command (explained down below).

The workaround is to wrap the child command with a shell setting the environment variable, like bundle exec sh -c 'RUBYOPT=-rlogger exec ruby ...'. However, this method is ugly and does not work cross-platform, because we cannot expect sh to be installed on Windows.


I would like to have support for this new syntax similar to how posix shell works:

bundle exec RUBYOPT=-rlogger ruby ...

It should export environment variables only for the child process but not bundle command itself.

Alternatively, a --env flag for bundle exec would also be good, if the shell like syntax is considered to be convoluted.

Did you try upgrading rubygems & bundler?

Yes.

Post steps to reproduce the problem
# Gemfile
source 'https://rubygems.org'
gem 'logger'
gem 'rake'
Which command did you run?
RUBYOPT=-rlogger bundle exec rake

This crashes with the following error:

bundler: failed to load command: rake (/Users/edward/Developer/github/ruby-test/vendor/bundle/ruby/3.4.0/bin/rake)
/opt/homebrew/Cellar/ruby/3.4.4/lib/ruby/3.4.0/bundler/runtime.rb:317:in 'Bundler::Runtime#check_for_activated_spec!': You have already activated logger 1.6.4, but your Gemfile requires logger 1.7.0. Since logger is a default gem, you can either remove your dependency on it or try updating to a newer version of bundler that supports logger as a default gem. (Gem::LoadError)

To be clear, the goal is not to fix the crash mentioned above, as the crash is actually expected due to exactly what the error message was saying. The feature request is to have a way to easily set environment variable only for the child process that's launching but not on the bundle parent process. It would not only solve the specific crash mentioned above, but also provide a safer way to set environment variables in general that it won't pollute the environment of bundler itself.

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 at the bundle exec command entry point and reproduce the documented RUBYOPT=-rlogger bundle exec rake behavior with the provided Gemfile. Compare it with the requested bundle exec RUBYOPT=-rlogger ruby ... form and verify that the variable reaches only the child process while the existing command behavior remains unchanged.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.