1

I am trying to write a visualizer that maps expressions in an expression tree to Roslyn syntax nodes, in order to generate code for the expression tree. Part of the syntax tree generation is a call to the AdhocWorkspace constructor.

When I run the visualizer using the VisualizerDevelopmentHost, everything works just fine:

using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Linq.Expressions;

namespace _testVisualizer {
    class Program {
        [STAThread]
        static void Main(string[] args) {
            Expression<Func<bool>> expr = () => true;
            var data = new TestVisualizerData(expr);
            var visualizerHost = new VisualizerDevelopmentHost(data, typeof(TestVisualizer));
            visualizerHost.ShowVisualizer();

            Console.ReadKey(true);
        }
    }
}

But when I try to use the visualizer through the Visual Studio UI (by hovering over expr, clicking on the magnifying glass icon and choosing my visualizer), I get the following message:

Unable to perform function evaluation on the process being debugged.

Additional information

The function evaluation requires all threads to run.

I've identified the following as triggering the error:

workspace = new AdhocWorkspace();

which assigns to the workspace field on my Mapper class (source).

Why does calling the AdhocWorkspace constructor trigger this warning? How can I work around this?


This is an MCVE that demonstrates the issue:

using Microsoft.CodeAnalysis;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;

[assembly: DebuggerVisualizer(typeof(_testVisualizer.TestVisualizer), typeof(_testVisualizer.TestVisualizerDataObjectSource), Target = typeof(System.Linq.Expressions.Expression), Description = "Test Visualizer")]

namespace _testVisualizer {
    public class TestVisualizer : DialogDebuggerVisualizer {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider) {
            var data = (TestVisualizerData)objectProvider.GetObject();
            var txt = new TextBlock();
            txt.SetBinding(TextBlock.TextProperty, "Status");
            var window = new Window {
                DataContext = data,
                Content = txt
            };
            window.ShowDialog();
        }
    }

    [Serializable]
    public class TestVisualizerData {
        public TestVisualizerData() { }
        public TestVisualizerData(System.Linq.Expressions.Expression expr) {
            var workspace = new AdhocWorkspace();
            Status = "Success";
        }
        public string Status { get; set; }
    }

    public class TestVisualizerDataObjectSource : VisualizerObjectSource {
        public override void GetData(object target, Stream outgoingData) {
            var expr = (System.Linq.Expressions.Expression)target;
            var data = new TestVisualizerData(expr);
            base.GetData(data, outgoingData);
        }
    }
}
Zev Spitz
  • 13,950
  • 6
  • 64
  • 136
  • A duplicate [is here](https://stackoverflow.com/questions/34625059/how-to-debug-code-compiled-with-roslyn-in-a-visual-studio-extension-inside-curre). I concur with the author's answer, this exception is intentional to prevent deadlock. – Hans Passant Jan 16 '19 at 07:52
  • @HansPassant Could you clarify how this is a duplicate of the linked question? Are you saying that if the current solution would use a compiled version of the visualizer (instead of a project reference), then it would work? – Zev Spitz Jan 16 '19 at 13:28
  • @HansPassant I was unable to test this. Having created a new solution with only a reference to the visualizer DLL, the visualizer isn't registered with Visual Studio. – Zev Spitz Jan 16 '19 at 15:06
  • @HansPassant Looking over at this over two years later, I still fail to see the relevance here. The instance of the Roslyn compiler I am trying to open is self-contained within the visualizer, and thus unrelated to any Roslyn instance used by Visual Studio. – Zev Spitz Apr 21 '21 at 21:19

1 Answers1

0

Perhaps the AdhocWorkspace alternate constructor could be leveraged to use the SyntaxNode API in a single thread.

I've filed an issue.

Zev Spitz
  • 13,950
  • 6
  • 64
  • 136