Skip to content

Commit 889a8db

Browse files
authored
Merge pull request #1596 from ahoppen/nullbyte-after-user
Remove null byte after home directory on Windows
2 parents 2a99abd + 52e95c4 commit 889a8db

File tree

7 files changed

+46
-9
lines changed

7 files changed

+46
-9
lines changed

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ let package = Package(
3131
"SKOptions",
3232
"SKSupport",
3333
"SourceKitLSP",
34+
"SwiftExtensions",
3435
"ToolchainRegistry",
3536
.product(name: "ArgumentParser", package: "swift-argument-parser"),
3637
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),

Sources/Diagnose/DiagnoseCommand.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ package struct DiagnoseCommand: AsyncParsableCommand {
254254
let destinationDir = bundlePath.appendingPathComponent("logs")
255255
try FileManager.default.createDirectory(at: destinationDir, withIntermediateDirectories: true)
256256

257-
let logFileDirectoryURL = URL(fileURLWithPath: ("~/.sourcekit-lsp/logs" as NSString).expandingTildeInPath)
257+
let logFileDirectoryURL = FileManager.default.sanitizedHomeDirectoryForCurrentUser
258+
.appendingPathComponent(".sourcekit-lsp")
259+
.appendingPathComponent("logs")
258260
let enumerator = FileManager.default.enumerator(at: logFileDirectoryURL, includingPropertiesForKeys: nil)
259261
while let fileUrl = enumerator?.nextObject() as? URL {
260262
guard fileUrl.lastPathComponent.hasPrefix("sourcekit-lsp") else {

Sources/SKSupport/FileSystem.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
import Foundation
14+
import SwiftExtensions
1415

1516
import struct TSCBasic.AbsolutePath
1617

17-
/// The home directory of the current user (same as returned by Foundation's `NSHomeDirectory` method).
18-
package var homeDirectoryForCurrentUser: AbsolutePath {
19-
try! AbsolutePath(validating: NSHomeDirectory())
20-
}
21-
2218
extension AbsolutePath {
2319

2420
/// Inititializes an absolute path from a string, expanding a leading `~` to `homeDirectoryForCurrentUser` first.
2521
package init(expandingTilde path: String) throws {
2622
if path.first == "~" {
27-
try self.init(homeDirectoryForCurrentUser, validating: String(path.dropFirst(2)))
23+
try self.init(
24+
AbsolutePath(validating: FileManager.default.sanitizedHomeDirectoryForCurrentUser.path),
25+
validating: String(path.dropFirst(2))
26+
)
2827
} else {
2928
try self.init(validating: path)
3029
}

Sources/SwiftExtensions/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ add_library(SwiftExtensions STATIC
55
AsyncUtils.swift
66
Collection+Only.swift
77
Collection+PartitionIntoBatches.swift
8+
FileManager+SanitizedHomeDirectoryOfCurrentUser.swift
89
NSLock+WithLock.swift
910
PipeAsStringHandler.swift
1011
ResultExtensions.swift
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import Foundation
14+
15+
extension FileManager {
16+
/// Same as `homeDirectoryForCurrentUser` but works around
17+
/// https://github.com/apple/swift-corelibs-foundation/issues/5041, which causes a null byte
18+
package var sanitizedHomeDirectoryForCurrentUser: URL {
19+
let homeDirectory = FileManager.default.homeDirectoryForCurrentUser
20+
#if os(Windows)
21+
if homeDirectory.lastPathComponent.hasSuffix("\0") {
22+
let newLastPathComponent = String(homeDirectory.lastPathComponent.dropLast())
23+
return homeDirectory.deletingLastPathComponent().appendingPathComponent(newLastPathComponent)
24+
}
25+
#endif
26+
return homeDirectory
27+
}
28+
}

Sources/sourcekit-lsp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ target_link_libraries(sourcekit-lsp PRIVATE
1010
SKOptions
1111
SKSupport
1212
SourceKitLSP
13+
SwiftExtensions
1314
ToolchainRegistry
1415
ArgumentParser
1516
TSCBasic)

Sources/sourcekit-lsp/SourceKitLSP.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import SKLogging
2121
import SKOptions
2222
import SKSupport
2323
import SourceKitLSP
24+
import SwiftExtensions
2425
import ToolchainRegistry
2526

2627
import struct TSCBasic.AbsolutePath
@@ -238,7 +239,9 @@ struct SourceKitLSP: AsyncParsableCommand {
238239
var options = SourceKitLSPOptions.merging(
239240
base: commandLineOptions(),
240241
override: SourceKitLSPOptions(
241-
path: URL(fileURLWithPath: ("~/.sourcekit-lsp/config.json" as NSString).expandingTildeInPath)
242+
path: FileManager.default.sanitizedHomeDirectoryForCurrentUser
243+
.appendingPathComponent(".sourcekit-lsp")
244+
.appendingPathComponent("config.json")
242245
)
243246
)
244247
#if canImport(Darwin)
@@ -282,7 +285,9 @@ struct SourceKitLSP: AsyncParsableCommand {
282285
let realStdoutHandle = FileHandle(fileDescriptor: realStdout, closeOnDealloc: false)
283286

284287
// Directory should match the directory we are searching for logs in `DiagnoseCommand.addNonDarwinLogs`.
285-
let logFileDirectoryURL = URL(fileURLWithPath: ("~/.sourcekit-lsp/logs" as NSString).expandingTildeInPath)
288+
let logFileDirectoryURL = FileManager.default.sanitizedHomeDirectoryForCurrentUser
289+
.appendingPathComponent(".sourcekit-lsp")
290+
.appendingPathComponent("logs")
286291
await setUpGlobalLogFileHandler(
287292
logFileDirectory: logFileDirectoryURL,
288293
logFileMaxBytes: 5_000_000,

0 commit comments

Comments
 (0)