# Possible bug in FileUtils::fu\_mkdir (Errno::EEXIST)

**URL:** <https://rubytalk.org/t/possible-bug-in-fileutils-fu-mkdir-errno-eexist/59140>\
**Category:** ruby-talk\
**Created:** [10 July 2010 15:34 UTC](https://rubytalk.org/t/possible-bug-in-fileutils-fu-mkdir-errno-eexist/59140 "2010-07-10T15:34:27Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luis\_Lavena1](https://avatars.discourse-cdn.com/v4/letter/l/f08c70/32.png) [@Luis\_Lavena1](https://rubytalk.org/u/Luis_Lavena1)\
**Post date:** [10 July 2010 15:34 UTC](https://rubytalk.org/t/possible-bug-in-fileutils-fu-mkdir-errno-eexist/59140/1 "2010-07-10T15:34:27Z")

</div>

Hello,

We are getting some reports in RubyInstaller project of weird gem  
installation issues.

[http://groups.google.com/group/rubyinstaller/browse\_thread/thread/df7b7c217ad7d882](http://groups.google.com/group/rubyinstaller/browse_thread/thread/df7b7c217ad7d882)

The bug output is the following:

ERROR: While executing gem ... (Errno::EEXIST)  
&nbsp;&nbsp;&nbsp;&nbsp;File exists - C:

I've created a simplified test case:

require 'fileutils'  
system("rd C:\\Foo /S/Q")  
FileUtils.mkdir\_p 'C:/Foo/Bar/Baz', :verbose =\> true

And the following is the output:

C:\Users\Luis\>ruby -v t.rb  
ruby 1.8.7 (2010-06-23 patchlevel 299) [i386-mingw32]  
mkdir -p C:/Foo/Bar/Baz  
C:/Users/Luis/Tools/Ruby/ruby-1.8.7-p299-i386-mingw32/lib/ruby/1.8/fileutils.rb:244:in  
`mkdir': File exists - C: (Errno::EEXIST)  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Users/Luis/Tools/Ruby/ruby-1.8.7-p299-i386-mingw32/lib/ruby/1.8/fileutils.rb:244:in  
`fu\_mkdir'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Users/Luis/Tools/Ruby/ruby-1.8.7-p299-i386-mingw32/lib/ruby/1.8/fileutils.rb:217:in  
`mkdir\_p'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Users/Luis/Tools/Ruby/ruby-1.8.7-p299-i386-mingw32/lib/ruby/1.8/fileutils.rb:215:in  
`reverse\_each'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Users/Luis/Tools/Ruby/ruby-1.8.7-p299-i386-mingw32/lib/ruby/1.8/fileutils.rb:215:in  
`mkdir\_p'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Users/Luis/Tools/Ruby/ruby-1.8.7-p299-i386-mingw32/lib/ruby/1.8/fileutils.rb:201:in  
`each'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from C:/Users/Luis/Tools/Ruby/ruby-1.8.7-p299-i386-mingw32/lib/ruby/1.8/fileutils.rb:201:in  
`mkdir\_p'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from t.rb:3

This happens with 1.8.6, 1.8.7 and 1.9.1 prior patchlevel 429. This do  
not show up with 1.9.1, nor with JRuby or IronRuby:

> <https://gist.github.com/luislavena/047915f46a88eb11dfcb>

Checking the code of FileUtils, specially fu\_mkdir:

&nbsp;&nbsp;def fu\_mkdir(path, mode) #:nodoc:  
&nbsp;&nbsp;&nbsp;&nbsp;path = path.sub(%r\</\z\>, '')  
&nbsp;&nbsp;&nbsp;&nbsp;if mode  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir path, mode  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;File.chmod mode, path  
&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Dir.mkdir path  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end

Seems that no matter if path exists and is a directory, it tries to create it.

It fails on Windows when tries to create "C:", since it never tries to  
test for File.directory?("C:")

Adding the following line before mode solves the problem:

&nbsp;&nbsp;&nbsp;&nbsp;return if File.directory?(path)

But the weird part of this is that the problem didn't existed before,  
just after E-Text Editor installation (which install cygwin)

I've removed all traces of both programs, registry and environment  
variables but the issue I've presented above is not going away.

The question is more related to the rescue SystemCallError and the  
"next unless File.directory?(path)"

fu\_mkdir path call modifies path removing trailing slashes, so:

File.directory?('C:/') # =\> false

becomes:

File.directory?('C:') # =\> true

The only problem is that for some reason, path contains the previous  
value, not the altered and correct one.

This is a bug? I doubt since it only appeared after the issue  
mentioned above, but still wanted to check prior reporting to  
Ruby-Core.

Thank you for your attention.

> **···**
>
> --  
> Luis Lavena  
> AREA 17  
> -  
> Perfection in design is achieved not when there is nothing more to add,  
> but rather when there is nothing more to take away.  
> Antoine de Saint-Exupéry
