Skip to content

Respect Kernel#require's duck-type #396

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

Closed
wants to merge 2 commits into from
Closed

Conversation

etiennebarrie
Copy link
Member

Kernel#require turns its argument into a path by calling to_path if it responds to it, and then into a String (the argument or the return value of to_path) by using implicit conversion (to_str).

We can see this in the spec: https://github.com/ruby/spec/blob/97364ff1f90e63ba0159216f9563318a0d97dceb/core/kernel/shared/require.rb#L51

  • it calls #to_str on non-String objects
  • it calls #to_path on non-String objects
  • it calls #to_str on non-String objects returned by #to_path

Or in MRI, in rb_get_path, called from require_internal:
https://github.com/ruby/ruby/blob/5e3a32021849718ae483eaaa9fbf155f91828039/file.c#L219-L221


I used a fork because I noticed none of the tests were loading the core ext (even the test below this one supposedly testing load, I'll follow-up on that) and wanted to avoid leaks for other tests.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
Kernel#require turns its argument into a path by calling `to_path` if it
responds to it, and then into a String (the argument or the return value
of `to_path`) by using implicit conversion (`to_str`).
Comment on lines 24 to 25
Process.wait(pid)
assert_predicate Process.last_status, :success?
Copy link
Contributor

Choose a reason for hiding this comment

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

Ruby 2.3 compat

Suggested change
Process.wait(pid)
assert_predicate Process.last_status, :success?
pid, status = Process.wait2(pid)
assert_predicate status, :success?

@casperisfine
Copy link
Contributor

Do we need the same for require_relative / load / autoload?

@etiennebarrie
Copy link
Member Author

Do we need the same for require_relative / load / autoload?

It seems like we do. I'll add this.

@casperisfine
Copy link
Contributor

Silly idea, rb_get_path is public API, and since we have a C ext already, we could just expose it on Bootsnap::Native and use it.

@casperisfine
Copy link
Contributor

here's how it could look like 17ea17b

@etiennebarrie
Copy link
Member Author

Neat. The tests were problematic with the usage of fork under Windows though, I have to get back to that.

@casperisfine
Copy link
Contributor

Closing in favor of #406

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.

None yet

2 participants