Skip to content

Commit 477e491

Browse files
authored
Finalize the diagnostic engine if an error is thrown (#637)
This ensures that any gathered diagnostics are printed before exiting. rdar://110782381
1 parent 4fb746b commit 477e491

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

Sources/SwiftDocC/Infrastructure/Diagnostics/DiagnosticEngine.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,7 @@ public final class DiagnosticEngine {
9797
}
9898

9999
public func finalize() {
100-
workQueue.async { [weak self] in
101-
// If the engine isn't around then return early
102-
guard let self = self else { return }
100+
workQueue.sync {
103101
for consumer in self.consumers.sync({ $0.values }) {
104102
try? consumer.finalize()
105103
}

Sources/SwiftDocC/Infrastructure/DocumentationConverter.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
177177
mutating public func convert<OutputConsumer: ConvertOutputConsumer>(
178178
outputConsumer: OutputConsumer
179179
) throws -> (analysisProblems: [Problem], conversionProblems: [Problem]) {
180+
defer {
181+
diagnosticEngine.finalize()
182+
}
183+
180184
// Unregister the current file data provider and all its bundles
181185
// when running repeated conversions.
182186
if let dataProvider = self.currentDataProvider {
@@ -397,8 +401,6 @@ public struct DocumentationConverter: DocumentationConverterProtocol {
397401

398402
context.linkResolutionMismatches.reportGatheredMismatchesIfEnabled()
399403

400-
diagnosticEngine.finalize()
401-
402404
return (analysisProblems: context.problems, conversionProblems: conversionProblems)
403405
}
404406

Tests/SwiftDocCUtilitiesTests/ConvertActionTests.swift

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,6 +2323,47 @@ class ConvertActionTests: XCTestCase {
23232323
XCTAssert(error is ErrorsEncountered, "Unexpected error type thrown by \(ConvertAction.self)")
23242324
}
23252325
}
2326+
2327+
func testWritesDiagnosticFileWhenThrowingError() throws {
2328+
let bundle = Folder(name: "unit-test.docc", content: [
2329+
InfoPlist(displayName: "TestBundle", identifier: "com.test.example"),
2330+
CopyOfFile(original: symbolGraphFile, newName: "MyKit.symbols.json"),
2331+
TextFile(name: "Article.md", utf8Content: """
2332+
Bad title
2333+
2334+
This article has a malformed title and can't be analyzed, so it
2335+
produces one warning.
2336+
"""),
2337+
incompleteSymbolGraphFile,
2338+
])
2339+
2340+
let testDataProvider = try TestFileSystem(folders: [bundle, Folder.emptyHTMLTemplateDirectory])
2341+
let targetDirectory = URL(fileURLWithPath: testDataProvider.currentDirectoryPath)
2342+
.appendingPathComponent("target", isDirectory: true)
2343+
2344+
let diagnosticFile = try createTemporaryDirectory().appendingPathComponent("test-diagnostics.json")
2345+
2346+
var action = try ConvertAction(
2347+
documentationBundleURL: bundle.absoluteURL,
2348+
outOfProcessResolver: nil,
2349+
analyze: true,
2350+
targetDirectory: targetDirectory,
2351+
htmlTemplateDirectory: Folder.emptyHTMLTemplateDirectory.absoluteURL,
2352+
emitDigest: false,
2353+
currentPlatforms: nil,
2354+
dataProvider: testDataProvider,
2355+
fileManager: testDataProvider,
2356+
temporaryDirectory: createTemporaryDirectory(),
2357+
diagnosticLevel: "error",
2358+
diagnosticFilePath: diagnosticFile
2359+
)
2360+
2361+
XCTAssertFalse(FileManager.default.fileExists(atPath: diagnosticFile.path), "Diagnostic file doesn't exist before")
2362+
XCTAssertThrowsError(try action.performAndHandleResult()) { error in
2363+
XCTAssert(error is ErrorsEncountered, "Unexpected error type thrown by \(ConvertAction.self)")
2364+
}
2365+
XCTAssertTrue(FileManager.default.fileExists(atPath: diagnosticFile.path), "Diagnostic file exist after")
2366+
}
23262367

23272368
// Verifies setting convert inherit docs flag
23282369
func testConvertInheritDocsOption() throws {

0 commit comments

Comments
 (0)