# Cygwin + TempFile + Dir.chdir = failure

**URL:** <https://www.ruby-forum.com/t/cygwin-tempfile-dir-chdir-failure/89586>\
**Category:** Ruby\
**Created:** [March 21, 2007, 11:51am UTC](https://www.ruby-forum.com/t/cygwin-tempfile-dir-chdir-failure/89586 "2007-03-21T11:51:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![vulpes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@vulpes](https://www.ruby-forum.com/u/vulpes)\
**Post date:** [March 21, 2007, 11:51am UTC](https://www.ruby-forum.com/t/cygwin-tempfile-dir-chdir-failure/89586/1 "2007-03-21T11:51:11Z")

</div>

I’m running ruby 1.8.5-3 for Cygwin under Win XP Pro SP 2. Here’s some  
code:

require ‘tempfile’

Dir.chdir("./aDir") do  
Tempfile.open(“johndoe”, “.”) do |f|  
# Can be even empty  
end  
end

With this code, the temporary file in ./aDir will not be removed. Is  
this a bug, a Cygwin/Windows “feature”, or what? How to get around  
this thing?

---

<div class="post-metadata">

**Author:** ![vulpes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@vulpes](https://www.ruby-forum.com/u/vulpes)\
**Post date:** [March 21, 2007, 2:01pm UTC](https://www.ruby-forum.com/t/cygwin-tempfile-dir-chdir-failure/89586/2 "2007-03-21T14:01:20Z")

</div>

On 21.03.2007 11:48, vulpes wrote:

> With this code, the temporary file in ./aDir will not be removed. Is  
> this a bug, a Cygwin/Windows “feature”, or what? How to get around  
> this thing?

If you do this it will be removed (regardless whether you use chdir or  
not) - at least that’s the behavior I observe on my system:

Tempfile.open(“johndoe”, “/tmp”) do |f|  
# Can be even empty  
end

Basically the chdir isn’t needed anyway. It appears you hit a bug in  
the stdlib: it seems to try to delete “./” but fails  
because the file does not exist (remember that “.” points to a different  
directory then).

I just had a quick check of the Tempfile code and it appears that no  
attempts are made to get an absolute path. There is not a single  
occurrence of expand\_path in tempfile.rb.

The obvious workaround is to use an absolute path (see above).

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![vulpes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@vulpes](https://www.ruby-forum.com/u/vulpes)\
**Post date:** [March 21, 2007, 4:40pm UTC](https://www.ruby-forum.com/t/cygwin-tempfile-dir-chdir-failure/89586/3 "2007-03-21T16:40:47Z")

</div>

On Mar 21, 6:57 am, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > end  
> > # Can be even empty
> 
> The obvious workaround is to use an absolute path (see above).

Looks like a bug to me. Please file it on the RubyForge tracker (and  
the patch, too, if you’ve got one).

Thanks,

Dan

---

<div class="post-metadata">

**Author:** ![vulpes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/a3d4f5/32.png) [@vulpes](https://www.ruby-forum.com/u/vulpes)\
**Post date:** [March 21, 2007, 6:11pm UTC](https://www.ruby-forum.com/t/cygwin-tempfile-dir-chdir-failure/89586/4 "2007-03-21T18:11:00Z")

</div>

> Basically the chdir isn’t needed anyway. It appears you hit a bug in  
> the stdlib: it seems to try to delete “./” but fails  
> because the file does not exist (remember that “.” points to a different  
> directory then).

Point is that I encountered this while using RubyZip. If I’m trying to  
edit a zip file in another directory with RubyZip, it is unable to  
delete the temp file it creates. And there I have tried using absolute  
paths, it doesn’t work.
