swiftlang / swift-foundation

The Foundation project
Apache License 2.0
2.4k stars 160 forks source link

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

Closed omochi closed 2 weeks ago

omochi commented 1 month 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.

jrflat commented 2 weeks ago

Resolved by #989, thanks!