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

Source Generator ArgumentNullException #103

Closed
jons-bakerhill opened this issue Feb 9, 2024 · 8 comments
Closed

Source Generator ArgumentNullException #103

jons-bakerhill opened this issue Feb 9, 2024 · 8 comments
Assignees
Labels
bug Something isn't working SourceGenerator

Comments

@jons-bakerhill
Copy link

jons-bakerhill commented Feb 9, 2024

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" -}}

@jons-bakerhill jons-bakerhill changed the title Source Generater ArgumentNullException Source Generator ArgumentNullException Feb 9, 2024
@NeVeSpl NeVeSpl self-assigned this Feb 9, 2024
@NeVeSpl NeVeSpl added the bug Something isn't working label Feb 9, 2024
@NeVeSpl
Copy link
Owner

NeVeSpl commented Feb 9, 2024

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
Copy link
Author

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
Copy link
Owner

NeVeSpl commented Feb 9, 2024

You need two instances of VS,

  • one instance with loaded project that uses SG
  • second one with opened SG source code

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 :

<ProjectReference Include="..\NTypewriter.SourceGenerator\NTypewriter.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />

@jons-bakerhill
Copy link
Author

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
Copy link
Owner

NeVeSpl commented Feb 10, 2024

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:

string fullOutputPath = Path.Combine(outputPath);

@NeVeSpl
Copy link
Owner

NeVeSpl commented Feb 10, 2024

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.

@NeVeSpl NeVeSpl closed this as completed Feb 10, 2024
@jons-bakerhill
Copy link
Author

@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
Copy link
Owner

NeVeSpl commented Feb 12, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working SourceGenerator
Projects
None yet
Development

No branches or pull requests

2 participants