Skip to content

Commit 379d282

Browse files
committed
Allow specification of where sourcekit-lsp diagnose writes its diagnose bundle
This allows VS Code to provide a command in the command palette that gathers sourcekit-lsp diagnostics alongside some VS Code Swift extension diagnostics.
1 parent cec73bb commit 379d282

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

Sources/Diagnose/DiagnoseCommand.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ public struct DiagnoseCommand: AsyncParsableCommand {
6565
)
6666
private var components: [BundleComponent] = BundleComponent.allCases
6767

68+
@Option(
69+
help: """
70+
The directory to which the diagnostic bundle should be written. No file or directory should exist at this path. \
71+
After sourcekit-lsp diagnose runs, a directory will exist at this path that contains the diagnostic bundle.
72+
"""
73+
)
74+
var bundleOutputPath: String? = nil
75+
6876
var toolchainRegistry: ToolchainRegistry {
6977
get throws {
7078
let installPath = try AbsolutePath(validating: Bundle.main.bundlePath)
@@ -320,8 +328,12 @@ public struct DiagnoseCommand: AsyncParsableCommand {
320328
let dateFormatter = ISO8601DateFormatter()
321329
dateFormatter.timeZone = NSTimeZone.local
322330
let date = dateFormatter.string(from: Date()).replacingOccurrences(of: ":", with: "-")
323-
let bundlePath = FileManager.default.temporaryDirectory
324-
.appendingPathComponent("sourcekit-lsp-diagnose-\(date)")
331+
let bundlePath =
332+
if let bundleOutputPath = self.bundleOutputPath {
333+
URL(fileURLWithPath: bundleOutputPath)
334+
} else {
335+
FileManager.default.temporaryDirectory.appendingPathComponent("sourcekit-lsp-diagnose-\(date)")
336+
}
325337
try FileManager.default.createDirectory(at: bundlePath, withIntermediateDirectories: true)
326338

327339
if components.isEmpty || components.contains(.crashReports) {
@@ -353,12 +365,17 @@ public struct DiagnoseCommand: AsyncParsableCommand {
353365
)
354366

355367
#if os(macOS)
356-
// Reveal the bundle in Finder on macOS
357-
do {
358-
let process = try Process.launch(arguments: ["open", "-R", bundlePath.path], workingDirectory: nil)
359-
try await process.waitUntilExitSendingSigIntOnTaskCancellation()
360-
} catch {
361-
// If revealing the bundle in Finder should fail, we don't care. We still printed the bundle path to stdout.
368+
// Reveal the bundle in Finder on macOS.
369+
// Don't open the bundle in Finder if the user manually specified a log output path. In that case they are running
370+
// `sourcekit-lsp diagnose` as part of a larger logging script (like the Swift for VS Code extension) and the caller
371+
// is responsible for showing the diagnose bundle location to the user
372+
if self.bundleOutputPath == nil {
373+
do {
374+
let process = try Process.launch(arguments: ["open", "-R", bundlePath.path], workingDirectory: nil)
375+
try await process.waitUntilExitSendingSigIntOnTaskCancellation()
376+
} catch {
377+
// If revealing the bundle in Finder should fail, we don't care. We still printed the bundle path to stdout.
378+
}
362379
}
363380
#endif
364381
}

0 commit comments

Comments
 (0)