Skip to content

Commit 9ff45ca

Browse files
committed
Search the executable in the passed env PATH for subprocesses
* Fixes #2419 * Always have @add_to_env set and be a Hash for convenience.
1 parent 13d27ef commit 9ff45ca

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Compatibility:
2323
* Implement `rb_str_vcatf`.
2424
* Add support for tracing allocations from C functions (#2403, @chrisseaton).
2525
* Implement `rb_str_catf`.
26+
* Search the executable in the passed env `PATH` for subprocesses (#2419).
2627

2728
Performance:
2829

src/main/ruby/truffleruby/core/truffle/process_operations.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,10 @@ def parse(env_or_cmd, *args)
191191

192192
parse_options(options)
193193

194-
if env and !env.empty?
195-
array = (@options[:env] ||= [])
196-
194+
@add_to_env = {}
195+
if env
197196
env.each_pair do |key, value|
198-
array << [convert_env_key(key), convert_env_value(value)]
197+
@add_to_env[convert_env_key(key)] = convert_env_value(value)
199198
end
200199
end
201200
end
@@ -373,14 +372,12 @@ def convert_env_value(value)
373372

374373
def spawn_setup(alter_process)
375374
env = @options.delete(:unsetenv_others) ? {} : ENV.to_hash
376-
@add_to_env = add_to_env = @options.delete(:env)
377-
if add_to_env
378-
add_to_env.each do |key, value|
379-
if value
380-
env[key] = value
381-
else
382-
env.delete(key)
383-
end
375+
376+
@add_to_env.each_pair do |key, value|
377+
if value
378+
env[key] = value
379+
else
380+
env.delete(key)
384381
end
385382
end
386383

@@ -502,10 +499,12 @@ def posix_spawnp(command, args, env_array, options)
502499
end
503500
end
504501

505-
if chdir
502+
use_helper = chdir || @add_to_env['PATH']
503+
if use_helper
506504
# Go through spawn-helper to change the working dir and then execve()
507505
spawn_helper = "#{Truffle::Boot.ruby_home}/lib/truffle/spawn-helper"
508-
args = [spawn_helper, chdir, command, *args]
506+
cwd = chdir || Dir.pwd
507+
args = [spawn_helper, cwd, command, *args]
509508
command = spawn_helper
510509
end
511510

@@ -548,7 +547,7 @@ def exec
548547

549548
def log_command(type)
550549
if LOG_SUBPROCESS
551-
env = (@add_to_env || {}).map { |k,v| "#{k}=#{v}" }.join(' ')
550+
env = @add_to_env.map { |k,v| "#{k}=#{v}" }.join(' ')
552551
Truffle::Debug.log_info "#{type}: #{env}#{' ' unless env.empty?}#{@argv.join(' ')}"
553552
end
554553
end
@@ -570,7 +569,8 @@ def resolve_in_path(command)
570569
end
571570
end
572571

573-
ENV['PATH'].split(File::PATH_SEPARATOR).each do |dir|
572+
path = @add_to_env['PATH'] || ENV['PATH']
573+
path.split(File::PATH_SEPARATOR).each do |dir|
574574
file = File.join(dir, command)
575575
if File.file?(file) && File.executable?(file)
576576
return file

0 commit comments

Comments
 (0)