martinothamar/Mediator

CS8785: Generator 'MediatorGenerator' failed to generate source.

TorreyGarland opened this issue · 7 comments

I have a request that implements IRequest<byte[]>.

public class ExportRequest : IRequest<byte[]>
{
}

I have a request handler that implements IRequestHandler<ExportRequest, byte[]>.

public class ExportRequestHandler : IRequestHandler<ExportRequest, byte[]>
{
    // implementation
}

I get a CS8785 warning and no code is generated for the ExportRequestHandler. I have several other request, command and notification handlers, but this is the only one that expected to return a byte array result.

Also, the warning message is complaining: "Exception of type 'FileNotFoundException' with message 'Could not load file or assembly 'System.Runtime.CompilerServices.Unsafe, Version=6.0.0.0. blah blah blah' or one of its dependencies.

Are there any restrictions on TResponse types?

the project that I am using this on is a .Net6.0 project in VS 2022.

Code generation fails for any array type but succeeds for everything else. Tried string[], int[], Stream[].

using version 1.0.5

Hey, thanks for the report!
I'm currently working on 2.0, so I've pushed a fix for this as part of 2.0.15-preview.
Could you test it? Even though its 2.0 you shouldn't have any trouble upgrading (no breaking changes for "normal" use)

Will try it out.

Thanks for looking into this.

please keep up the good work.

I have not tried out the new preview version, but I wanted to bring to your attention some other anomalies that I am noticing.

Let's say if we have an ExportRequest : IRequest class declared, but no corresponding ExportRequestHandler : IRequestHandler<ExportRequest, Stream> class, I get the same CS8785 warning, plus errors regarding the assembly-level MediatorOptionsAttribute no longer existing (presumably not generated by the Incremental Generator).

It was my understanding that if this was the case, the Source/Incremental generator would report this as a warning in the error list, but not disrupt source code generation.

I am assuming that the CompilationAnalyzer might be throwing an exception, but it is not being properly reported. I cloned your repo, and it threw when trying to cast a instance of IArrayTypeSymbol to INamedTypeSymbol for an byte[] TResponse, but I see that you have corrected this already.

The CS8785 warning is very misleading and there are no other helpful indications of what or where the problem is.

Going to try out the new preview to see if there is any change now.

Thanks again for the quick response.

Some good news, though. 2.0.15-preview fixed the byte array response issue.

I thinnk you're right, there is some exception that must be thrown. I couldn't reprodude - tried creating a sample for debugging here: https://github.com/martinothamar/Mediator/pull/35/files#diff-539f5c67e6da0b76d2289ab3e3adfea62cc21d9607fdc6fb31f9e8e55384b705R1

Heres my output from running:
image
This is using the latest 2.0 preview

Can you spot a difference from your own setup?

The attribute should still be generated from the incremental compiler. I'm going to work on the error handling more generally, since I agree that exceptions that are thrown never makes sense.

I think I misunderstood you a bit when I wrote the above. I'm now assuming the specific issue you had is solved - but the error handling situation is pretty poor. I agree and that is what I've worked on a bit now.

It was my understanding that if this was the case, the Source/Incremental generator would report this as a warning in the error list, but not disrupt source code generation.

I've started #40 now where this is implemented. So if I remove the PingHandler in the "SimpleConsole" sample, it will emit a warning but still build with no other errors:
image

For more unexpected errors - like a bug in the analysis part of the source generator like you found initially - the exception will be reported as unexpected with some directions for error reporting:

image

I think this makes sense and is what you are suggesting in your message. Let me know if you have any more comments/feedback. Thanks!

right now it is working fine.

If I create the code for an IRequest class, I just have to remember to at least start the shell for the corresponding IRequestHandler.