Skip to content
Merged
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
1 change: 1 addition & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ let package = Package(
"SKOptions",
"SKSupport",
"SourceKitLSP",
"SwiftExtensions",
"ToolchainRegistry",
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
Expand Down
4 changes: 3 additions & 1 deletion Sources/Diagnose/DiagnoseCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ package struct DiagnoseCommand: AsyncParsableCommand {
let destinationDir = bundlePath.appendingPathComponent("logs")
try FileManager.default.createDirectory(at: destinationDir, withIntermediateDirectories: true)

let logFileDirectoryURL = URL(fileURLWithPath: ("~/.sourcekit-lsp/logs" as NSString).expandingTildeInPath)
let logFileDirectoryURL = FileManager.default.sanitizedHomeDirectoryForCurrentUser
.appendingPathComponent(".sourcekit-lsp")
.appendingPathComponent("logs")
let enumerator = FileManager.default.enumerator(at: logFileDirectoryURL, includingPropertiesForKeys: nil)
while let fileUrl = enumerator?.nextObject() as? URL {
guard fileUrl.lastPathComponent.hasPrefix("sourcekit-lsp") else {
Expand Down
11 changes: 5 additions & 6 deletions Sources/SKSupport/FileSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@
//===----------------------------------------------------------------------===//

import Foundation
import SwiftExtensions

import struct TSCBasic.AbsolutePath

/// The home directory of the current user (same as returned by Foundation's `NSHomeDirectory` method).
package var homeDirectoryForCurrentUser: AbsolutePath {
try! AbsolutePath(validating: NSHomeDirectory())
}

extension AbsolutePath {

/// Inititializes an absolute path from a string, expanding a leading `~` to `homeDirectoryForCurrentUser` first.
package init(expandingTilde path: String) throws {
if path.first == "~" {
try self.init(homeDirectoryForCurrentUser, validating: String(path.dropFirst(2)))
try self.init(
AbsolutePath(validating: FileManager.default.sanitizedHomeDirectoryForCurrentUser.path),
validating: String(path.dropFirst(2))
)
} else {
try self.init(validating: path)
}
Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftExtensions/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ add_library(SwiftExtensions STATIC
AsyncUtils.swift
Collection+Only.swift
Collection+PartitionIntoBatches.swift
FileManager+SanitizedHomeDirectoryOfCurrentUser.swift
NSLock+WithLock.swift
PipeAsStringHandler.swift
ResultExtensions.swift
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Foundation

extension FileManager {
/// Same as `homeDirectoryForCurrentUser` but works around
/// https://github.com/apple/swift-corelibs-foundation/issues/5041, which causes a null byte
package var sanitizedHomeDirectoryForCurrentUser: URL {
let homeDirectory = FileManager.default.homeDirectoryForCurrentUser
#if os(Windows)
if homeDirectory.lastPathComponent.hasSuffix("\0") {
let newLastPathComponent = String(homeDirectory.lastPathComponent.dropLast())
return homeDirectory.deletingLastPathComponent().appendingPathComponent(newLastPathComponent)
}
#endif
return homeDirectory
}
}
1 change: 1 addition & 0 deletions Sources/sourcekit-lsp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ target_link_libraries(sourcekit-lsp PRIVATE
SKOptions
SKSupport
SourceKitLSP
SwiftExtensions
ToolchainRegistry
ArgumentParser
TSCBasic)
Expand Down
9 changes: 7 additions & 2 deletions Sources/sourcekit-lsp/SourceKitLSP.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import SKLogging
import SKOptions
import SKSupport
import SourceKitLSP
import SwiftExtensions
import ToolchainRegistry

import struct TSCBasic.AbsolutePath
Expand Down Expand Up @@ -238,7 +239,9 @@ struct SourceKitLSP: AsyncParsableCommand {
var options = SourceKitLSPOptions.merging(
base: commandLineOptions(),
override: SourceKitLSPOptions(
path: URL(fileURLWithPath: ("~/.sourcekit-lsp/config.json" as NSString).expandingTildeInPath)
path: FileManager.default.sanitizedHomeDirectoryForCurrentUser
.appendingPathComponent(".sourcekit-lsp")
.appendingPathComponent("config.json")
)
)
#if canImport(Darwin)
Expand Down Expand Up @@ -282,7 +285,9 @@ struct SourceKitLSP: AsyncParsableCommand {
let realStdoutHandle = FileHandle(fileDescriptor: realStdout, closeOnDealloc: false)

// Directory should match the directory we are searching for logs in `DiagnoseCommand.addNonDarwinLogs`.
let logFileDirectoryURL = URL(fileURLWithPath: ("~/.sourcekit-lsp/logs" as NSString).expandingTildeInPath)
let logFileDirectoryURL = FileManager.default.sanitizedHomeDirectoryForCurrentUser
.appendingPathComponent(".sourcekit-lsp")
.appendingPathComponent("logs")
await setUpGlobalLogFileHandler(
logFileDirectory: logFileDirectoryURL,
logFileMaxBytes: 5_000_000,
Expand Down