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

Conversation

onlynone
Copy link

fixes #1220

# 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.

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

whereami gets confused in a Dir.chdir block
2 participants