'Sequence points missing in some portable PDB files

Context

I'm developing a documentation generating tool for .NET libraries, which extracts types from assemblies via reflection and relevant documentation from the compile-time generated XML.

My project for more context

Goal

I want to extend my tool with the ability to display for each type member (method, property...) their origin in the source code - extract the source file and relevant line number.

Current state

I've used System.Reflection.Metadata to load PDB files and extract the required data successfully.

using var stream = new StreamReader(pdbPath);
var provider = MetadataReaderProvider.FromPortablePdbStream(stream.BaseStream, MetadataStreamOptions.PrefetchMetadata, 0);
var reader = provider.GetMetadataReader();
var scopes = reader.MethodDebugInformation
  .Where(h => !h.IsNil)
  .Select(md => (reader.GetMethodDebugInformation(md), System.Reflection.Metadata.Ecma335.MetadataTokens.GetToken(md.ToDefinitionHandle())))
  .Where(m => !m.Item1.SequencePointsBlob.IsNil)
  .Select(m =>
  {
      return m.Item1.GetSequencePoints()
      .Select(sp =>
      {
           var document = reader.GetDocument(sp.Document);
            var name = reader.GetString(document.Name);

            return (m.Item2, sp.StartLine, name);
      })
      .OrderBy(x => x.StartLine)
      .First();
  })
  .ToArray();

Issue

The issue is that the MethodDebugInformation SequencePoints are not reliably available. Some of my PDBs return nothing, others return all member sequence points.

All tested PDBs come from .NET Core/5+ projects. No .NET Framework.

Question

What am I doing wrong? Are my PDBs compiled incorrectly? Or am I misusing the reflection library? Thanks in advance for any tips



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source