Bug #10362
closedspawn doesn't raise exception on redirection error
Description
irb(main):002:0*
irb(main):003:0*
irb(main):004:0* system 'cmd', '/c', 'echo', 'aaa'
aaa
=> true
irb(main):005:0>
irb(main):006:0*
irb(main):007:0*
irb(main):008:0* system 'cmd', '/c', 'echo', 'aaa', :out => ['bad/file.txt', 'w']
=> nil
irb(main):009:0>
irb(main):010:0*
irb(main):011:0*
irb(main):012:0*
irb(main):013:0* system 'cmd', '/c', 'echo', 'aaa', :out => File.open('bad/file.txt', 'w')
Errno::ENOENT: No such file or directory - bad/file.txt
from (irb):13:in `initialize'
from (irb):13:in `open'
from (irb):13
from C:/Ruby200/bin/irb:12:in `<main>'
irb(main):014:0>
irb(main):015:0*
irb(main):016:0*
exception is much more descriptive than just nil
Updated by normalperson (Eric Wong) over 9 years ago
bdimych@narod.ru wrote:
irb(main):013:0* system 'cmd', '/c', 'echo', 'aaa', :out => File.open('bad/file.txt', 'w')
Errno::ENOENT: No such file or directory - bad/file.txt
from (irb):13:ininitialize' from (irb):13:in
open'
from (irb):13
from C:/Ruby200/bin/irb:12:in `'exception is much more descriptive than just nil
Right, but I think the system' method is intended to hide errors by default. $? must be checked when
system' returns nil:
if system('non-existent-command').nil?
$? => #<Process::Status: pid ... exit 127>
end
I think it is in the spec for `system' to behave like this.
Updated by nobu (Nobuyoshi Nakada) over 9 years ago
Yes, it's intentional spec, but to raise errors Process.spawn
and Process.wait
are needed.
It may be good to add an option for errors to system
.
Updated by normalperson (Eric Wong) over 9 years ago
On the other hand, maybe we should allow Ruby-level options to `system'
to raise, but continue hiding errors when running the command itself.
In other words:
# continue old 1.8 behavior if redirect is done via shell
# (n.b. this example is bad practice, but just an example)
system('true >bad/file.txt') -> nil
So I think the original example by Dmitry should raise:
system('true', out: %w(bad/file.txt w)) -> Errno...
Maybe this (potentially incompatible) change can be acceptable
for 2.2 (if not, then for Ruby 3.0)
Updated by jeremyevans0 (Jeremy Evans) almost 5 years ago
- Status changed from Open to Closed
system
was made more strict for redirection errors between Ruby 2.2 and 2.3:
speedstar$ ruby22 -e "p system('true', out: %w(bad/file.txt w))"
nil
speedstar$ ruby23 -e "p system('true', out: %w(bad/file.txt w))"
-e:1:in `system': No such file or directory - bad/file.txt (Errno::ENOENT)
from -e:1:in `<main>'