Project

General

Profile

Actions

Bug #22194

open

File.realpath and File.realdirpath handle differently path encoding incompatible with a filesystem's one

Bug #22194: File.realpath and File.realdirpath handle differently path encoding incompatible with a filesystem's one
1

Added by andrykonchin (Andrew Konchin) 10 days ago. Updated 3 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:126077]

Description

Both realpath and realdirpath methods accept a path parameter and try to return result in the path's encoding.

realpath:

File.realpath(".rubocop.yml").encoding # => #<Encoding:UTF-8>
File.realpath(".rubocop.yml".encode("US-ASCII")).encoding # => #<Encoding:US-ASCII>

realdirpath:

File.realdirpath("src").encoding # => #<Encoding:UTF-8>
File.realdirpath("src".encode("US-ASCII")).encoding # => #<Encoding:US-ASCII>

But it's possible that path's encoding isn't compatible with the filesystem's one (that's is common to be UTF-8 on Linux and macOS), that's converting from the filesystem's encoding to path's encoding fails.

In this case realpath and realdirpath behaves differently - realpath just "forces" encoding to path's one (and as fallback to filesystem's and then to binary if a string becomes broken) and realdirpath just skips this encoding and returns a string as is in the filesystem's encoding.

realpath:

File.realpath(".".encode(Encoding::ISO_8859_1), "test_dir_🦊").encoding # => #<Encoding:ISO-8859-1>

realdirpath:

File.realdirpath(".".encode(Encoding::ISO_8859_1), "test_dir_🦊").encoding # => #<Encoding:UTF-8>

It makes sense for the methods to handle this edge case consistently.

Updated by andrykonchin (Andrew Konchin) 9 days ago Actions #1

  • Subject changed from File.realpath and File.realdirpath handle differently path encoding incompatible to a filesystem's one to File.realpath and File.realdirpath handle differently path encoding incompatible with a filesystem's one

Updated by andrykonchin (Andrew Konchin) 9 days ago Actions #2

  • Description updated (diff)

Updated by andrykonchin (Andrew Konchin) 9 days ago Actions #3

  • Description updated (diff)

Updated by sophiathedev (Nguyen Thang) 3 days ago Actions #4 [ruby-core:126125]

I've submitted a PR to fix this issue here:
https://github.com/ruby/ruby/pull/17981

Thank you @andrykonchin (Andrew Konchin) for the bug report and the helpful notes! 🐐

Any feedback on the PR is highly appreciated.

Actions

Also available in: PDF Atom