pakrym / jab

C# Source Generator based dependency injection container implementation.
MIT License
1.03k stars 33 forks source link

Removing unnecessary code from output assembly #150

Closed cn-ml closed 9 months ago

cn-ml commented 9 months ago

I just tried to migrate one of my projects from Injectio to Jab and one issue I came across is that the Marker attributes required for annotating the service provider are also contained in my output assembly when building. Is it possible to remove the attributes from the output assembly?

This is an issue to me, because it messes with the code coverage, and also because it is just not necessary to have these markers referenced in the assembly after building.

From what I see it should be possible to use conditionals or msbuild variables to hide these markers. I'm not really familiar with SG yet, but thats what I understood from Andrew Lock's blogpost.

Here is an image on how this looks on my code coverage report[^1].

image

[^1]: Dont make fun of my coverage, I'm working on it, it's just a rapid prototype.

pakrym commented 9 months ago

The code has to be there because these attributes are explicitly mentioned in your code.

Attributes.cs is marked with // <auto-generated/> and should be ignored by coverage tools. Check if your code coverage tool has "Ignore generated code settings".

cn-ml commented 9 months ago

Not particularly sure, i assumed coverlet would take care of that by default.

Alternatively, adding an ExcludeFromCodeCcoverageAttribute would likely help.

I will try to use coverage runsettings first.

skarllot commented 9 months ago

@pakrym What you think about adding [global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage] to the attributes and generated container?

pakrym commented 9 months ago

.NET uses System.CodeDom.Compiler.GeneratedCodeAttribute (https://github.com/search?q=repo%3Adotnet%2Fruntime%20s_generatedCodeAttribute&type=code) would that work?

skarllot commented 9 months ago

.NET uses System.CodeDom.Compiler.GeneratedCodeAttribute (https://github.com/search?q=repo%3Adotnet%2Fruntime%20s_generatedCodeAttribute&type=code) would that work?

Yes, I've noticed that on projects that I work the code with that attribute is not accounted in test coverage.

pakrym commented 9 months ago

Would you like to open a PR? 😉

skarllot commented 9 months ago

Would you like to open a PR? 😉

Sure, but will take a while

skarllot commented 9 months ago

I think that this issue is solved now, can you confirm @cn-ml?

pakrym commented 9 months ago

The fix was released in 0.10.0. Thank you @skarllot !

cn-ml commented 9 months ago

The classes Jab.FromNamedServicesAttribute and Jab.JabHelpers are not annotated with GeneratedCodeAttribute. Is that on purpose?

skarllot commented 8 months ago

The version 0.10.1 fixes it