2016-09-23 33 views
5

Theo sau từ this hướng dẫn từ MS, tôi đã tạo một trình phân tích cho Roslyn.Quy tắc phân tích Roslyn không thất bại trong việc xây dựng

Theo trang, bạn có thể đánh dấu sự cai trị như DiagnosticSeverity.Error, và điều này sẽ gây ra xây dựng để phá vỡ:

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);

Trong mã của tôi, tôi đã tạo ra sự cai trị nhiều hơn hoặc ít hơn như trình bày chi tiết ở đây:

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

Quy tắc này hoạt động tốt. Nó ném lên các đường màu đỏ, nó hiển thị thông báo trong danh sách lỗi. Tuy nhiên, việc xây dựng thành công và tôi có thể chạy ứng dụng thành công.

NB: Tôi đã tạo quy tắc này để chụp Thread.Sleep cho ví dụ này.

Code Capture

Có thiết lập bổ sung cần thiết để đảm bảo một quy tắc phá vỡ xây dựng?

Trả lời

11

Đây là tính năng của Máy phân tích chạy từ tệp 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

Để thực hiện việc xây dựng thất bại cho các quy tắc, bạn cần phải thêm máy phân tích như là một gói NuGet cho dự án. Điều này sẽ đảm bảo rằng các lỗi sẽ làm cho quá trình xây dựng thất bại như mong đợi.

Các vấn đề liên quan