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

Projects
None yet
9 participants
@knu
Copy link
Contributor

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

This comment has been minimized.

Copy link

rails-bot commented Nov 28, 2017

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

This comment has been minimized.

Copy link
Member

matthewd commented Nov 28, 2017

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

matthewd commented Nov 28, 2017

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

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

matthewd commented Nov 28, 2017

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

This comment has been minimized.

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.

@oniofchaos

This comment has been minimized.

Copy link
Contributor

oniofchaos commented Sep 26, 2018

Anything stopping this from being merged?

@rafaelfranca

This comment has been minimized.

Copy link
Member

rafaelfranca commented Sep 27, 2018

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

@knu knu force-pushed the knu:backquote_returns_string branch from 1459340 to 37725fc Oct 1, 2018

@knu

This comment has been minimized.

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

This comment has been minimized.

@kamipo

kamipo Oct 1, 2018

Member

This has no longer used.

@knu

This comment has been minimized.

Copy link
Contributor Author

knu commented Dec 5, 2018

Friendly ping.

@matthewd

This comment has been minimized.

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 knu force-pushed the knu:backquote_returns_string branch from bc5807e to 4267c64 Dec 5, 2018

@knu

This comment has been minimized.

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

This comment has been minimized.

Copy link
Member

rafaelfranca commented Dec 5, 2018

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

@knu knu force-pushed the knu:backquote_returns_string branch from 4267c64 to 30c00d3 Dec 11, 2018

@knu

This comment has been minimized.

Copy link
Contributor Author

knu commented Jan 9, 2019

Someone?

@y-yagi

This comment has been minimized.

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?

Remove the Kernel#` override that turns ENOENT into nil
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 knu force-pushed the knu:backquote_returns_string branch from 9f1295c to 545b6e5 Jan 31, 2019

@knu

This comment has been minimized.

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

2 checks passed

codeclimate All good!
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@y-yagi

This comment has been minimized.

Copy link
Member

y-yagi commented Jan 31, 2019

@knu Thanks!

@knu knu deleted the knu:backquote_returns_string branch Feb 1, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.