Skip to content

Commit b131d3c

Browse files
committed
[GR-17457] JT: Do not change directory for sh()
PullRequest: truffleruby/3416
2 parents 1bcd398 + 58fb7e5 commit b131d3c

File tree

2 files changed

+41
-38
lines changed

2 files changed

+41
-38
lines changed

src/main/c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ABI_VERSION := $(ROOT)/lib/cext/ABI_version.txt
2525
RBCONFIG := $(ROOT)/lib/truffle/rbconfig.rb
2626
MKMF := $(ROOT)/lib/mri/mkmf.rb
2727
LIBTRUFFLERUBY = cext/libtruffleruby.$(SOEXT)
28-
BASIC_EXTCONF_DEPS := $(SPAWN_HELPER) $(TRUFFLE_POSIX) $(RUBY_HEADERS) $(ABI_VERSION) $(RBCONFIG) $(MKMF)
28+
BASIC_EXTCONF_DEPS := Makefile $(SPAWN_HELPER) $(TRUFFLE_POSIX) $(RUBY_HEADERS) $(ABI_VERSION) $(RBCONFIG) $(MKMF)
2929
# C extensions link against libtruffleruby (and might do have_func() checks against it), so it needs to be there before.
3030
# However, if libtruffleruby is recompiled, there is no need to rebuild C extensions, so it's a order-only-prerequisite.
3131
EXTCONF_DEPS := $(BASIC_EXTCONF_DEPS) | $(LIBTRUFFLERUBY)

tool/jt.rb

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def get_truffle_version(from: :suite)
160160
raise unless /"name": "sulong",.+?"version": "(\h{40})"/m =~ suite
161161
$1
162162
when :repository
163-
raw_sh('git', 'rev-parse', 'HEAD', capture: :out, no_print_cmd: true, chdir: GRAAL_DIR).chomp
163+
sh('git', 'rev-parse', 'HEAD', capture: :out, no_print_cmd: true, chdir: GRAAL_DIR).chomp
164164
else
165165
raise ArgumentError, from: from
166166
end
@@ -332,9 +332,8 @@ def find_or_clone_repo(url, commit=nil)
332332
name = File.basename url, '.git'
333333
path = File.expand_path("../#{name}", TRUFFLERUBY_DIR)
334334
unless Dir.exist? path
335-
target = "../#{name}"
336-
sh 'git', 'clone', url, target
337-
sh 'git', 'checkout', commit, chdir: target if commit
335+
git_clone url, path
336+
sh 'git', 'checkout', commit, chdir: path if commit
338337
end
339338
path
340339
end
@@ -386,7 +385,7 @@ def diff(expected, actual)
386385
`diff -u #{expected} #{actual}`
387386
end
388387

389-
def raw_sh_failed_status
388+
def sh_failed_status
390389
`false`
391390
$?
392391
end
@@ -404,7 +403,7 @@ def terminate_process(pid, timeout = 10)
404403
STDERR.puts "Process #{pid} terminated"
405404
end
406405

407-
def raw_sh_with_timeout(timeout, pid)
406+
def sh_with_timeout(timeout, pid)
408407
if !timeout
409408
yield
410409
else
@@ -423,14 +422,14 @@ def raw_sh_with_timeout(timeout, pid)
423422
end
424423
end
425424

426-
def raw_sh_track_subprocess(pid)
425+
def sh_track_subprocess(pid)
427426
SUBPROCESSES << pid
428427
yield
429428
ensure
430429
SUBPROCESSES.delete(pid)
431430
end
432431

433-
def raw_sh(*args)
432+
def sh(*args)
434433
options = args.last.is_a?(Hash) ? args.last : {}
435434
continue_on_failure = options.delete :continue_on_failure
436435
use_exec = options.delete :use_exec
@@ -459,17 +458,17 @@ def raw_sh(*args)
459458
pid = Process.spawn(*args)
460459
rescue Errno::ENOENT => no_such_executable
461460
STDERR.puts bold no_such_executable
462-
status = raw_sh_failed_status
461+
status = sh_failed_status
463462
else
464-
raw_sh_track_subprocess(pid) do
463+
sh_track_subprocess(pid) do
465464
pipe_w.close if capture
466465

467-
result = raw_sh_with_timeout(timeout, pid) do
466+
result = sh_with_timeout(timeout, pid) do
468467
out = pipe_r.read if capture
469468
_, status = Process.waitpid2(pid)
470469
end
471470
if result == :timeout
472-
status = raw_sh_failed_status
471+
status = sh_failed_status
473472
end
474473
end
475474
end
@@ -546,15 +545,8 @@ def replace_env_vars(string, env = ENV)
546545
end
547546
end
548547

549-
def sh(*args)
550-
chdir(TRUFFLERUBY_DIR) do
551-
raw_sh(*args)
552-
end
553-
end
554-
555548
def app_open(file)
556549
cmd = darwin? ? 'open' : 'xdg-open'
557-
558550
sh cmd, file
559551
end
560552

@@ -570,6 +562,12 @@ def chdir(dir, &block)
570562
end
571563
end
572564

565+
def in_truffleruby_repo_root!
566+
unless File.realpath(Dir.pwd) == TRUFFLERUBY_DIR
567+
raise 'This command should be called at the root of the truffleruby repository'
568+
end
569+
end
570+
573571
def find_java_home
574572
@java_home ||= begin
575573
mx_env = File.expand_path('~/.mx/env')
@@ -617,7 +615,7 @@ def mx(*args, java_home: find_java_home, **options)
617615
mx_args.unshift '--java-home', java_home
618616
end
619617

620-
raw_sh(env, find_mx, *mx_args, **options)
618+
sh(env, find_mx, *mx_args, **options)
621619
end
622620

623621
def mx_os
@@ -643,7 +641,7 @@ def git_clone(url, path)
643641
# Unset $JAVA_HOME, mx sclone should not use it, and $JAVA_HOME might be set to the standalone home in graalvm tests
644642
mx('sclone', '--kind', 'git', url, path, java_home: :none)
645643
else
646-
raw_sh 'git', 'clone', url, path
644+
sh 'git', 'clone', url, path
647645
end
648646
end
649647

@@ -718,9 +716,9 @@ def github(dir = TRUFFLERUBY_DIR)
718716
def remote_urls(dir = TRUFFLERUBY_DIR)
719717
@remote_urls ||= Hash.new
720718
@remote_urls[dir] ||= begin
721-
out = raw_sh 'git', '-C', dir, 'remote', capture: :out, no_print_cmd: true
719+
out = sh 'git', '-C', dir, 'remote', capture: :out, no_print_cmd: true
722720
out.split.map do |remote|
723-
url = raw_sh 'git', '-C', dir, 'config', '--get', "remote.#{remote}.url", capture: :out, no_print_cmd: true
721+
url = sh 'git', '-C', dir, 'config', '--get', "remote.#{remote}.url", capture: :out, no_print_cmd: true
724722
[remote, url.chomp]
725723
end
726724
end
@@ -885,11 +883,12 @@ def launcher
885883
end
886884

887885
def build(*options)
886+
in_truffleruby_repo_root!
888887
project = options.shift
889888
case project
890889
when 'parser'
891890
jay = "#{TRUFFLERUBY_DIR}/tool/jay"
892-
raw_sh 'make', chdir: jay
891+
sh 'make', chdir: jay
893892
ENV['PATH'] = "#{jay}:#{ENV['PATH']}"
894893
sh 'bash', 'tool/generate_parser'
895894
yytables = 'src/main/java/org/truffleruby/parser/parser/YyTables.java'
@@ -904,6 +903,7 @@ def build(*options)
904903
end
905904

906905
def clean(*options)
906+
in_truffleruby_repo_root!
907907
project = options.shift
908908
case project
909909
when 'cexts'
@@ -929,7 +929,7 @@ def env
929929
env_vars.each do |e|
930930
puts format "%#{column_size}s: %s", e, ENV[e].inspect
931931
end
932-
shell = -> command { raw_sh(*command.split, continue_on_failure: true) }
932+
shell = -> command { sh(*command.split, continue_on_failure: true) }
933933
shell['ruby -v']
934934
shell['uname -a']
935935
shell['cc -v']
@@ -1053,7 +1053,7 @@ def rebuild(*options)
10531053

10541054
vm_args, ruby_args, options = ruby_options(options, args)
10551055

1056-
raw_sh env_vars, ruby_launcher, *(vm_args if truffleruby?), *ruby_args, options
1056+
sh env_vars, ruby_launcher, *(vm_args if truffleruby?), *ruby_args, options
10571057
end
10581058

10591059
def ruby(*args)
@@ -1098,7 +1098,7 @@ def cextc(cext_dir)
10981098
chdir(ext_dir) do
10991099
run_ruby(env, '-rmkmf', "#{ext_dir}/extconf.rb") # -rmkmf is required for C ext tests
11001100
if File.exist?('Makefile')
1101-
raw_sh('make')
1101+
sh('make')
11021102
FileUtils::Verbose.cp("#{name}.#{DLEXT}", target) if target
11031103
else
11041104
STDERR.puts "Makefile not found in #{ext_dir}, skipping make."
@@ -1287,6 +1287,7 @@ def test(*args)
12871287
end
12881288

12891289
def retag(*args)
1290+
in_truffleruby_repo_root!
12901291
require_ruby_launcher!
12911292
options, test_files = args.partition { |a| a.start_with?('-') }
12921293

@@ -1354,6 +1355,7 @@ def retag(*args)
13541355
end
13551356

13561357
private def run_single_cexts_test(test_name)
1358+
in_truffleruby_repo_root!
13571359
time_test("jt test cexts #{test_name}") do
13581360
case test_name
13591361
when 'tools'
@@ -1633,16 +1635,16 @@ def gem_test_pack
16331635
# Unset variable set by the pre-commit hook which confuses git
16341636
env = { 'GIT_DIR' => nil, 'GIT_INDEX_FILE' => nil }
16351637

1636-
current = raw_sh(env, 'git', '-C', gem_test_pack, 'rev-parse', 'HEAD', capture: :out, no_print_cmd: true).chomp
1638+
current = sh(env, 'git', '-C', gem_test_pack, 'rev-parse', 'HEAD', capture: :out, no_print_cmd: true).chomp
16371639
unless current == TRUFFLERUBY_GEM_TEST_PACK_VERSION
16381640
if ENV['GEM_TEST_PACK_WIP'] == 'true'
16391641
STDERR.puts 'WARNING: the gem test pack commit is different than TRUFFLERUBY_GEM_TEST_PACK_VERSION in jt.rb'
16401642
else
1641-
has_commit = raw_sh env, 'git', '-C', gem_test_pack, 'cat-file', '-e', TRUFFLERUBY_GEM_TEST_PACK_VERSION, continue_on_failure: true
1643+
has_commit = sh env, 'git', '-C', gem_test_pack, 'cat-file', '-e', TRUFFLERUBY_GEM_TEST_PACK_VERSION, continue_on_failure: true
16421644
unless has_commit
1643-
raw_sh env, 'git', '-C', gem_test_pack, 'fetch', Remotes.bitbucket(gem_test_pack), continue_on_failure: true
1645+
sh env, 'git', '-C', gem_test_pack, 'fetch', Remotes.bitbucket(gem_test_pack), continue_on_failure: true
16441646
end
1645-
raw_sh env, 'git', '-C', gem_test_pack, 'checkout', '-q', TRUFFLERUBY_GEM_TEST_PACK_VERSION
1647+
sh env, 'git', '-C', gem_test_pack, 'checkout', '-q', TRUFFLERUBY_GEM_TEST_PACK_VERSION
16461648
end
16471649
end
16481650

@@ -1827,11 +1829,11 @@ def metrics(command, *args)
18271829
log '.', "sampling\n"
18281830

18291831
max_rss_in_mb = if linux?
1830-
out = raw_sh('/usr/bin/time', '-v', '--', ruby_launcher, *args, capture: :both, no_print_cmd: true)
1832+
out = sh('/usr/bin/time', '-v', '--', ruby_launcher, *args, capture: :both, no_print_cmd: true)
18311833
out =~ /Maximum resident set size \(kbytes\): (?<max_rss_in_kb>\d+)/m
18321834
Integer($~[:max_rss_in_kb]) / 1024.0
18331835
elsif darwin?
1834-
out = raw_sh('/usr/bin/time', '-l', '--', ruby_launcher, *args, capture: :both, no_print_cmd: true)
1836+
out = sh('/usr/bin/time', '-l', '--', ruby_launcher, *args, capture: :both, no_print_cmd: true)
18351837
out =~ /(?<max_rss_in_bytes>\d+)\s+maximum resident set size/m
18361838
Integer($~[:max_rss_in_bytes]) / 1024.0 / 1024.0
18371839
else
@@ -1869,7 +1871,7 @@ def metrics(command, *args)
18691871

18701872
use_json = args.delete '--json'
18711873

1872-
out = raw_sh('perf', 'stat', '-e', 'instructions', '--', ruby_launcher, *args, capture: :both, no_print_cmd: true)
1874+
out = sh('perf', 'stat', '-e', 'instructions', '--', ruby_launcher, *args, capture: :both, no_print_cmd: true)
18731875

18741876
out =~ /(?<instruction_count>[\d,]+)\s+instructions/m
18751877
instruction_count = $~[:instruction_count].gsub(',', '')
@@ -2270,13 +2272,13 @@ def install(name, *options)
22702272

22712273
chdir(CACHE_EXTRA_DIR) do
22722274
unless File.exist?(eclipse_tar)
2273-
raw_sh 'curl', '-L', eclipse_url, '-o', eclipse_tar
2275+
sh 'curl', '-L', eclipse_url, '-o', eclipse_tar
22742276
end
22752277
unless File.exist?(eclipse_name)
22762278
computed = Digest::SHA256.file(eclipse_tar).hexdigest
22772279
if computed == sha256
22782280
Dir.mkdir eclipse_name
2279-
raw_sh 'tar', 'xf', eclipse_tar, '-C', eclipse_name
2281+
sh 'tar', 'xf', eclipse_tar, '-C', eclipse_name
22802282
else
22812283
raise "Incorrect sha256 for #{eclipse_tar}: #{computed} instead of expected #{sha256}"
22822284
end
@@ -2905,6 +2907,7 @@ def format_specializations_check
29052907
end
29062908

29072909
def lint(*args)
2910+
in_truffleruby_repo_root!
29082911
fast = args.first == 'fast'
29092912
args.shift if fast
29102913

@@ -3016,7 +3019,7 @@ def docker(*args)
30163019
end
30173020

30183021
def visualvm
3019-
raw_sh "#{graalvm_home}/bin/jvisualvm"
3022+
sh "#{graalvm_home}/bin/jvisualvm"
30203023
end
30213024
end
30223025

0 commit comments

Comments
 (0)