Skip to content

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

Closed
@omochi

Description

@omochi

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions