Skip to content

Commit e50e24d

Browse files
Add some more Roslyn generated file suffixes (#1352)
Add some more Roslyn generated file suffixes
1 parent 73396a3 commit e50e24d

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,13 @@ public bool PortablePdbHasLocalSource(string module, out string firstNotFoundDoc
178178
Document document = metadataReader.GetDocument(docHandle);
179179
string docName = _sourceRootTranslator.ResolveFilePath(metadataReader.GetString(document.Name));
180180
Guid languageGuid = metadataReader.GetGuid(document.Language);
181+
181182
// We verify all docs and return false if not all are present in local
182183
// We could have false negative if doc is not a source
183184
// Btw check for all possible extension could be weak approach
184185
// We exlude from the check the autogenerated source file(i.e. source generators)
185186
// We exclude special F# construct https://github.com/coverlet-coverage/coverlet/issues/1145
186-
if (!_fileSystem.Exists(docName) && !docName.EndsWith(".g.cs") &&
187+
if (!_fileSystem.Exists(docName) && !IsGeneratedDocumentName(docName) &&
187188
!IsUnknownModuleInFSharpAssembly(languageGuid, docName))
188189
{
189190
return (false, docName);
@@ -449,6 +450,35 @@ private static bool IsAssembly(string filePath)
449450
}
450451
}
451452

453+
// Follow the same rules that exist in Microsoft.CodeAnalysis
454+
// https://sourceroslyn.io/#Microsoft.CodeAnalysis/InternalUtilities/GeneratedCodeUtilities.cs,55bff725ec9f1338,references
455+
private static bool IsGeneratedDocumentName(string docPath)
456+
{
457+
if (!string.IsNullOrEmpty(docPath))
458+
{
459+
string fileName = Path.GetFileName(docPath);
460+
if (fileName.StartsWith("TemporaryGeneratedFile_", StringComparison.OrdinalIgnoreCase))
461+
{
462+
return true;
463+
}
464+
465+
string extension = Path.GetExtension(fileName);
466+
if (!string.IsNullOrEmpty(extension))
467+
{
468+
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(docPath);
469+
if (fileNameWithoutExtension.EndsWith(".designer", StringComparison.OrdinalIgnoreCase) ||
470+
fileNameWithoutExtension.EndsWith(".generated", StringComparison.OrdinalIgnoreCase) ||
471+
fileNameWithoutExtension.EndsWith(".g", StringComparison.OrdinalIgnoreCase) ||
472+
fileNameWithoutExtension.EndsWith(".g.i", StringComparison.OrdinalIgnoreCase))
473+
{
474+
return true;
475+
}
476+
}
477+
}
478+
479+
return false;
480+
}
481+
452482
private static bool IsUnknownModuleInFSharpAssembly(Guid languageGuid, string docName)
453483
{
454484
// https://github.com/dotnet/runtime/blob/main/docs/design/specs/PortablePdb-Metadata.md#document-table-0x30

0 commit comments

Comments
 (0)