swift-foundation icon indicating copy to clipboard operation
swift-foundation copied to clipboard

URL.path returns forward slashes instead of backslashes on Windows

Open ahoppen opened this issue 1 year ago • 1 comments

print(URL(fileURLWithPath: #"C:\Users\alex"#).path)
print(URL(fileURLWithPath: #"C:/Users/alex"#).path)

Both print statements above print C:/Users/alex. I think they should print C:\Users\alex.

Maybe worth noting: Using withUnsafeFileSystemRepresentation returns backslashes. Should that be exposed through URL.path?

URL(fileURLWithPath: #"C:/Users/alex"#).withUnsafeFileSystemRepresentation {
    print(String(cString: $0!))
}
// prints C:\Users\alex

ahoppen avatar Oct 10 '24 15:10 ahoppen

Based on my previous experience in this area, it's expected behavior.

If you want the file path associated with a file URL, you must use withUnsafeFileSystemRepresentation. path quite specifically refers to the path component of the URL per RFC8089, which is conceptually not the same thing.

In fact, I'd have expected your example to print /C:/Users/alex (with an extra leading slash), as that's that the RFC specifies. Not sure if the Foundation team changed that to remove the leading slash VERY recently... (EDIT: Yeah: #964)

jakepetroules avatar Dec 08 '24 06:12 jakepetroules