swiftlang / swift-foundation

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

Appending to an empty file path results in an absolute path #921

Open omochi opened 2 days ago

omochi commented 2 days ago

Problematic Code:

import Foundation

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

As shown in the example, even though dir holds the path to the current directory, appending a file to it results in an absolute path starting with a slash. This behavior is unexpected because appending to a current directory URL should ideally maintain a relative path, not convert it to an absolute one.