Name Generated files with the .g.cs
convention
#74
Replies: 8 comments 7 replies
-
Of course but, being just hints, this would guarantee exactly nothing.
The problem is that We need to generate code like the following (or the VB equivalent of course) exactly once regardless of which //------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Provides access to the current assembly information as pure constants,
/// without requiring reflection.
/// </summary>
[ExcludeFromCodeCoverage]
partial class ThisAssembly
{
} (Of course @pinkfloydx33 you can add the same code to your project as a quick workaround.) This can be solved two different ways:
Personally I'd go for number 1; the rationale is that @kzu, thoughts? |
Beta Was this translation helpful? Give feedback.
-
It's been a while since I originally posted and I'm not near a computer to check, but I think it has nothing to do with the attribute itself. Most coverage tools (Coverlet included) know how to exclude generated code from the coverage process. In this case they identity such code by various means, one being the g.cs extension. I can't recall offhand exactly since it's been so long, but I seem to remember during debugging that Coverlet ends up with a broken compilation due to its handling of such files. Attribute or not it'd still fail. I can confirm later today with your work-around, which would be enough for me if it does actually fix things. |
Beta Was this translation helpful? Give feedback.
-
Oh, now I see. Sorry @pinkfloydx33, you're right, the attribute would not help here - although it may be a good idea to add it nonetheless. What bugs me is, if the While I don't have a real, definitive solution to your problem, I'd suggest to research, as much as possible, a workaround that involves modifying your project, not source generators themselves. My rationale is that source generators are becoming nearly ubiquitous and your project could easily end up using three or four of them, some even from the SDK itself; good luck filing issues with every source generator whose output gets Coverlet confused. A possible workaround, assuming you run Coverlet immediately after a build, may be to tell Roslyn to "materialize" generated source files on disk, by adding the following XML code to your project (or to a <PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup> This way Coverlet will find generated all files on the local file system, regardless of the source generator involved. I hope this helps you. |
Beta Was this translation helpful? Give feedback.
-
Could you confirm that the workaround works? That would indicate whether the issue is that coverlet does not find the files, rather than an issue with the code extension? |
Beta Was this translation helpful? Give feedback.
-
I haven't tried yet. I'll try to check today. In the past I've had issues when outputting source generated files to disk...specifically not being able to compile at all since the compiler finds both the source on disk and the generared "in memory" files, ending up with duplicate classes, etc. Though that was a while back so perhaps that's been fixed. |
Beta Was this translation helpful? Give feedback.
-
Ok so the trick to getting In any event, this does not help Coverlet--it still fails to analyze the project. I'll just keep my work-around of using reflection in the app. I'm only grabbing two properties anyways. Maybe someday I'll update one of my generators to grab them. |
Beta Was this translation helpful? Give feedback.
-
At about the time this issue came up, I did some research about the best way to inject code in projects, avoiding problems with Coverlet as well as other tools. I've finally found the time to polish my results up and publish them as a blog post: here it is, should anyone care. |
Beta Was this translation helpful? Give feedback.
-
Coverlet has actually fixed the underlying problem finally! coverlet-coverage/coverlet#1164
|
Beta Was this translation helpful? Give feedback.
-
Can the generator be updated to name (hint) the generated "files" using the
.g.cs
convention?I just spent the last six hours trying to figure out why Coverlet stopped reporting coverage results for one of my projects. As it turns out, they identify generated sources using the
.g.cs
extension for the "file". If the extension is missing it breaks their logic as they now parse it as a missing file, silently breaking the rest of the process.Now I realize this isn't a bug with this library per se, however this has been the typical naming convention for generated files for years (even prior to source generators). It also has impact when using the option to output the generated files to disk (
EmitCompilerGeneratedFiles
) as those coming from this project don't match everything else.I was able to confirm this is truly the issue by updating one of my own generators to use a non-conforming file name.
I now need to remove this library from my project as I value the code coverage more than the constants...which is Incredibly unfortunate :-(
I plan on opening an issue with Coverlet, however their response in all the issues I scoured through this morning all say to "just name your generated files as
.g.cs
", which is all fine and good if you own the generator code. A similar report had surfaced related to Razor page source generators and it was dotnet that made the "fix" on their end, rather than Coverlet, so I'm not going to hold my breath.Beta Was this translation helpful? Give feedback.
All reactions