2016-09-23 2 views
5

Следующее из this учебник от MS, я создал анализатор для Roslyn.Правило анализатора Roslyn не подводит итог:

Согласно странице, вы можете пометить правило как DiagnosticSeverity.Error, и это заставит строить ломаться:

In the line declaring the Rule field, you can also update the severity of the diagnostics you’ll be producing to be errors rather than warnings. If the regex string doesn’t parse, the Match method will definitely throw an exception at run time, and you should block the build as you would for a C# compiler error. Change the rule’s severity to DiagnosticSeverity.Error:

internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, DiagnosticSeverity.Error, isEnabledByDefault: true, description: Description);

В моем коде, я создал правило, более или менее, как описано здесь:

private static readonly DiagnosticDescriptor Rule = 
    new DiagnosticDescriptor(DiagnosticId, Title, MessageFormat, Category, 
    DiagnosticSeverity.Error, true, helpLinkUri: HelpUrl); 

Это правило отлично работает. Он подбрасывает красные линии, отображает сообщение в списке ошибок. Однако сборка успешно завершена, и я могу успешно запустить приложение.

NB: Я создал это правило для захвата Thread.Sleep для этого примера.

Code Capture

Есть ли дополнительные настройки, необходимые для обеспечения правило нарушает сборку?

ответ

11

Это функция анализаторов, запущенных из файла VSIX.

If the IDE-installed rules ran as part of the in-IDE build, it would result in IDE builds and command line builds having potentially very different outputs. For example, a user with code-cracker installed as a VSIX could end up filing a bug report that an open source project does not build due to an analyzer error (or perhaps a warning when the project uses /warnaserror). They would be forced to either uninstall the analyzer extension or modify the rule set used by the project to disable some rule that only exists on one developer's machine.

In contrast, rules that are installed via NuGet become part of the project and part of the build. They run the same way across developer machines, and they run the same way in-IDE, on the command line, and in automated build environments.

Source: IDE rules don't fail builds

Для того, чтобы сделать билд неудачу для правил, вам нужно добавить анализатор в качестве NuGet пакета к проекту. Это гарантирует, что сбои приведут к сбою сборки, как ожидалось.