@@ -65,6 +65,14 @@ public struct DiagnoseCommand: AsyncParsableCommand {
65
65
)
66
66
private var components : [ BundleComponent ] = BundleComponent . allCases
67
67
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
+
68
76
var toolchainRegistry : ToolchainRegistry {
69
77
get throws {
70
78
let installPath = try AbsolutePath ( validating: Bundle . main. bundlePath)
@@ -320,8 +328,12 @@ public struct DiagnoseCommand: AsyncParsableCommand {
320
328
let dateFormatter = ISO8601DateFormatter ( )
321
329
dateFormatter. timeZone = NSTimeZone . local
322
330
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
+ }
325
337
try FileManager . default. createDirectory ( at: bundlePath, withIntermediateDirectories: true )
326
338
327
339
if components. isEmpty || components. contains ( . crashReports) {
@@ -353,12 +365,17 @@ public struct DiagnoseCommand: AsyncParsableCommand {
353
365
)
354
366
355
367
#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
+ }
362
379
}
363
380
#endif
364
381
}
0 commit comments