Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit errors processing .yaml files that Xcode can display #65

Closed
JetForMe opened this issue Jun 13, 2023 · 3 comments · Fixed by #81
Closed

Emit errors processing .yaml files that Xcode can display #65

JetForMe opened this issue Jun 13, 2023 · 3 comments · Fixed by #81
Labels
area/generator Affects: plugin, CLI, config file. kind/enhancement Improvements to existing feature. size/S Small task. (A couple of hours of work.)

Comments

@JetForMe
Copy link

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.

@czechboy0 czechboy0 added area/generator Affects: plugin, CLI, config file. kind/enhancement Improvements to existing feature. size/S Small task. (A couple of hours of work.) labels Jun 13, 2023
@czechboy0
Copy link
Contributor

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.

@macblazer
Copy link
Contributor

@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).

@czechboy0
Copy link
Contributor

czechboy0 commented Jun 14, 2023

@macblazer Great, thanks for considering to take it on!

The printing of diagnostics happens in

public struct StdErrPrintingDiagnosticCollector: DiagnosticCollector {
when the plugin invokes the CLI.

The text formatting of diagnostics happens in

return "\(severity): \(message) [\(contextString.isEmpty ? "" : "context: \(contextString)")]"
.

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.

macblazer added a commit to macblazer/swift-openapi-generator that referenced this issue Jun 20, 2023
### Motivation

If there are any errors in the openapi.yaml file currently the errors show up in the build transcript
but are not shown in the Xcode issue sidebar and are also not linked to the openapi.yaml file in the sidebar.
This makes it hard to know what has failed, and what to do to fix the file.

### Modifications

The YAML parser now catches decoding errors, and if they are common parsing or scanning errors it will emit
a diagnostic message then throw a failure exit code.  This required importing the swift-argument-parser module into
the main _OpenAPIGeneratorCore.

Added `absoluteFilePath` and `lineNumber` optional properties to the `Diagnostics` struct to capture more info.
Modified the `Diagnostics.description` method to build a properly formatted message that Xcode can parse to point
out the file and line causing issues.

### Result

In consuming projects, invalid yaml in the openapi.yaml file is now highlighted in the Xcode Issue Navigator
after a build with this plugin.

### Test Plan

Added unit tests to verify emitted diagnostic and thrown error.  Also tested manually with an example project.

### Resolves

- Resolves apple#65.

Signed-off-by: Kyle Hammond <[email protected]>
simonjbeaumont pushed a commit that referenced this issue Jun 22, 2023
### Motivation

If there are any errors in the openapi.yaml file currently the errors
show up in the build transcript but are not shown in the Xcode issue
sidebar and are also not linked to the openapi.yaml file in the sidebar.
This makes it hard to know what has failed, and what to do to fix the
file.

### Modifications

The YAML parser now catches decoding errors, and if they are common
parsing or scanning errors it will emit a diagnostic message then throw
a failure exit code. This required importing the swift-argument-parser
module into the main _OpenAPIGeneratorCore.

Added `absoluteFilePath` and `lineNumber` optional properties to the
`Diagnostics` struct to capture more info. Modified the
`Diagnostics.description` method to build a properly formatted message
that Xcode can parse to point out the file and line causing issues.

### Result

In consuming projects, invalid yaml in the openapi.yaml file is now
highlighted in the Xcode Issue Navigator after a build with this plugin.

### Test Plan

Added unit tests to verify emitted diagnostic and thrown error. Also
tested manually with an example project.

### Resolves

- Resolves #65.

---------

Signed-off-by: Kyle Hammond <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/generator Affects: plugin, CLI, config file. kind/enhancement Improvements to existing feature. size/S Small task. (A couple of hours of work.)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants