-1

I'm developing a Visual Studio Extension and I need to show errors in the VS Error List to the user. I want these errors to prevent/break the building/running of the solution, i.e., something like this. How can I achieve this?

I've tried two approaches but both of them only show errors, they do note break/prevent building of the solution:

  1. Adapt the ErrorList VSSDK Example project such that errors instead of messages are raised. Errors are shown properly but building is still possible.
  2. Use the ErrorListProvider as described here. Same issue as 1.

I've also looked a bit at using Diagnostics but you need ReportDiagnostic which seems to be related to code Analyzers specifically.

Robin M
  • 3
  • 1
  • So .. are you wanting to `try` something and then `catch` the exceptions? – Jaskier Nov 18 '19 at 16:51
  • I'm pretty sure that thrown `Exceptions` in my extension code will not break the code (i.e., the build process) of the user using my extension. That would mean you can never run your solution if one of your extensions is broken. But yes, generally, my extension checks some conditions (which could of course be `try/catch`) and if one of them is not adhered to then my extension should prevent the user from building, i.e., I have to show an error in the VS Error List and that error should break the build pipeline. – Robin M Nov 19 '19 at 12:53

2 Answers2

0

What you actually need is a build error. For this, you should use Roslyn technology. See this similar issue.

Reason why the error occurs in Error list but can't break the build:

According to this similar issue, it's by design that the rules from Nuget can break the build in VS IDE while rules from VS extension can't. So we can use the MSBuild magic in nuget to break the build if we need.

Community
  • 1
  • 1
LoLance
  • 25,666
  • 1
  • 39
  • 73
  • Thank you for the reference to the other topic, I will definitely look more into that! Interestingly, for approach 1. (which uses the VSSDK Example) the errors are indeed `Intellisense` errors. For approach 2., however, the errors are `Build` errors. Still, the build process does not break. – Robin M Nov 19 '19 at 12:46
  • It's quite strange, do you see the error occurs in the `output window` after build? – LoLance Nov 20 '19 at 11:36
  • Thanks to your reference I found that from an extension you cannot break the build because an extension is IDE specific (https://github.com/dotnet/roslyn/issues/6195). I have to add a NuGet (which contains an MSBuildTask that throws errors) to the project in order to break it. – Robin M Nov 21 '19 at 12:33
  • Yes, like that described in build errors part in [this document](https://learn.microsoft.com/en-us/visualstudio/code-quality/roslyn-analyzers-overview?view=vs-2019#build-errors). Will update the answer with this detail tomorrow:) – LoLance Nov 21 '19 at 13:14
0

You should extend Roslyn if and only if your Extension analyzes C# or VB code.

My usecase had neither, yet I needed to raise errors that prevent compilation.

Solution:

cancel ongoining builds with

var dte = Package.GetGlobalService(typeof(DTE)) as DTE2;

//  will be called if debugging, running, or building
dte.Events.BuildEvents.OnBuildBegin += (cancellationToken, progress) => {
    if(isHasErrors)  // TODO check for errors
        dte.ExecuteCommand("Build.Cancel");
};
Olli
  • 1
  • 1