Skip to content

Commit 840166c

Browse files
authored
[SwiftSyntax] Add debug print when the JSONDecoder fails to decode JSON (#14056)
from the compiler's '-emit-syntax'. Investigating: rdar://problem/36379512
1 parent 9922958 commit 840166c

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

test/SwiftSyntax/VisitorTest.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@
33
// REQUIRES: OS=macosx
44
// REQUIRES: objc_interop
55

6-
// FIXME: This test fails occassionally in CI with invalid json.
7-
// REQUIRES: disabled
8-
96
import StdlibUnittest
107
import Foundation
118
import SwiftSyntax
@@ -38,4 +35,4 @@ VisitorTests.test("Basic") {
3835
})
3936
}
4037

41-
runAllTests()
38+
runAllTests()

tools/SwiftSyntax/SwiftSyntax.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import Glibc
2525
public enum ParserError: Error {
2626
case swiftcFailed(Int, String)
2727
case invalidFile
28+
case jsonDecodeError(input: String, originalError: Error);
2829
}
2930

3031
extension Syntax {
@@ -42,8 +43,16 @@ extension Syntax {
4243
guard result.wasSuccessful else {
4344
throw ParserError.swiftcFailed(result.exitCode, result.stderr)
4445
}
46+
let jsonData = result.stdoutData
4547
let decoder = JSONDecoder()
46-
let raw = try decoder.decode(RawSyntax.self, from: result.stdoutData)
48+
let raw: RawSyntax
49+
do {
50+
raw = try decoder.decode(RawSyntax.self, from: jsonData)
51+
} catch let err {
52+
throw ParserError.jsonDecodeError(
53+
input: String(data: jsonData, encoding: .utf8) ?? jsonData.base64EncodedString(),
54+
originalError: err)
55+
}
4756
guard let file = Syntax.fromRaw(raw) as? SourceFileSyntax else {
4857
throw ParserError.invalidFile
4958
}

0 commit comments

Comments
 (0)