swift-openapi-generator
swift-openapi-generator copied to clipboard
Emit errors processing .yaml files that Xcode can display
I got the following error in the build transcript:
Showing All Messages
Error: dataCorrupted(Swift.DecodingError.Context(codingPath: [], debugDescription: "The given data was not valid YAML.", underlyingError: Optional(19:3: error: scanner: while scanning for the next token in line 19, column 3
found character that cannot start any token:
post:
^)))
It would be great if this were something Xcode could parse and show as an error in the error navigator.
That's a great idea. The CLI, which is used by the plugin, already emits warning and error diagnostics in a format that gets picked up by Xcode's Issue Navigator, but as you report here, that's not the case when the YAML parser throws an error.
The way I understand it, the piece of work here would be to catch this exact error in YAML parsing, extract the location information, and emit a Diagnostic with the right file path and line number, so that it shows up in IDEs correctly.
@czechboy0 I'd love to work on this little bit of functionality. Can you point me at some other diagnostic code that outputs things in the right format for Xcode?
I've pretty much figured out how to catch the yaml decoding error, but I'm unsure what to do with all the parts of the info I have (file path, line number, column, and the parsing error message).
@macblazer Great, thanks for considering to take it on!
The printing of diagnostics happens in https://github.com/apple/swift-openapi-generator/blob/f530c906215528a7450fc7602b9f93d9236eb4c0/Sources/_OpenAPIGeneratorCore/Diagnostics.swift#L249 when the plugin invokes the CLI.
The text formatting of diagnostics happens in https://github.com/apple/swift-openapi-generator/blob/f530c906215528a7450fc7602b9f93d9236eb4c0/Sources/_OpenAPIGeneratorCore/Diagnostics.swift#L105.
You might need to make some changes to the Diagnostic struct to add location to make it print in the format /file/to/openapi.yaml:1234 error: Failed to parse for this or that reason.
, which should get picked up by IDEs and other tools and highlight the right place in the yaml file. To see the exact format, put a build error into a random file, try to build, and then inspect the detailed log in the Report Navigator in Xcode to see the build output and how error messages with location are formatted.