Daily Usage Issues

Extension Methods in Watch window

Started by SchlaWiener on 6,344 views

Consider the following test


    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            var items = new List<string> { "Dog", "Cat" };
            var item = items.First();

            Assert.AreEqual("Dog", item);
        }


Now, if I debug the Test with Visual Studio I can use Enumerable.Extensions like [h]First()[/h] but with NCrunch I can not. This makes it sometimes very hard to debug ncrunch tests.
A great blog post why this is possible with Visual Studio can be found here. http://blogs.msdn.com/b/jaredpar/archive/2010/07/22/extension-methods-and-the-debugger.aspx


With MSTest
External image

With NCrunch
External image

If I understand the blog post right, it would be possible to achive this, If NCrunch would preload System.Core into it's debugger process. Is there a way to do this. I even tried the "Pre-load all assembly references into test environment" but it didn't help.

On a side note: Extension Methods within the same project seem to work. If I add

    
public static class ExtensionMethods
    {
        public static T First2<T>(this IEnumerable<T> source)
        {
            return source.First();
        }
    }


to the project I can use items.First2() in the debugger.

I use
- Visual Studio 2010 Profession
- Windows 8.1 x64
- NCrunch 2 (latest)
Hi,

Thanks for sharing this and for taking the time to explain it in detail.

There is more going on here than just the need to load System.Core.dll into the process - although I can understand why preloading this DLL might make a difference in resolving the extension method. The problem for NCrunch is deeper and actually much more serious.

Each method written to a DLL by the C# compiler is associated with debug metadata written to a PDB file. This metadata includes information such as local variable names, iterator data (i.e. delegates to virtual classes constructed by the compiler) and namespace usage data. When you debug your code, the VS debugger extracts this metadata from the PDB and uses it to make sense of the debug context.

In all presently released versions of NCrunch, there is a known problem where this debug metadata is not being correctly rewritten when NCrunch instruments assemblies prior to running tests. This means that when you debug your method under NCrunch, the metadata simply isn't there. The debugger won't know about the First() method because it doesn't know where to find it.

The metadata itself is in an undocumented MS format that isn't clearly formed by any of the available debug APIs. This is why it has been impossible to preserve it or write it back to the PDB. PDB files themselves are based on a very old format controlled by internal windows component that is generally a horrible nightmare to work with.

However, the release of Roslyn's source code has put new light over the structure of the metadata and I'm happy to say that the next release of NCrunch will include some fixes in this area. It still remains to be see how the fixes will perform (the windows symbol writer has a mind of its own), but I'm hopeful that you will see an improvement in issues such as this with the release of NCrunch v2.8.

Cheers,

Remco

Post a reply

Log in to reply.