rubyscriptingcompiler-errorspackagingocra

Ocra throws errors when packing an script


SCENARIO

I've installed the Ruby 2.1.5 (x64) package then I installed the Ocra gem successfully:

gem install ocra
Fetching: ocra-1.3.3.gem (100%)
Successfully installed ocra-1.3.3
Parsing documentation for ocra-1.3.3
Installing ri documentation for ocra-1.3.3
Done installing documentation for ocra after 0 seconds
1 gem installed

PROBLEM

I'm trying to pack a test Script:

# -*- coding: UTF-8 -*-

# require ''
exit if Object.const_defined?(:Ocra)

print "Hello World!"
sleep 3

__END__

But when I try to use Ocra, it throws errors:

ocra "TestScript.rb"

=== Loading script to check dependencies
=== Detected gem ocra-1.3.3 (loaded, files)
===     6 files, 190931 bytes
=== Detected gem io-console-0.4.2 (loaded, files)
C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:86:in `open':
 No such file or directory @ dir_initialize - C:/Program Files/Ruby/lib/ruby/gem
s/2.1.0/gems/io-console-0.4.2 (Errno::ENOENT)
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
86:in `entries'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
86:in `entries'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
92:in `find_all_files'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
665:in `block (2 levels) in find_gem_files'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
658:in `each'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
658:in `block in find_gem_files'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
611:in `each'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
611:in `find_gem_files'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
728:in `build_exe'
        from C:/Program Files/Ruby/lib/ruby/gems/2.1.0/gems/ocra-1.3.3/bin/ocra:
1165:in `block in <top (required)>'

QUESTION

Why happens this? How to fix it?


Solution

  • tl;dr: I was able to successfully build an executable from your script using Ocra with Ruby version 2.0.0p481 and rubygems 2.0.14. If you can switch Ruby versions, you should be able to get the same results. (Here's a write-up I did on using multiple Ruby versions on Windows with the awesome uru.)

    Deeper Dive:

    This io-console issue has troubled many users of the Ocra gem, and there is currently an open issue for it on github:

    https://github.com/larsch/ocra/issues/77

    Ocra calls Gem::Specification#gem_dir on all the loaded gems in your script, which includes io-console. That operation returns a path that does not exist, hence the error.

    (Here's a link to the gem_dir method from rubygems too, for your reference.)

    You can try it out yourself:

    irb(main):001:0>require "io/console"
    => true
    irb(main):002:0>io_console_spec = Gem.loaded_specs["io-console"]
    => #<Gem::Specification:0xblahblah io-console-0.4.2>
    irb(main):003:0>proposed_path = io_console_spec.gem_dir
    => "$YOUR_RUBY_DIRECTORY/lib/ruby/gems/2.1.0/gems/io-console-0.4.2"
    irb(main):004:0>File.directory? proposed_path
    => false
    

    Ocra created an executable from this script successfully when using Ruby 2.0.0p481:

    ocra-success

    This leads me to believe that the result of Gem.loaded_specs has changed somewhere between rubygems 2.0.14 (which is the version running on my 2.0.0p481 build) and rubygems 2.2.2 (which is the version running on my 2.1.5 build). Getting to the bottom of why Gem::Specification#gem_dir for io-console is something I'll be working on more (I'm very new to the inner-workings of Ocra and Rubygems), but hopefully this was helpful.