microsoft / VSExtensibility

A repo for upcoming changes to extensibility in Visual Studio, the new extensibility model, and language server protocol.
MIT License
370 stars 48 forks source link

Could not load file or assembly 'Newtonsoft.Json...' #248

Closed ThomasArdal closed 1 year ago

ThomasArdal commented 1 year ago

I'm playing around with creating a new visualizer and copied most of the code from this sample: https://github.com/microsoft/VSExtensibility/tree/main/New_Extensibility_Model/Samples/RegexMatchDebugVisualizer

When running my code, I get the following error inside my custom XAML view:

image

I was under the impression that VS automatically loads Newtonsoft.Json used in this call:

ExceptionModel targetObjectValue = await visualizerTarget.ObjectSource.RequestDataAsync<ExceptionModel>(jsonSerializer: null, cancellationToken);

It doesn't help to install that package manually.

What could be the issue here?

matteo-prosperi commented 1 year ago

Hello, a few questions:

  1. are you able to use the unmodified RegexMatchDebugVisualizer?
  2. Which version of Visual Studio are you using? And which version of the Microsoft.VisualStudio.Extensibility.Sdk are you using?
  3. Which edition of Visual Studio are you using? (Community, Professional, Enterprise)
  4. If you put a breakpoint on the RequestDataAsync call, does the breakpoint get hit? In other words, does everything work fine until before the RequestDataAsync call?
  5. In your object source project, what package reference do you have? Which version of Microsoft.VisualStudio.DebuggerVisualizers? Did you add an explicit reference to Newtonsoft.Json or to some other package?

If you want to share your project (make sure not to include any code or information that you wouldn't want to be visible to the public), I would be happy to check it out. Thanks!

ThomasArdal commented 1 year ago

Good points. I will provide everything next week.

ThomasArdal commented 1 year ago

@matteo-prosperi I have some updates here.

  1. I'm getting the same error on an unmodified RegexMatchDebugVisualizer.
  2. Visual Studio Version 17.8.0 Preview 1.0. And the SDK is 17.7.19-preview-1.
  3. Professional.
  4. Yes, the breakpoint is hit in both my own project and in the unmodified RegexMatchDebugVisualizer. It's when stepping over that line that the exception happens.
  5. The only package reference in the source project is this:
<PackageReference Include="Microsoft.VisualStudio.DebuggerVisualizers" Version="17.6.1032901" />
matteo-prosperi commented 1 year ago

One issue that I see is that you are using Visual Studio Version 17.8.0 Preview 1.0 and the SDK is 17.7.19-preview-1. Because the VisualStudio.Extensibility libraries are in preview and see frequent breaking changes, you should always use the latest Visual Studio preview version (you are good there) and the corresponding version of the SDK that we advertise in this samples repo. Right now the version should be 17.8.13-preview-1 as you can see here.

Hopefully this will help

ThomasArdal commented 1 year ago

I just tried upgrading both packages to 17.8.13-preview-1. Unfortunately, it doesn't make any difference. The error doesn't seem to be related to that package version since I also get the JSON error on the unmodified RegexMatchDebugVisualizer that has the correct version.

matteo-prosperi commented 1 year ago

I tried using 17.8.13-preview-1 with VS 17.8.0 Preview 1.0 on a clean virtual machine and I was able to build, run and launch the RegexMatchDebugVisualizer. In my case the project that I was debugging was targeting .NET 8. What is the target framework of the project you are debugging, that is the only variable that I can think of.

ThomasArdal commented 1 year ago

The visualizer project is targeting net6.0-windows and the source project is targeting netstandard2.0. I think I just copied this from the sample.

matteo-prosperi commented 1 year ago

I mean what is the target framework of the application that you are debugging when you try to use the visualizer

ThomasArdal commented 1 year ago

Oh, good point. It's a net6.0 console app. I just tried creating a new net8.0 console app and my visualizer works 😮 Does this only work with .NET 8?

matteo-prosperi commented 1 year ago

Debugger visualizers are supposed to work with .NET 6. I was able to successfully test the sample RegexMatchVisualizer with net472, net6.0 and net8.0. So I can't reproduce your issue yet. I will tag in @mpeyrotc who works on the debugger side of visualizers.

ThomasArdal commented 1 year ago

I just tried it with a clean-from-template net6.0 console app. Everything works fine. So, it must be the console application that I have been testing with that is causing this. I'm suspecting it to include configuration like this that could cause this but not sure:

<RuntimeIdentifiers>win-x64;linux-x64;osx-x64</RuntimeIdentifiers>
<Platforms>x64</Platforms>

It's a self-contained console app, so maybe there are some issues there.

matteo-prosperi commented 1 year ago

Unfortunately, I am still unable to reproduce the issue even with those configuration and setting the project to be a self-contained console app. If you want to share the project that you are debugging, that may help.

ThomasArdal commented 1 year ago

Sure. It's this project here: https://github.com/elmahio/Elmah.Io.Cli

matteo-prosperi commented 1 year ago

I tested with your project and I was able to reproduce the issue. I think the reason for the error is that your project has an indirect reference on an earlier version of Newtonsoft.Json (10.0.3). tried adding <PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> (the version currently used in VS 2022 17.8 Preview 2) and I was able to get a visualizer to work. So my guess is that the debugger is not able to load a newer version of Newtonsoft.Json into the target process because an earlier version is already present. I agree that this may be an issue considering how widespread usage of Newtonsoft.Json is (and the fact that an updated VS is usually on the very latest version). @mpeyrotc, any idea how to address this? @ThomasArdal, a workaround for the moment, would be to pin the version of Newtonsoft.Json as I did above.

ThomasArdal commented 1 year ago

Makes sense, thank you. I'll update the package. But agree, this is probably something that should be fixed since I'm guessing a lot of people will run into this problem eventually.

ThomasArdal commented 1 year ago

As an update, I'm already starting to get error reports on this exact error. And this is with very limited installs and use of my extension. So, this is definitely going to be a problem :smile:

mpeyrotc commented 1 year ago

Hello @ThomasArdal, the issue has been fixed and is currently available in VS 17.8 Preview 3. More information can be found in the related VS developer community ticket.

ThomasArdal commented 1 year ago

@mpeyrotc As already mentioned in the community ticket the problem is still there in preview 4. I'll follow the community ticket for updates.