Skip to content

Commit b8c5c25

Browse files
committed
[GR-18163] Search the executable in the passed env PATH for subprocesses
PullRequest: truffleruby/2853
2 parents 0c3745e + 9ff45ca commit b8c5c25

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-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

spec/ruby/core/process/spawn_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@
212212
end.should output_to_fd("nil\n")
213213
end
214214

215+
it "uses the passed env['PATH'] to search the executable" do
216+
dir = tmp("spawn_path_dir")
217+
mkdir_p dir
218+
begin
219+
exe = 'process-spawn-executable-in-path'
220+
path = "#{dir}/#{exe}"
221+
File.write(path, "#!/bin/sh\necho $1")
222+
File.chmod(0755, path)
223+
224+
env = { "PATH" => "#{dir}#{File::PATH_SEPARATOR}#{ENV['PATH']}" }
225+
Process.wait Process.spawn(env, exe, 'OK', out: @name)
226+
$?.should.success?
227+
File.read(@name).should == "OK\n"
228+
ensure
229+
rm_r dir
230+
end
231+
end
232+
215233
it "calls #to_hash to convert the environment" do
216234
o = mock("to_hash")
217235
o.should_receive(:to_hash).and_return({"FOO" => "BAR"})

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)