Build/Test Issues

Pdb loading fail using System.Reflection.Metadata

Started by bhugot on 3,509 views

Kiaora,

I met a problem with the il injection when using System.Reflection.Metadata it seem's the pdb file can't be read anymore.
This code is working in simple runner as in live unit testing but fail with ncrunch.


using System;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using Xunit;

namespace MetadataTests
{
    public class UnitTest1
    {
        [Fact]
        public void Test1()
        {
            using (var stream = File.OpenRead(typeof(UnitTest1).Assembly.Location))
            using (var reader = new PEReader(stream))
            {
                Func<string, Stream> streamProvider = p => new FileStream(p, FileMode.Open, FileAccess.Read);

                var metadata = reader.GetMetadataReader(MetadataReaderOptions.ApplyWindowsRuntimeProjections);
                var pdbLoaded = reader.TryOpenAssociatedPortablePdb(stream.Name, streamProvider, out var metadataReaderProvider,
                    out var pdbPath);
                Assert.True(pdbLoaded);
                var metadataSymbol = metadataReaderProvider.GetMetadataReader();
                var type = metadata.TypeDefinitions.Select(metadata.GetTypeDefinition).First(a => metadata.GetString(a.Name).Equals(nameof(UnitTest1)));
                
                var meta = type.GetMethods().Select(a => metadataSymbol.GetMethodDebugInformation(a)).ToList();

                var debug = meta.SelectMany(a => a.GetSequencePoints()).Select(a => a.Document).First();
                var docu = metadataSymbol.GetDocument(debug);

                var name = metadataSymbol.GetString(docu.Name);
                Assert.Contains("UnitTest1.cs", name);
            }
        }
    }
}
Hi, thanks for posting!

This is caused by NCrunch's current lack of support for portable PDBs. To compensate, the NCrunch build system automatically overrides your project settings so that the PDB generated for your project will always be a legacy Win32 PDB. This then allows NCrunch to properly read and manipulate the PDB file when it adds instrumentation.

Because System.Reflection.PortableExecution relies on the PDB being a portable (not legacy Win32), it's unable to read this PDB. So you'll likely get an exception when this code is run.

Implementing support for portable PDBs is a significant piece of work for NCrunch. We're hoping to find a way to handle this over the next year or so.

Post a reply

Log in to reply.