我刚用rails new
创建了一个全新的Rails项目。在irb
的目录中,我尝试为不存在的可执行文件执行shell命令,并抛出异常:
irb(main):001:0> `foobar`
Errno::ENOENT: No such file or directory - foobar
from (irb):1:in ``'
from (irb):1
from /home/jackson/.rbenv/versions/2.4.1/bin/irb:11:in `<top (required)>'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in `load'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:74:in `kernel_load'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/cli/exec.rb:27:in `run'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/cli.rb:335:in `exec'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/command.rb:27:in `run'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/invocation.rb:126:in `invoke_command'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor.rb:359:in `dispatch'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/cli.rb:20:in `dispatch'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/vendor/thor/lib/thor/base.rb:440:in `start'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/cli.rb:11:in `start'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/exe/bundle:32:in `block in <top (required)>'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/lib/bundler/friendly_errors.rb:121:in `with_friendly_errors'
from /home/jackson/.rbenv/versions/2.4.1/lib/ruby/gems/2.4.0/gems/bundler-1.14.6/exe/bundle:24:in `<top (required)>'
from /home/jackson/.rbenv/versions/2.4.1/bin/bundle:22:in `load'
from /home/jackson/.rbenv/versions/2.4.1/bin/bundle:22:in `<main>'
然而,当我尝试在rails c
的Rails环境中执行相同的操作时,不会抛出异常。反引号改为nil
:
irb(main):001:0> `foobar`
rails_console: No such file or directory - foobar
=> nil
这种不一致似乎反映在Rails控制台之外。反引号也会在我的Rails应用程序中返回nil
。这是一个问题,因为我们的某个宝石中缺少命令的错误处理被破坏了,因为它没有抛出异常:https://github.com/dstil/localtunnel/blob/87b1e4b98f600c2a767654caf0a2d94fef5be0e5/lib/localtunnel/client.rb#L37-L41
这种行为的意图是否有意?如果没有,那有什么不对,我该如何解决这个问题?
答案 0 :(得分:5)
你的问题似乎是Rails overrides backticks试图标准化行为:
class Object
# Makes backticks behave (somewhat more) similarly on all platforms.
# On win32 `nonexistent_command` raises Errno::ENOENT; on Unix, the
# spawned shell prints a message to stderr and sets $?. We emulate
# Unix on the former but not the latter.
def `(command) #:nodoc:
super
rescue Errno::ENOENT => e
STDERR.puts "#$0: #{e}"
end
end
有责任到处走走:
Open3
。Kernel#`
应该如何表现(如果你的意见足够,甚至可能有反引号)。有很多方法可以解决这个问题。您可以尝试在初始化程序中删除ActiveSupport覆盖:
Object.send(:remove_method, :'`')
以便使用来自Kernel
的标准反引号。或者,如果您从未期望拥有lt
命令,则可以替换gem的方法:
module LocalTunnel
class Client
def self.package_installed?
false
end
end
end
或者用你自己的实现替换LocalTunnel::Client.package_installed?
,通过检查反引号可能失败的各种方法或使用Open3
中的方法而不是反引号来解决Rails覆盖问题。您也可以通过分解lv
环境变量(使用PATH
)和File::PATH_SEPARATOR
手动查找File.executable?
。
您使用哪种kludge取决于您拥有的可移植性要求以及适用于您的环境的内容。