Skip to content

Basic: make dirname work better on Windows #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Sources/TSCBasic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ if(NOT CMAKE_SYSTEM_NAME STREQUAL Darwin)
target_link_libraries(TSCBasic PUBLIC
Foundation)
endif()
if(CMAKE_SYSTEM_NAME STREQUAL Windows)
target_link_libraries(TSCBasic PRIVATE
Pathcch)
endif()
# NOTE(compnerd) workaround for CMake not setting up include flags yet
set_target_properties(TSCBasic PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
Expand Down
11 changes: 9 additions & 2 deletions Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,15 @@ private struct PathImpl: Hashable {
/// the case if and only if there is no path separator).
fileprivate var dirname: String {
#if os(Windows)
let dir = string.deletingLastPathComponent
return dir == "" ? "." : dir
let fsr: UnsafePointer<Int8> = string.fileSystemRepresentation
defer { fsr.deallocate() }

let path: String = String(cString: fsr)
return path.withCString(encodedAs: UTF16.self) {
let data = UnsafeMutablePointer(mutating: $0)
PathCchRemoveFileSpec(data, path.count)
return String(decodingCString: data, as: UTF16.self)
}
#else
// FIXME: This method seems too complicated; it should be simplified,
// if possible, and certainly optimized (using UTF8View).
Expand Down