tareqimbasher / NetPad

A cross-platform C# editor and playground.
MIT License
1.34k stars 68 forks source link

Script with a Source Generator doesn't compile #233

Open mdonatas opened 2 months ago

mdonatas commented 2 months ago

A script with a Source Generator fails to compile when run although intelli-sense shows that a Source Generator has run (at least during edit time) as correct xml-doc was made for the partial method.

Ext.AbcOrDefGeneratedRegex().IsMatch("abc").Dump();

public static partial class Ext
{
    [GeneratedRegex("abc|def", RegexOptions.IgnoreCase, "en-US")]
    public static partial Regex AbcOrDefGeneratedRegex();
}
image
tareqimbasher commented 2 months ago

@mdonatas thank you for the feedback. I haven't messed with this scenario within NetPad yet, but this is a great use case that I'd like to support. I'll check it out.

tareqimbasher commented 3 weeks ago

Update: I tried to create a fix for this for the upcoming release but as far as I can tell .NET is missing a couple APIs to make this possible via the implementation currently used by NetPad. I'll give it another push after the next update.

mdonatas commented 2 weeks ago

Thanks for the update and for the effort. I take it NetPad uses different pipelines for intellisense and for building as intellisense can handle generators. Best of luck with this! There are more and more generators being used in dotnet so it would be really nice to have support for them.

tareqimbasher commented 2 weeks ago

I take it NetPad uses different pipelines for intellisense and for building as intellisense can handle generators.

It does. The problem I'm facing currently is I need to get the code the generator would output to include it when compiling your user code but there's no straightforward way to do that programmatically with a generator defined in the BCL (and not in your user code) that I've found so far. One rather hacky way to do it is to create a temp project and have Roslyn run the generator and grab the output but I'm trying to find a more elegant solution.