Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix whereami to work after a chdir in a main script #2312

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 49 additions & 2 deletions lib/pry/commands/whereami.rb
Expand Up @@ -44,11 +44,29 @@ class << self
def setup
if target.respond_to?(:source_location)
file, @line = target.source_location
@file = expand_path(file)
else
@file = expand_path(target.eval('__FILE__'))
file = target.eval('__FILE__')
@line = target.eval('__LINE__')
end

# If the current location is in a top-level script, then the filename
# returned by source_location or __FILE__ will be just the filename with
# no path: e.g. 'myscript.rb'. If the current working directory has been
# changed, then expand_path(file) will construct an incorrect path to
# the source. We can use __dir__ to fix this case.
if !file.nil? && !absolute_path?(file)
dir = target.eval('__dir__')

# We have to be careful not to join with dir if it's also a relative
# path. There are some cases where the file and dir paths are both
# relative to the same directory, like:
#
# __FILE__: "spec/fixtures/example.erb"
# __dir__: "spec/fixtures"
file = File.join(dir, file) if !dir.nil? && absolute_path?(dir)
end

@file = expand_path(file)
@method = Pry::Method.from_binding(target)
end

Expand Down Expand Up @@ -189,6 +207,35 @@ def expand_path(filename)
File.expand_path(filename)
end

# These are #defines in the ruby C code that control how absolute_path?
# works. They don't seem to be accessible at runtime but they can be
# figured out from runtime behavior. This is copied from
# test/pathname/test_pathname.rb in the ruby source code (which looks to
# be under the 2-clause BSD license, not sure the best way to comply with
# the licensing terms for just 3 lines of code).
DOSISH = !File::ALT_SEPARATOR.nil?
DOSISH_DRIVE_LETTER = File.dirname("A:") == "A:."
DOSISH_UNC = File.dirname("//") == "//"

def absolute_path?(path)
# File.absolute_path? was added in ruby-2.7.0
return File.absolute_path?(path) if File.respond_to?(:absolute_path?)

if DOSISH_DRIVE_LETTER
return true if path =~ %r{^[a-z]:[/\\]}i
end

if DOSISH_UNC
return true if path =~ %r{^[/\\]{2}}i
end

unless DOSISH
return true if path[0] == '/'
end

false
end

def window_size
if args.empty?
pry_instance.config.default_window_size
Expand Down
11 changes: 11 additions & 0 deletions spec/commands/whereami_spec.rb
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'method_source'
require 'open3'

describe "whereami" do
it 'should work with methods that have been undefined' do
Expand Down Expand Up @@ -277,4 +278,14 @@ def blimey!
it "should work inside an object" do
expect(pry_eval(Object.new, 'whereami')).to match(/Inside #<Object/)
end

it 'should work after chdir from main script' do
script_dir = File.expand_path(File.join(__dir__, '..', 'fixtures'))
script = 'whereami_main_chdir.rb'

output, status = Open3.capture2(RbConfig.ruby, script, chdir: script_dir)

expect(status.success?).to be true
expect(output.strip).to eq File.join(script_dir, script)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are specs failing for Ruby >3.0. link

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup. Looks like target.eval('__dir__') returns nil in versions greater than 2.7. Not sure how to get around that... is there something else that could get the actual pathname or dirname of a file after 3.0?

end
end
22 changes: 22 additions & 0 deletions spec/fixtures/whereami_main_chdir.rb
@@ -0,0 +1,22 @@
# frozen_string_literal: true

if $PROGRAM_NAME != __FILE__
raise "#{__FILE__} should only be executed as a top level program"
end

$LOAD_PATH.unshift File.expand_path(File.join(__dir__, '..', '..', 'lib'))

require 'pry'
require 'pry/testable'

class Cor
include Pry::Testable::Evalable

def blimey!
Dir.chdir '..' do
pry_eval(binding, 'whereami', '_file_')
end
end
end

puts Cor.new.blimey!