swiftlang / swift-foundation

The Foundation project
Apache License 2.0
2.36k stars 150 forks source link

Removing the last path component of a standalone file path results in an empty path #922

Open omochi opened 2 days ago

omochi commented 2 days ago

As described in the title, the following occurs:

import Foundation

let file = URL(fileURLWithPath: "main.swift")
let dir = file.deletingLastPathComponent()
print(dir.relativePath) // => 
print(dir.appendingPathComponent("main.swift").path) // => /main.swift

Since main.swift represents a relative path from the current directory, removing the last component should result in . to indicate the current directory. However, it incorrectly results in an empty path ``.

When combined with the bug #921, this behavior causes the path to change unexpectedly when re-adding the removed component, resulting in a different path from the original file.

On macOS, the result is . as expected, so re-adding the removed component restores the path to the original file.