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

Enforce error diagnostics by aborting execution #607

Conversation

PARAIPAN9
Copy link
Contributor

@PARAIPAN9 PARAIPAN9 commented Aug 2, 2024

Motivation

Modifications

  • Create ErrorThrowingDiagnosticCollector wrapper collector.

Result

  • The ErrorThrowingDiagnosticCollector throws an error when a diagnostic with severity .error is emited.

Test Plan

  • Make sure all tests pass and add additional test.

@PARAIPAN9
Copy link
Contributor Author

I have a question because I'm a bit confused about what to do after the creation of the wrapper. Should we use the wrapper in place of the extensions from DiagnosticCollector?

@czechboy0
Copy link
Contributor

I think DiagnosticCollector's emit method should become throwing, and then create a new concrete collector implementation that looks something like this:

struct ErrorThrowingDiagnosticCollector: DiagnosticCollector {
    var upstream: any DiagnosticCollector

    func emit(_ diagnostic: Diagnostic) throws {
        upstream.emit(diagnostic)
        if diagnostic.severity == .error { throw diagnostic }
    }
}

And then use this collector by default, wrapping today's collector.

@PARAIPAN9
Copy link
Contributor Author

I think DiagnosticCollector's emit method should become throwing, and then create a new concrete collector implementation that looks something like this:

struct ErrorThrowingDiagnosticCollector: DiagnosticCollector {
    var upstream: any DiagnosticCollector

    func emit(_ diagnostic: Diagnostic) throws {
        upstream.emit(diagnostic)
        if diagnostic.severity == .error { throw diagnostic }
    }
}

And then use this collector by default, wrapping today's collector.

It makes sense, perfect!!

@PARAIPAN9 PARAIPAN9 marked this pull request as ready for review August 5, 2024 11:00
@czechboy0
Copy link
Contributor

@swift-server-bot test this please

@czechboy0
Copy link
Contributor

Ok the code looks good, would you be able to add one test that constructs the pipeline with a malformed input and ends up throwing the error? Just to verify that it's really working?

@PARAIPAN9
Copy link
Contributor Author

Sure.

@czechboy0
Copy link
Contributor

@swift-server-bot test this please

@czechboy0
Copy link
Contributor

Got some build failures on Linux:

/code/Tests/OpenAPIGeneratorTests/Test_GenerateOptions.swift:38:34: error: cannot call value of non-function type 'String'
        let arguments = [docPath.path(), "--config", configPath.path]
                                 ^   ~~
                                     
/code/Tests/OpenAPIGeneratorTests/Test_GenerateOptions.swift:42:94: error: 'nil' requires a contextual type
            try await generator.runGenerator(outputDirectory: outputDirectory, pluginSource: nil, isDryRun: false)

You can fix the first one by using docPath.path (the property) instead. For the second, just pass the "build plugin" plugin source case.

@czechboy0
Copy link
Contributor

@swift-server-bot test this please

@czechboy0
Copy link
Contributor

** Running /code/scripts/check-license-headers.sh...
** ERROR: Unsupported file extension for file (exclude or update this script): Tests/OpenAPIGeneratorTests/Resources/Docs/malformed-openapi.yaml

You'll need to add the file to the license check ignore list.

@czechboy0
Copy link
Contributor

Hmm this actually breaks 5.9.0, because it creates a dependency of a test target on an executable target. https://ci.swiftserver.group/job/swift-openapi-generator-swift590-prb/246/console

To fix this, you'll need to start depending on _OpenAPIGeneratorCore instead of swift-openapi-generator, and move _YamlFileDiagnosticsCollector to _OpenAPIGeneratorCore, and create a method called e.g. preparedDiagnosticsCollector(...) -> DiagnosticCollector & Sendable that lives in _OpenAPIGeneratorCore. Then just test that method, and call the method from the swift-openapi-generator code.

I don't think we'll be able to make the new dependency work otherwise.

@czechboy0
Copy link
Contributor

@swift-server-bot test this please

Package.swift Outdated Show resolved Hide resolved
@czechboy0
Copy link
Contributor

@swift-server-bot test this please

@czechboy0
Copy link
Contributor

@swift-server-bot test this please

@czechboy0
Copy link
Contributor

@PARAIPAN9 The CI is happy, but I believe we still need to move the remaining diagnostic collection code into the new prepare... function, so that it's called from the CLI and also from tests. Right now we're still re-creating the collectors using different code in the CLI vs the tests.

Copy link
Contributor

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above.

Copy link
Contributor

@czechboy0 czechboy0 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @PARAIPAN9, looks great now!

@czechboy0 czechboy0 merged commit 86ae4f6 into apple:main Sep 18, 2024
9 checks passed
@czechboy0 czechboy0 added the 🔨 semver/patch No public API change. label Sep 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 semver/patch No public API change.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enforce error diagnostics by aborting execution
2 participants