Skip to content

Commit e0a8152

Browse files
committed
Improve swiftbuild error formatting
1 parent 49e69c7 commit e0a8152

File tree

1 file changed

+45
-8
lines changed

1 file changed

+45
-8
lines changed

Sources/SwiftBuildSupport/SwiftBuildSystem.swift

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,15 +353,23 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
353353
}
354354
progressAnimation.update(step: step, total: 100, text: message)
355355
case .diagnostic(let info):
356-
if info.kind == .error {
357-
self.observabilityScope.emit(error: "\(info.location) \(info.message) \(info.fixIts)")
358-
} else if info.kind == .warning {
359-
self.observabilityScope.emit(warning: "\(info.location) \(info.message) \(info.fixIts)")
360-
} else if info.kind == .note {
361-
self.observabilityScope.emit(info: "\(info.location) \(info.message) \(info.fixIts)")
362-
} else if info.kind == .remark {
363-
self.observabilityScope.emit(debug: "\(info.location) \(info.message) \(info.fixIts)")
356+
let fixItsDescription = if info.fixIts.hasContent {
357+
": " + info.fixIts.map { String(describing: $0) }.joined(separator: ", ")
358+
} else {
359+
""
360+
}
361+
let message = if let locationDescription = info.location.userDescription {
362+
"\(locationDescription) \(info.message)\(fixItsDescription)"
363+
} else {
364+
"\(info.message)\(fixItsDescription)"
364365
}
366+
let severity: Diagnostic.Severity = switch info.kind {
367+
case .error: .error
368+
case .warning: .warning
369+
case .note: .info
370+
case .remark: .debug
371+
}
372+
self.observabilityScope.emit(severity: severity, message: message)
365373
case .taskOutput(let info):
366374
self.observabilityScope.emit(info: "\(info.data)")
367375
case .taskStarted(let info):
@@ -509,6 +517,8 @@ public final class SwiftBuildSystem: SPMBuildCore.BuildSystem {
509517
}
510518
}
511519

520+
// MARK: - Helpers
521+
512522
extension String {
513523
/// Escape the usual shell related things, such as quoting, but also handle Windows
514524
/// back-slashes.
@@ -541,3 +551,30 @@ extension Basics.Diagnostic.Severity {
541551
self <= .info
542552
}
543553
}
554+
555+
fileprivate extension SwiftBuild.SwiftBuildMessage.DiagnosticInfo.Location {
556+
var userDescription: String? {
557+
switch self {
558+
case .path(let path, let fileLocation):
559+
switch fileLocation {
560+
case .textual(let line, let column):
561+
var description = "\(path):\(line)"
562+
if let column { description += ":\(column)" }
563+
return description
564+
case .object(let identifier):
565+
return "\(path):\(identifier)"
566+
case .none:
567+
return path
568+
}
569+
570+
case .buildSettings(let names):
571+
return names.joined(separator: ", ")
572+
573+
case .buildFiles(let buildFiles, let targetGUID):
574+
return "\(targetGUID): " + buildFiles.map { String(describing: $0) }.joined(separator: ", ")
575+
576+
case .unknown:
577+
return nil
578+
}
579+
}
580+
}

0 commit comments

Comments
 (0)