NeVeSpl / NTypewriter

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

Template processing fails when reading arguments from an Attribute that has arguments with default values (Roslyn project) #97

Closed dbuvid closed 9 months ago

dbuvid commented 1 year ago

An attribute with a signature with a default value argument, i.e. public SomeAttribute(string arg1, string arg2 = null)

With the usage

[Some("arg1Value"]

Causes an exception to be thrown when you attempt to access the Arguments property on the IAttribute model.

https://github.com/NeVeSpl/NTypewriter/blob/98fbb168afe3d394862b8bae299c8741853ac968/NTypewriter.CodeModel.Roslyn/Attribute.cs#L46C50-L46C70 is the root cause; defaulted arguments are not included in the ConstructorArguments array.

As a workaround I have just been explicitly passing these parameters in the necessary attributes.

NeVeSpl commented 1 year ago

Are you sure about that?

I have added unit test that shows there is no problem with handling default value for arguments.

dbuvid commented 1 year ago

Hmm, must be something I'm doing wrong -- I was attempting to write my own CLI but I determined just using the extension will meet my requirements.

I'm using the same C# custom functions for both the CLI and extension but this scenario works fine with the extension.

NeVeSpl commented 1 year ago

Unfortunately there are small discrepancies in the data obtained through Roslyn API, between CLI and VS extension. VS is doing perfect job in compiling solution, and getting exactly the same results even with the help of Buildalyzer is not easy.