0
require 'FileUtils'
path = '../tmp/brpm_storage/FGRKKSSUI/lentkriskeditor/1.2.5/20170705121128/lentkriskeditor-1.2.5-dist/lentkriskeditor-1.2.5/jre/jre1.8.0_131/lib/desktop/icons/HighContrast/48x48/mimetypes/gnome-mime-application-x-java-jnlp-file.png'

FileUtils.mkdir_p(File.dirname(path))

File.open(path, 'wb') {|file|
# File.open(File.expand_path(path), 'wb') {|file|  # Full path always works
    file.write('Hello') 
}

fails

tmp.rb:6:in `initialize': No such file or directory - ../tmp/brpm_storage/FGRKKSSUI/lentkriskeditor/1.2.5/20170705121128/lentkriskeditor-1.2.5-dist/lentkriskeditor-1.2.5/jre/jre1.8.0_131/lib/desktop/icons/HighContrast/48x48/mimetypes/gnome-mime-application-x-java-jnlp-file.png (Errno::ENOENT)
"C:/src3/branches/PSENG-5315/BMC/utils/tmp/brpm_storage/FGRKKSSUI/lentkriskeditor/1.2.5/20170705121128/lentkriskeditor-1.2.5-dist/lentkriskeditor-1.2.5/jre/jre1.8.0_131/lib/desktop/icons/HighContrast/48x48/mimetypes/gnome-mime-application-x-java-jnlp-file.png"
    from C:/src3/branches/PSENG-5315/BMC/utils/yatra_logic/tmp.rb:6:in `open'
    from C:/src3/branches/PSENG-5315/BMC/utils/yatra_logic/tmp.rb:6:in `<top (required)>'
    from -e:1:in `load'
    from -e:1:in `<main>'

I noticed this phenomena in ruby 1.9.3 v551 and 2.0.0. With JRuby everything works as expected. I wonder what is the culprit?

c:\Ruby200\bin\ruby.exe -v
ruby 2.0.0p0 (2013-02-24) [i386-mingw32]
bioffe
  • 6,283
  • 3
  • 50
  • 65
  • 1
    Do you see this issue in a later version of ruby (e.g 2.4.1)? Also what is the behavior you see? Is it an exception or the code path just doesn't get executed? – Leo Correa Jul 05 '17 at 18:50
  • I added the stacktrace. If one would short the param in half, it works fine. Can't install newer ruby at work. – bioffe Jul 05 '17 at 18:54

1 Answers1

1

See Why does the 260 character path length limit exist in Windows? and https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

Certain Windows APIs have a file path string-length limit of 260 'characters.' (I assume that refers to a Windows CP-1252 character.)

I suspect that JRuby navigates file paths through the JVM, which works around this limitation. See How does Java circumvent the windows MAX_PATH WinAPI limitation.

Jon Wolski
  • 2,293
  • 2
  • 19
  • 21
  • But how do you explain that full path always works regardless of the size? – bioffe Jul 05 '17 at 19:01
  • @bioffe Perhaps the path string is computed by Ruby before calling any WinAPI. The path without the filename is only 215 'characters,' while the full file path itself is 258 'characters'. (Technically, it is not just a 260-character limit. I think the path after the drive specification [`C:`] has to be 255 or fewer characters) – Jon Wolski Jul 06 '17 at 16:12