swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
66.69k stars 10.28k forks source link

Emit serialized diagnostics in JSON #73423

Open award999 opened 2 months ago

award999 commented 2 months ago

Motivation

The vscode-swift extension has been parsing diagnostics from stdout. This is output as a flat list. Whereas when sourcekit-lsp sends diagnostics, it will provide nested diagnostics for related information, and suggest quick fixes. There is the serialized diagnostics emitted to -serialize-diagnostics-path, but this is written in bitstream format which would be harder to deserialize in JS/TS.

Proposed solution

It would be amazing if swift could emit a serialized JSON file that would be a lot easier for us the consume in typescript. Ideally (1) we know the nested structure of diagnostics and (2) it seems like tsc knows about the available fixits so would be great to have those

Alternatives considered

No response

Additional information

No response

Pulkit1822 commented 1 month ago

@award999 ,to generate a JSON file from Swift diagnostics, I would first emit serialized diagnostics in JSON format from the Swift compiler to make it easier to consume in TypeScript, ideally containing the nested structure of diagnostics and information about available quick fixes; then use a library like swift-json-gen to generate JSON encoders and decoders based on Swift struct definitions, allowing me to parse JSON into immutable Swift structs and encode structs back to JSON; next, I would leverage the Diagnostics framework to easily share diagnostic information with users, supporting logging simple string messages, errors, and generating directory tree reports, while also providing custom insights based on the diagnostics; starting with iOS 15 and macOS 12, I would take advantage of the fact that apps that generate crash reports store the data as JSON in files with an .ips or .ips64 extension, which can be interpreted to diagnose problems; and finally, I would use the Codable protocol in Swift to load and decode specific kinds of data from JSON files, creating extensions on Bundle to load JSON files and decode them into Swift structs. I would like to work on the issue.