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

Make the backquote operator always return a string #31253

Merged
merged 1 commit into from
Jan 31, 2019

Conversation

knu
Copy link
Contributor

@knu knu commented Nov 28, 2017

This fixes this behavior:

# Vanilla Ruby (on unix)
`nonexistentcommand` #=> Errno::ENOENT
`nonexistentcommand >/dev/null` == ""

# With ActiveSupport loaded
`nonexistentcommand` == nil # I believe this should be fixed
`nonexistentcommand >/dev/null` == "" # With an error message on stderr

With this PR merged:

# With ActiveSupport loaded
`nonexistentcommand` == "" # With an error message on stderr
`nonexistentcommand >/dev/null` == "" # With an error message from the shell on stderr

Summary

ActiveSupport overrides Kernel#` so that it would not raise
Errno::ENOENT but return nil instead if a given command were not
found. In vanilla Ruby you can safely say `command`.strip or
`command`.empty? but with ActiveSupport loaded that's not the
case anymore.

A good function wrapper does not change the return value type, so the
core extension of Kernel#` should return an empty (mutable)
string when it decides to suppress an exception.

@rails-bot
Copy link

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @schneems (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

@matthewd
Copy link
Member

Are we unifying a behaviour that's since been unified upstream in the other direction?

# Vanilla Ruby
`nonexistentcommand` == ""

Everywhere I tried this just now I got an ENOENT, not a string. 😕

@knu
Copy link
Contributor Author

knu commented Nov 28, 2017

Oops, that description I added the last minute was wrong, I’ll fix it shortly.

@matthewd
Copy link
Member

It sounds like maybe we should just remove the method. I don't think it was a strong opinion that it should fail quietly -- we just wanted it to do something consistently. If upstream is now meeting that condition, we don't need to do anything.

@knu
Copy link
Contributor Author

knu commented Nov 28, 2017

@matthewd Sorry, I've fixed the description.

It sounds like maybe we should just remove the method. I don't think it was a strong opinion that it should fail quietly -- we just wanted it to do something consistently. If upstream is now meeting that condition, we don't need to do anything.

That'd be an idea, but at least the upstream (Ruby) did not change anything with the said behavior. `nonexistentcommand` will raise Errno::ENOENT at least on unix.

@matthewd
Copy link
Member

the upstream (Ruby) did not change anything with the said behavior

It clearly did, because the comment on this method says that [when it was written] on Unix, `badcmd` warned and returned nothing, and that's what it's trying to emulate. A quick experiment confirms:

irb(main):001:0> `nonexistentcommand`
(irb):1: command not found: nonexistentcommand
=> ""
irb(main):002:0> RUBY_VERSION
=> "1.8.7"

(Interestingly, an empty string, not a nil. I don't have 1.8.6 nearby to compare; maybe that was always inaccurate.)

I think this reinforces the idea that we should be removing it: we standardized on the 1.8.7 Unix behaviour, and now we're just (very) out of date.

@knu
Copy link
Contributor Author

knu commented Nov 28, 2017

Hmm. Is it really safe to break the assumption among Rails users that a backquote operator wouldn’t raise ENOENT? I have no particular opinion and just think it is unexpectable and inappropriate for it to return nil.

@dillonwelch
Copy link
Contributor

Anything stopping this from being merged?

@rafaelfranca
Copy link
Member

I agree with Matthew that we should remove this method. @knu can you remove the method and add a CHANGELOG entry?

@knu
Copy link
Contributor Author

knu commented Oct 1, 2018

@rafaelfranca Done. Should I squash the commits?

require "active_support/core_ext/kernel"

class KernelTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Stream
Copy link
Member

Choose a reason for hiding this comment

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

This has no longer used.

@knu
Copy link
Contributor Author

knu commented Dec 5, 2018

Friendly ping.

@matthewd
Copy link
Member

matthewd commented Dec 5, 2018

@knu sorry for the delay -- yes, please go ahead and squash. 👍

@rafaelfranca do you have any thoughts on whether we should deprecate this first? I'm leaning towards treating it as a bugfix (though one we wouldn't backport) and merging this as is, but we could put a deprecation inside the rescue first. Personally, I think the existing STDERR.puts should've been ugly enough to act as a deprecation -- that plus the fact we're aligning with standard ruby behaviour seems sufficient to me.

@knu
Copy link
Contributor Author

knu commented Dec 5, 2018

@matthewd I've squashed all commits into one and updated the commit message.

I agree you can treat this as a mere bug fix!

@rafaelfranca
Copy link
Member

I's also treat this as a bug fix. No need for deprecation.

@knu
Copy link
Contributor Author

knu commented Jan 9, 2019

Someone?

@y-yagi
Copy link
Member

y-yagi commented Jan 31, 2019

I want to this include in Rails 6.0.
@knu Sorry for many times, please rebase and squash?

ActiveSupport overrides `` Kernel#` `` so that it would not raise
`Errno::ENOENT` but return `nil` instead (due to the last statement
`STDERR.puts` returning nil) if a given command were not found.
Because of this, you cannot safely say somthing like
`` `command`.chomp `` when ActiveSupport is loaded.

It turns out that this is an outdated monkey patch for Windows
platforms to emulate Unix behavior on an ancient version of Ruby, and
it should be removed by now.
@knu
Copy link
Contributor Author

knu commented Jan 31, 2019

@y-yagi Done, and the CI build passed.

@y-yagi y-yagi merged commit d929b95 into rails:master Jan 31, 2019
@y-yagi
Copy link
Member

y-yagi commented Jan 31, 2019

@knu Thanks!

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

Successfully merging this pull request may close these issues.

None yet

9 participants