0

I've been trying to get my feet wet with vs extensions & roslyn though I seem to have hit a roadblock pretty early on. I'm trying to detect when a file is saved, then analyse it to find certain base classes, properties etc.

I've got the following code in my package at the moment which successfully gets the contents of the saved file:

protected override void Initialize()
{
    base.Initialize();
    SetupEvents();
}

private DTE _dte;
private Events _dteEvents;
private DocumentEvents _dteDocEvents;

private void SetupEvents()
{
    _dte = (DTE)GetService(typeof(SDTE));
    _dteEvents = _dte.Events;
    _dteDocEvents = _dteEvents.DocumentEvents;
    _dteDocEvents.DocumentSaved += _dteDocEvents_DocumentSaved;
}


private void _dteDocEvents_DocumentSaved(Document document)
{
    string text = document.GetDocumentText();
    //var syntaxTree = Microsoft.CodeAnalysis.CSharp.CSharpSyntaxTree.ParseText(text);
}

I've got a breakpoint on the first line of my _dteDocEvents_DocumentSaved handler where I can see that the code is being hit. However, if I just uncomment the syntaxTree declaration line, my handler suddenly isn't getting called any more. This behaviour makes absolutely no sense to me, how can an additional line of code in my handler prevent the handler from being called at all?

Does anyone have any possible suggestions what could be going on here?

Thanks

James

JSteward
  • 6,833
  • 2
  • 21
  • 30
JCoyle
  • 100
  • 9
  • 1
    Enable Break on handled exceptions and see if anything happens. – SLaks Mar 07 '18 at 19:53
  • @SLaks I'm getting a load of AdalSilentTokenAcquisitionExceptions and RuntimeBinderExceptions for Newtonsoft json types, though I'm getting these when it does successfully debug. It definitely does seem to be a debugging issue though, since including that line of code means I can't even use Debug.WriteLine to write to the output window – JCoyle Mar 07 '18 at 20:16
  • You can ignore most of those. – SLaks Mar 07 '18 at 20:44
  • I managed to find that it was a problem with Microsoft.CodeAnalysis.CSharp loading, I found another so question here https://stackoverflow.com/questions/42536506/could-not-load-file-or-assembly-microsoft-codeanalysis-version-1-3-1-0-cultu I'm currently updating vs to see if that sorts the problem – JCoyle Mar 07 '18 at 20:59
  • Sounds like you're referencing a different version of Roslyn or setting it to Copy Local. – SLaks Mar 07 '18 at 21:11
  • I think versioning was the problem. After updating to the latest version of VS17, everything is working again. Thanks for your help @Slaks – JCoyle Mar 07 '18 at 22:03

1 Answers1

0

The issue was that Microsoft.CodeAnalysis.CSharp was failing to load. In order to fix the problem I had to update to the latest version of visual studio

JCoyle
  • 100
  • 9