Actions
Bug #21561
openWrong encoding for File.dirname result on Windows
Status:
Open
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.5 (2025-07-16 revision 20cda200d3) +PRISM [x64-mingw-ucrt]
Description
I placed the file containing the code below in a directory named "à_signaler" to show the effect on non-ascii characters:
def dirname_encoding(file)
h = {
"file" => file,
"File.dirname(file)" => File.dirname(file),
"File.expand_path(file)" => File.expand_path(file),
"File.expand_path(File.dirname(file))" => File.expand_path(File.dirname(file)),
"File.dirname(File.expand_path(file))" => File.dirname(File.expand_path(file)),
}
puts
h.each do |name, value|
puts "#{name} = #{value.inspect} (#{value.encoding})"
end
end
dirname_encoding "foo.bar"
dirname_encoding "foo/bar"
The result:
file = "foo.bar" (UTF-8)
File.dirname(file) = "." (US-ASCII)
File.expand_path(file) = "D:/à_signaler/foo.bar" (UTF-8)
File.expand_path(File.dirname(file)) = "D:/\xE0_signaler" (US-ASCII)
File.dirname(File.expand_path(file)) = "D:/à_signaler" (UTF-8)
file = "foo/bar" (UTF-8)
File.dirname(file) = "foo" (UTF-8)
File.expand_path(file) = "D:/à_signaler/foo/bar" (UTF-8)
File.expand_path(File.dirname(file)) = "D:/à_signaler/foo" (UTF-8)
File.dirname(File.expand_path(file)) = "D:/à_signaler/foo" (UTF-8)
The source problem is the encoding US-ASCII for "."
This may be linked to Bug #20699.
Updated by byroot (Jean Boussier) about 5 hours ago
- Backport changed from 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN to 3.2: WONTFIX, 3.3: REQUIRED, 3.4: REQUIRED
The source problem is the encoding US-ASCII for "."
Yes, but File.expand_path
behavior is also suspicious.
Either way, I think returning a different encoding in a special case isn't a good idea. So I'm in favor of always using the same logic for the encoding selection: https://github.com/ruby/ruby/pull/14472
Actions
Like0
Like0