From e50e24d4545f8c9c918423812131ea0f9b67cd20 Mon Sep 17 00:00:00 2001 From: George Vanburgh <1670176+georgevanburgh@users.noreply.github.com> Date: Fri, 24 Jun 2022 08:37:05 +0100 Subject: [PATCH] Add some more Roslyn generated file suffixes (#1352) Add some more Roslyn generated file suffixes --- .../Helpers/InstrumentationHelper.cs | 32 ++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/coverlet.core/Helpers/InstrumentationHelper.cs b/src/coverlet.core/Helpers/InstrumentationHelper.cs index 70dd7b6..a953273 100644 --- a/src/coverlet.core/Helpers/InstrumentationHelper.cs +++ b/src/coverlet.core/Helpers/InstrumentationHelper.cs @@ -178,12 +178,13 @@ namespace Coverlet.Core.Helpers Document document = metadataReader.GetDocument(docHandle); string docName = _sourceRootTranslator.ResolveFilePath(metadataReader.GetString(document.Name)); Guid languageGuid = metadataReader.GetGuid(document.Language); + // We verify all docs and return false if not all are present in local // We could have false negative if doc is not a source // Btw check for all possible extension could be weak approach // We exlude from the check the autogenerated source file(i.e. source generators) // We exclude special F# construct https://github.com/coverlet-coverage/coverlet/issues/1145 - if (!_fileSystem.Exists(docName) && !docName.EndsWith(".g.cs") && + if (!_fileSystem.Exists(docName) && !IsGeneratedDocumentName(docName) && !IsUnknownModuleInFSharpAssembly(languageGuid, docName)) { return (false, docName); @@ -449,6 +450,35 @@ namespace Coverlet.Core.Helpers } } + // Follow the same rules that exist in Microsoft.CodeAnalysis + // https://sourceroslyn.io/#Microsoft.CodeAnalysis/InternalUtilities/GeneratedCodeUtilities.cs,55bff725ec9f1338,references + private static bool IsGeneratedDocumentName(string docPath) + { + if (!string.IsNullOrEmpty(docPath)) + { + string fileName = Path.GetFileName(docPath); + if (fileName.StartsWith("TemporaryGeneratedFile_", StringComparison.OrdinalIgnoreCase)) + { + return true; + } + + string extension = Path.GetExtension(fileName); + if (!string.IsNullOrEmpty(extension)) + { + string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(docPath); + if (fileNameWithoutExtension.EndsWith(".designer", StringComparison.OrdinalIgnoreCase) || + fileNameWithoutExtension.EndsWith(".generated", StringComparison.OrdinalIgnoreCase) || + fileNameWithoutExtension.EndsWith(".g", StringComparison.OrdinalIgnoreCase) || + fileNameWithoutExtension.EndsWith(".g.i", StringComparison.OrdinalIgnoreCase)) + { + return true; + } + } + } + + return false; + } + private static bool IsUnknownModuleInFSharpAssembly(Guid languageGuid, string docName) { // https://github.com/dotnet/runtime/blob/main/docs/design/specs/PortablePdb-Metadata.md#document-table-0x30