Skip to content

May 27 / Rupak Ganguly

Error in Ruby relative_path_from call on Windows

I was recently working on some code for building an extension for Spree, when I ran into an issue while generating a extension controller. The error only happens on Windows.

The error in the console looks like this:

$ ruby script/generate extension_controller PromotionManager admin/promotions
c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails/gem_dependency.rb:119:Warning: Gem::Dependency#
version_requirements is deprecated and will be removed on or after August 2010. Use #requirement
Loading haml-edge gem.
c:/ruby/lib/ruby/1.8/pathname.rb:709:in `relative_path_from': different prefix: "c:/" and "C:/RoRPro
js/spreestore" (ArgumentError)
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails_generator/lookup.rb:110:in `use_co
mponent_sources!'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails_generator/lookup.rb:109:in `each'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails_generator/lookup.rb:109:in `use_co
mponent_sources!'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails_generator/lookup.rb:55:in `include
d'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails_generator.rb:38:in `include'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails_generator.rb:38:in `send'
from c:/ruby/lib/ruby/gems/1.8/gems/rails-2.3.5/lib/rails_generator.rb:38
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:1
56:in `require'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:5
21:in `new_constants_in'
from c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:1
56:in `require'
from script/generate:4

The error is specifically in this line:

c:/ruby/lib/ruby/1.8/pathname.rb:709:in `relative_path_from': different prefix: "c:/" and "C:/RoRPro
js/spreestore" (ArgumentError)

After a little digging, it was evident that the ‘relative_path_from’ method in ‘/ruby/lib/ruby/1.8/pathname.rb’ file had the problem. The problem was also described as a ticket at http://redmine.ruby-lang.org/issues/show/1366 and the excerpt of the issue is shown below:

On Windows, the case of the drive letter can be either upper case or lower case (eg, "C:" or "c:") on the same machine at the same time in different Command Prompt Windows (see below for details). Dir.pwd will return either lower-case or upper-case for the drive letter (“C:/” or “c:/”) depending on the Command Prompt it is run from. However, FILE always uses lower-case drive letter. This can cause an ArgumentError when comparing Dir.pwd and FILE using Pathname#relative_path_from. This happens with version 1.9.1p0 as well. Pathname#relative_path_from should deal with the case where the case of the argument is different.

Note, that I have both my ruby install folder and my project folder on c: drive. And, I am running Ruby 1.8.6  - ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]

Solution

I monkey-patched the following lines in pathname.rb file as shown below.

def relative_path_from(base_directory)
dest_directory = self.cleanpath.to_s.capitalize!
base_directory = base_directory.cleanpath.to_s.capitalize!

I added the capitalize! keyword at the end of the two lines so that both the paths are in uppercase, so that later in the code (line 708-710) shown below does not barf and give us the error we got shown in the first para. of the article.

if dest_prefix != base_prefix
raise ArgumentError, "different prefix: #{dest_prefix.inspect} and #{base_directory.inspect}"
end

The temporary solution works after the patch. Hope it helps.

2 Comments

leave a comment
  1. Hamid Raza / Jul 6 2010

    Yes it has resolved the problem but it now gave the following error, stuck with that please help me if you have any solution regarding that thanks and kind regards,

    D:\hamidzwork\mostaml>ruby script/generate extension mostaml
    C:/Ruby/lib/ruby/1.8/pathname.rb:265:in `basename’: can’t convert nil into Strin
    g (TypeError)
    from C:/Ruby/lib/ruby/1.8/pathname.rb:265:in `chop_basename’
    from C:/Ruby/lib/ruby/1.8/pathname.rb:698:in `relative_path_from’
    from C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails_generator/look
    up.rb:110:in `use_component_sources!’
    from C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails_generator/look
    up.rb:109:in `each’
    from C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails_generator/look
    up.rb:109:in `use_component_sources!’
    from C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails_generator/look
    up.rb:55:in `included’
    from C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails_generator.rb:4
    1:in `include’
    from C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails_generator.rb:4
    1:in `send’
    from C:/Ruby/lib/ruby/gems/1.8/gems/rails-2.3.8/lib/rails_generator.rb:4
    1
    from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `ge
    m_original_require’
    from C:/Ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `po
    lyglot_original_require’
    from C:/Ruby/lib/ruby/gems/1.8/gems/polyglot-0.3.1/lib/polyglot.rb:64:in
    `require’
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_suppo
    rt/dependencies.rb:156:in `require’
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_suppo
    rt/dependencies.rb:521:in `new_constants_in’
    from C:/Ruby/lib/ruby/gems/1.8/gems/activesupport-2.3.8/lib/active_suppo
    rt/dependencies.rb:156:in `require’

    Hamid Raza.

  2. Hamid Raza / Jul 6 2010

    okie find solution on

    http://redmine.ruby-lang.org/issues/show/1266

    :)

    thanks.
    Hamid.

Leave a Comment