Is it possible to use .NET regular expression source generators in LINQPad?
I was watching the Deep Dive into RegEx with Stephen Toub video and learned about .NET regular expression source generators, so I decided to see if I could use them in LINQPad but it doesn't seem possible. I created the following script in LINQPad v8.2.4 (X64) based on one shown in the video.
void Main()
{
Console.WriteLine(Example.Demo().Count("abc"));
}
static partial class Example
{
[GeneratedRegex("abc")]
public static partial Regex Demo();
}
Unfortunately this doesn't work, drawing a red squiggly under Demo in the partial method declaration; the error being "CS8795 Partial method 'UserQuery.Example.Demo()' must have an implementation part because it has accessibility modifiers." It seems that the generation of the other side of the partial class and method happens.
Have I missed some setting that might allow this or is it not possible in LINQPad at this time?
Comments
-
I noticed there was a new version and have updated to v8.3.7 but it hasn't helped.
-
LINQPad doesn't support source generators right now. It would require either switching the LINQPad GUI from .NET 6+ back to .NET Framework, or moving the entire Roslyn toolchain out-of-process - a massive job with performance implications.
-
I also wants this [GeneratedRegex] function.
-
This is kinda old, but just ran into not being able to use
GeneratedRegex. Understandable if it can't be done, but showing demand. -
I'm planning to support inbuilt source generators in LINQPad 9, time permitting.
-
V9 is here, so I guess time didn't permit?....
-
Yes, it didn't make the initial V9 RTM. It's high on the list now, though, and I'm expecting to get to it later this month.
-
Looking forward to it, thanks for your hard work and a great product!
-
Check out the 9.6.1 beta and let me know how you get along:
https://www.linqpad.net/linqpad9.aspx#beta -
Great work with source generators, thank you! @JoeAlbahari Would it be possible to implement
Go to definitionfor generated sources? For exampleAppJsonContext.Default.Personfrom the included sample.Also it would be great for
Go to definitionto work the same way as VS, so it would 'jump' between partial definitions or the way Resharper works -> list all definitions and allow choosing, currently LINQPad only goes to the first definition -
This functionality is now available in 9.6.3. Let me know how you get along.
-
@JoeAlbahari would it be possible to have a "Go to generated source" for the generated classes? That would open the generated source (if it writes source files somewhere) or generates it for viewing. Right now, the only way to get to it is through ILSpy and see its decompilation.
-
Use Go to Definition (F12). This will bring up the generated source in a read-only tab. In the case of partial classes, it will display a list for you to choose from.
-
That's not working for me. Am I missing something?
Go to Definition jumps to the partial declaration and stops there.

The only option as far as I can see from there is to open in ILSpy directly (Alt+Shift+R) for the method and open the class there.

v9.6.3 (Beta)
-
It seems like it's limited to going to the definition of partial classes, but not partial functions.
Looking at the Source Generators sample, what you describe works on the
AppJsonContext, but not for any of theLibraryImportfunctions or the Regex.
-
Additionally when opening generated sources and when hovering over types tooltips are not shown. Also when pressing F12 inside generated sources on existing types it does not work
-
Try now.
-
Looks good Joe thanks.
Though one thing, it should be applicable to properties as well as methods, and the generated property isn't working. I can see it in the generated source, I just can't jump to it. It looks like it was extended to allow for properties in .NET 9.
void Main() { Pattern.Match("foobar").Dump(); } [GeneratedRegex(@"^foobar$")] public partial Regex Pattern { get; } [GeneratedRegex(@"^foobar$")] public partial Regex GetPattern(); -
Thanks - I'll include this in the next build.
-
most of the tooltips and go to source now work for generated sources
but some still does not, example: go to AppJsonContext.Default.Personfrom the sample:

-
I tried installing a custom source generator NuGet - https://github.com/byme8/DuckInterface/tree/main - and it didn't seem to work.
Assuming that user-defined source generators are not yet supported?

-
@mellamokb unfortunately it's limited to a handful of generators, that are manually integrated.
GeneratedRegex
LibraryImport
JsonSerializableI think the issue was that it requires a completely different set of tooling to integrate with msbuild or something (which LINQPad does not use I think).
-
The underlying problem is that user-defined source generators can include an arbitrary graph of dependencies. To avoid the possibility of these conflicting with the LINQPad host's own dependencies (or those of other source generators) and to allow them to be reliably unloaded, LINQPad would have to load them into a separate process, like it does with data context drivers and scripts. This would then require running the whole Roslyn toolchain in another process, which would add considerable complexity and possibly hurt performance and reliability.
