Bug #21561
closedWrong encoding for File.dirname result on Windows
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) 9 days 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
Updated by byroot (Jean Boussier) 6 days ago
- Status changed from Open to Closed
Applied in changeset git|7a05dbc47831a655a1ef8a1635f88292acd325da.
File.dirname: return consistent encoding for "."
[Bug #21561]
It's preferable if the method is consistent in the encoding in
the returned string.
Updated by k0kubun (Takashi Kokubun) 5 days ago
- Backport changed from 3.2: WONTFIX, 3.3: REQUIRED, 3.4: REQUIRED to 3.2: WONTFIX, 3.3: REQUIRED, 3.4: DONE
ruby_3_4 21709a58682b9fef53e3e3dec97a0fe4498334e3 merged revision(s) 7a05dbc47831a655a1ef8a1635f88292acd325da.