From 77e2f4902cc1177f1b091ae737cf31fc23ed5014 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sat, 20 Mar 2021 00:20:17 +0100 Subject: [PATCH 1/4] Fix Process::INITIAL_PWD for non-existent path --- spec/std/exception/call_stack_spec.cr | 16 ++++++++++++++++ src/exception/call_stack.cr | 2 +- src/process/executable_path.cr | 15 +++++++++++++-- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/spec/std/exception/call_stack_spec.cr b/spec/std/exception/call_stack_spec.cr index 1fe31b01ad41..29574bf741fc 100644 --- a/spec/std/exception/call_stack_spec.cr +++ b/spec/std/exception/call_stack_spec.cr @@ -57,4 +57,20 @@ describe "Backtrace" do output.to_s.empty?.should be_true error.to_s.should contain("Invalid memory access") end + + it "print exception with non-existing PWD" do + source_file = datapath("blank_test_file.txt") + compile_file(source_file) do |executable_file| + output, error = IO::Memory.new, IO::Memory.new + with_tempfile("non-existent") do |path| + Dir.mkdir path + Dir.cd(path) do + Dir.delete(path) + status = Process.run executable_file + + status.success?.should be_true + end + end + end + end end diff --git a/src/exception/call_stack.cr b/src/exception/call_stack.cr index 1a8d632d637b..a0c5f0dd7539 100644 --- a/src/exception/call_stack.cr +++ b/src/exception/call_stack.cr @@ -22,7 +22,7 @@ struct Exception::CallStack # Compute current directory at the beginning so filenames # are always shown relative to the *starting* working directory. CURRENT_DIR = begin - dir = Process::INITIAL_PWD + dir = Process::INITIAL_PWD || "" dir += File::SEPARATOR unless dir.ends_with?(File::SEPARATOR) dir end diff --git a/src/process/executable_path.cr b/src/process/executable_path.cr index 5485a921bb73..d2637c718089 100644 --- a/src/process/executable_path.cr +++ b/src/process/executable_path.cr @@ -9,7 +9,16 @@ class Process INITIAL_PATH = ENV["PATH"]? # :nodoc: - INITIAL_PWD = Dir.current + # + # Working directory at program start. Nil if working directory does not exist. + # + # Used for `Exception::CallStack::CURRENT_DIR` + # and `Process.executable_path_impl` on openbsd. + INITIAL_PWD = begin + Dir.current + rescue File::NotFoundError + nil + end # Returns an absolute path to the executable file of the currently running # program. This is in opposition to `PROGRAM_NAME` which may be a relative or @@ -166,7 +175,9 @@ end # openbsd, ... class Process private def self.executable_path_impl - find_executable(PROGRAM_NAME, INITIAL_PATH, INITIAL_PWD) + if pwd = INITIAL_PWD + find_executable(PROGRAM_NAME, INITIAL_PATH, pwd) + end end end {% end %} From 7c52f0d72d5ad709558c32d933affe7126fb4aaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sat, 20 Mar 2021 00:37:33 +0100 Subject: [PATCH 2/4] Disable spec on win32 --- spec/std/exception/call_stack_spec.cr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/std/exception/call_stack_spec.cr b/spec/std/exception/call_stack_spec.cr index 29574bf741fc..337a678430c2 100644 --- a/spec/std/exception/call_stack_spec.cr +++ b/spec/std/exception/call_stack_spec.cr @@ -58,13 +58,14 @@ describe "Backtrace" do error.to_s.should contain("Invalid memory access") end - it "print exception with non-existing PWD" do + pending_win32 "print exception with non-existing PWD" do source_file = datapath("blank_test_file.txt") compile_file(source_file) do |executable_file| output, error = IO::Memory.new, IO::Memory.new with_tempfile("non-existent") do |path| Dir.mkdir path Dir.cd(path) do + # on win32 it seems not possible to remove the directory while we're cd'ed into it Dir.delete(path) status = Process.run executable_file From dceb7715a1dd85e69ca6aca5e2a2bbfc8e3c63f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Sat, 20 Mar 2021 13:28:52 +0100 Subject: [PATCH 3/4] Avoid `/` as CURRENT_DIR when INITIAL_PWD does not exist --- src/exception/call_stack.cr | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/exception/call_stack.cr b/src/exception/call_stack.cr index a0c5f0dd7539..e2b32c2832b5 100644 --- a/src/exception/call_stack.cr +++ b/src/exception/call_stack.cr @@ -22,9 +22,12 @@ struct Exception::CallStack # Compute current directory at the beginning so filenames # are always shown relative to the *starting* working directory. CURRENT_DIR = begin - dir = Process::INITIAL_PWD || "" - dir += File::SEPARATOR unless dir.ends_with?(File::SEPARATOR) - dir + if dir = Process::INITIAL_PWD + dir += File::SEPARATOR unless dir.ends_with?(File::SEPARATOR) + dir + else + "" + end end @@skip = [] of String From eb7d7e2381d6aa692293d22b152c590efd102e41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20M=C3=BCller?= Date: Mon, 22 Mar 2021 13:30:49 +0100 Subject: [PATCH 4/4] Let CallStack::CURRENT_DIR be nilable --- src/exception/call_stack.cr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/exception/call_stack.cr b/src/exception/call_stack.cr index e2b32c2832b5..7b5cdf7f9638 100644 --- a/src/exception/call_stack.cr +++ b/src/exception/call_stack.cr @@ -25,8 +25,6 @@ struct Exception::CallStack if dir = Process::INITIAL_PWD dir += File::SEPARATOR unless dir.ends_with?(File::SEPARATOR) dir - else - "" end end @@ -178,7 +176,9 @@ struct Exception::CallStack next if @@skip.includes?(file) # Turn to relative to the current dir, if possible - file = file.lchop(CURRENT_DIR) + if current_dir = CURRENT_DIR + file = file.lchop(current_dir) + end file_line_column = "#{file}:#{line}:#{column}" end