NeVeSpl / NTypewriter

File/code generator using Scriban text templates populated with C# code metadata from Roslyn API.
https://nevespl.github.io/NTypewriter/
MIT License
117 stars 24 forks source link

Source Generator ArgumentNullException #103

Closed jons-bakerhill closed 5 months ago

jons-bakerhill commented 5 months ago

With both 0.2.6 and 0.3.2 I get the following error during build:

CSC : warning CS8785: Generator 'NTypewriterSourceGenerator' failed to generate source. It will not contribute to the output and compilation errors may occur as a result. Exception was of type 'ArgumentNullException' with message 'Value cannot be null.

The template runs fine using the VS extension.

I'm happy to do more troubleshooting, just not sure where to start.


Things stopped rendering altogether shortly after posting the above (possibly related to changing NuGet/VS Extension things) and I had to reboot to get templates to attempt to render again. I'd see "ProjectBuildDone event" but nothing would happen after that. Post reboot when I try to render by saving the template it tries again but now I get 13:10:03.127 ERROR: RpuQCc2 <input>(53,19) Unexpected exception while accessing target expression: Exception has been thrown by the target of an invocation. where line 53 is {{ for class in data.Classes #| Symbols.ThatHaveAttribute "ApiModel" -}}

NeVeSpl commented 5 months ago

I see that things escalated quickly.

Do you use by any chance custom functions/*.nt.cs files?

You can always try to debug source generator, the code of the SG is a very simple one.

Keep in mind, that VS extensions and source generator are running in the same app domain(inside VS process), they share the same dlls, if you mix the latest version of extension with not the latest version of SG, you may get exception like the second one from your post.

jons-bakerhill commented 5 months ago

I don't have any custom *.nt.cs files.

Do you have a link on how to debug something running inside VS like the source generator? I've done lots of debugging over the years but nothing "inside" VS like that so I'm not sure where to start.

I have 0.5.2 of the extension and 0.3.2 of the source generator. I don't have any other NTypeWriter packages or extensions installed. I did a reboot after updating the packages and extension and things got "better" in that they started attempting to generate again but I'm still getting the "Unexpected exception" error when the template renders on save via the extension and source gen still fails. I'll try one more reboot after posting this comment but unless I come back and go "yay it's fixed" you can assume that didn't help.

NeVeSpl commented 5 months ago

You need two instances of VS,

and you simply do : Debug -> Attach to process - from the second to the first instance of VS

You can also replace SG nuget with straight forward reference to SG project the same way as it is done here :

https://github.com/NeVeSpl/NTypewriter/blob/a854e6a3ccad7948f7427b6698dfe90977a51444/Tests.Assets.WebApi2022/Tests.Assets.WebApi2022.csproj#L25

jons-bakerhill commented 5 months ago

Haven't had any luck getting debugging working (yet) mostly following this https://github.com/JoanComasFdz/dotnet-how-to-debug-source-generator-vs2022 I was setting up for the attach when I noticed that the extension seemed fine after I uninstalled all the source generator nuget packages so I grabbed 1 of the 100+ projects and both the extension and the source generator appear to work with just that one so I guess I'll start binary searching the projects to see if I can figure out which one is making it blow up.

NeVeSpl commented 5 months ago

I have suspicion that the output path of one of yours projects may be null, this line would definitely throw exception in such a case:

https://github.com/NeVeSpl/NTypewriter/blob/a854e6a3ccad7948f7427b6698dfe90977a51444/NTypewriter.SourceGenerator/SourceGenerator.cs#L119

NeVeSpl commented 5 months ago

I think that all problems mentioned here were addressed, unfortunately I am not able to updated old SG releases, but since now on, they should be completely independent from VS extension.

jons-bakerhill commented 5 months ago

@NeVeSpl I pulled the latest and built locally. That fixed the ArgumentNullException. Any idea when the next release might be?

I suggest adding a build failing diagnostic (instead of the current warning diagnostics) when no templates are found along the lines of "No *.nt template files were found. Please make sure there is a template present in the project and its Build Action is set to C# analyzer additional file". It doesn't make sense to me to have the source builder in a project and not be using it.

That last bit gets tricky when referencing files via "existing file links" as it jumps you to the actual file location if you left click on it, you have to right click first to be able to edit the link properties and set it as an additional file. Probably not worth adding that part to the error message as it's likely an uncommon scenario.

I have some hacked in diagnostics I might be able to create a PR for, but I'm already pushing the limits of the time I can spend on this at my employer.

Thanks for the responsiveness and quick fix!

NeVeSpl commented 5 months ago

Nuget is already published, for small fixes I usually do not do releases.

Yeah, I am not fond of adding this failing diagnostics, I would prefer to leave it as it is. The core of NT is shared between, SG, VS extension, and Blazor app, I would prefer to keep it as end platform agnostic as possible.