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 1 commit
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
24 changes: 22 additions & 2 deletions lib/pry/commands/whereami.rb
Expand Up @@ -44,11 +44,31 @@ 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? && !File.absolute_path?(file)
Copy link
Member

Choose a reason for hiding this comment

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

As File.absolute_path? was added on Ruby 2.7. And we are still supporting older versions, is there any other way to make it compatible between versions?

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, that's disappointing. I guess the logic from absolute_path? could be back-ported inside of pry/whereami for earlier versions.

Copy link
Author

@onlynone onlynone Apr 24, 2024

Choose a reason for hiding this comment

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

I did my best to port the logic of File.absolute_path? to plain ruby inside of whereami. Can the tests for versions less than 2.7 be re-run with the latest code?

Also, since I copied 3 lines verbatim from a file from the ruby source repo to implement it, what's the best way to deal with the copyright issues? Ruby appears to be available under 2-clause BSD and pry looks to be under MIT, so I think they should be compatible, but I think we'd have to include the copyright info from ruby:

Copyright (C) 1993-2013 Yukihiro Matsumoto. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

somewhere.

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"
if !dir.nil? && File.absolute_path?(dir)
file = File.join(dir, file)
end
end

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

Expand Down
12 changes: 12 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,15 @@ 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__
fail "#{__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'

include Pry::Testable::Evalable

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

puts Cor.new.blimey!