Skip to content

Commit e8328ac

Browse files
authored
Fix error handling for dump symbol graph (#7141)
We were ignoring exit codes and output here, making the corresponding tests impossible to debug (and probably also making the command hard to use).
1 parent 9abbe3c commit e8328ac

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

Sources/Commands/PackageTools/DumpCommands.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,22 @@ struct DumpSymbolGraph: SwiftCommand {
6969
let targets = try buildSystem.getPackageGraph().rootPackages.flatMap{ $0.targets }.filter{ $0.type == .library }
7070
for target in targets {
7171
print("-- Emitting symbol graph for", target.name)
72-
try symbolGraphExtractor.extractSymbolGraph(
72+
let result = try symbolGraphExtractor.extractSymbolGraph(
7373
target: target,
7474
buildPlan: buildPlan,
75+
outputRedirection: .collect(redirectStderr: true),
7576
outputDirectory: symbolGraphDirectory,
7677
verboseOutput: swiftTool.logLevel <= .info
7778
)
79+
80+
if result.exitStatus != .terminated(code: 0) {
81+
switch result.output {
82+
case .success(let value):
83+
swiftTool.observabilityScope.emit(error: "Failed to emit symbol graph for '\(target.c99name)': \(String(decoding: value, as: UTF8.self))")
84+
case .failure(let error):
85+
swiftTool.observabilityScope.emit(error: "Internal error while emitting symbol graph for '\(target.c99name)': \(error)")
86+
}
87+
}
7888
}
7989

8090
print("Files written to", symbolGraphDirectory.pathString)

Sources/Commands/Utilities/SymbolGraphExtract.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import DriverSupport
2323
#endif
2424

2525
import class TSCBasic.Process
26+
import struct TSCBasic.ProcessResult
2627

2728
/// A wrapper for swift-symbolgraph-extract tool.
2829
public struct SymbolGraphExtract {
@@ -56,7 +57,7 @@ public struct SymbolGraphExtract {
5657
outputRedirection: TSCBasic.Process.OutputRedirection = .none,
5758
outputDirectory: AbsolutePath,
5859
verboseOutput: Bool
59-
) throws {
60+
) throws -> ProcessResult {
6061
let buildParameters = buildPlan.buildParameters
6162
try self.fileSystem.createDirectory(outputDirectory, recursive: true)
6263

@@ -101,6 +102,6 @@ public struct SymbolGraphExtract {
101102
outputRedirection: outputRedirection
102103
)
103104
try process.launch()
104-
try process.waitUntilExit()
105+
return try process.waitUntilExit()
105106
}
106107
}

Tests/CommandsTests/PackageToolTests.swift

+8-2
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ final class PackageToolTests: CommandsTestCase {
497497

498498
let arguments = withPrettyPrinting ? ["dump-symbol-graph", "--pretty-print"] : ["dump-symbol-graph"]
499499

500-
_ = try SwiftPM.Package.execute(arguments, packagePath: path, env: ["SWIFT_SYMBOLGRAPH_EXTRACT": symbolGraphExtractorPath.pathString])
500+
let result = try SwiftPM.Package.execute(arguments, packagePath: path, env: ["SWIFT_SYMBOLGRAPH_EXTRACT": symbolGraphExtractorPath.pathString])
501501
let enumerator = try XCTUnwrap(FileManager.default.enumerator(at: URL(fileURLWithPath: path.pathString), includingPropertiesForKeys: nil), file: file, line: line)
502502

503503
var symbolGraphURL: URL?
@@ -506,7 +506,13 @@ final class PackageToolTests: CommandsTestCase {
506506
break
507507
}
508508

509-
let symbolGraphData = try Data(contentsOf: XCTUnwrap(symbolGraphURL, file: file, line: line))
509+
let symbolGraphData: Data
510+
if let symbolGraphURL {
511+
symbolGraphData = try Data(contentsOf: symbolGraphURL)
512+
} else {
513+
XCTFail("Failed to extract symbol graph: \(result.stdout)\n\(result.stderr)")
514+
return nil
515+
}
510516

511517
// Double check that it's a valid JSON
512518
XCTAssertNoThrow(try JSONSerialization.jsonObject(with: symbolGraphData), file: file, line: line)

0 commit comments

Comments
 (0)