Skip to content

Allow specification of where sourcekit-lsp diagnose writes its diagnose bundle #1428

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

Merged
merged 1 commit into from
Jun 5, 2024
Merged
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
33 changes: 25 additions & 8 deletions Sources/Diagnose/DiagnoseCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public struct DiagnoseCommand: AsyncParsableCommand {
)
private var components: [BundleComponent] = BundleComponent.allCases

@Option(
help: """
The directory to which the diagnostic bundle should be written. No file or directory should exist at this path. \
After sourcekit-lsp diagnose runs, a directory will exist at this path that contains the diagnostic bundle.
"""
)
var bundleOutputPath: String? = nil

var toolchainRegistry: ToolchainRegistry {
get throws {
let installPath = try AbsolutePath(validating: Bundle.main.bundlePath)
Expand Down Expand Up @@ -320,8 +328,12 @@ public struct DiagnoseCommand: AsyncParsableCommand {
let dateFormatter = ISO8601DateFormatter()
dateFormatter.timeZone = NSTimeZone.local
let date = dateFormatter.string(from: Date()).replacingOccurrences(of: ":", with: "-")
let bundlePath = FileManager.default.temporaryDirectory
.appendingPathComponent("sourcekit-lsp-diagnose-\(date)")
let bundlePath =
if let bundleOutputPath = self.bundleOutputPath {
URL(fileURLWithPath: bundleOutputPath)
} else {
FileManager.default.temporaryDirectory.appendingPathComponent("sourcekit-lsp-diagnose-\(date)")
}
try FileManager.default.createDirectory(at: bundlePath, withIntermediateDirectories: true)

if components.isEmpty || components.contains(.crashReports) {
Expand Down Expand Up @@ -353,12 +365,17 @@ public struct DiagnoseCommand: AsyncParsableCommand {
)

#if os(macOS)
// Reveal the bundle in Finder on macOS
do {
let process = try Process.launch(arguments: ["open", "-R", bundlePath.path], workingDirectory: nil)
try await process.waitUntilExitSendingSigIntOnTaskCancellation()
} catch {
// If revealing the bundle in Finder should fail, we don't care. We still printed the bundle path to stdout.
// Reveal the bundle in Finder on macOS.
// Don't open the bundle in Finder if the user manually specified a log output path. In that case they are running
// `sourcekit-lsp diagnose` as part of a larger logging script (like the Swift for VS Code extension) and the caller
// is responsible for showing the diagnose bundle location to the user
if self.bundleOutputPath == nil {
do {
let process = try Process.launch(arguments: ["open", "-R", bundlePath.path], workingDirectory: nil)
try await process.waitUntilExitSendingSigIntOnTaskCancellation()
} catch {
// If revealing the bundle in Finder should fail, we don't care. We still printed the bundle path to stdout.
}
}
#endif
}
Expand Down