diff --git a/docs/csharp_binary.md b/docs/csharp_binary.md index 79a188cd..2e528893 100644 --- a/docs/csharp_binary.md +++ b/docs/csharp_binary.md @@ -13,9 +13,10 @@ csharp_binary(name, defines, deps, generate_documentation_file, include_host_model_dll, internals_visible_to, keyfile, langversion, nullable, out, override_strict_deps, override_treat_warnings_as_errors, override_warning_level, override_warnings_as_errors, - override_warnings_not_as_errors, project_sdk, resources, runtime_identifier, srcs, - strict_deps, target_frameworks, targeting_packs, treat_warnings_as_errors, - warning_level, warnings_as_errors, warnings_not_as_errors, winexe) + override_warnings_not_as_errors, project_sdk, resources, run_analyzers, + runtime_identifier, srcs, strict_deps, target_frameworks, targeting_packs, + treat_warnings_as_errors, warning_level, warnings_as_errors, warnings_not_as_errors, + winexe) Compile a C# exe @@ -47,6 +48,7 @@ Compile a C# exe | override_warnings_not_as_errors | Whether or not to override the warnings_not_as_errors attribute. | Boolean | optional | False | | project_sdk | The project SDK that is being targeted. See https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview | String | optional | "default" | | resources | A list of files to embed in the DLL as resources. | List of labels | optional | [] | +| run_analyzers | Controls whether analyzers run at build time. | Boolean | optional | True | | runtime_identifier | The runtime identifier that is being targeted. See https://docs.microsoft.com/en-us/dotnet/core/rid-catalog | String | required | | | srcs | The source files used in the compilation. | List of labels | optional | [] | | strict_deps | Whether to use strict dependencies or not.

This attribute mirrors the DisableTransitiveProjectReferences in MSBuild. The default setting of this attribute can be overridden in the toolchain configuration | Boolean | optional | True | diff --git a/docs/csharp_library.md b/docs/csharp_library.md index 0c0cebbf..8fd9682a 100644 --- a/docs/csharp_library.md +++ b/docs/csharp_library.md @@ -13,9 +13,9 @@ csharp_library(name, exports, generate_documentation_file, internals_visible_to, keyfile, langversion, nullable, out, override_strict_deps, override_treat_warnings_as_errors, override_warning_level, override_warnings_as_errors, override_warnings_not_as_errors, - project_sdk, resources, runtime_identifier, srcs, strict_deps, target_frameworks, - targeting_packs, treat_warnings_as_errors, warning_level, warnings_as_errors, - warnings_not_as_errors) + project_sdk, resources, run_analyzers, runtime_identifier, srcs, strict_deps, + target_frameworks, targeting_packs, treat_warnings_as_errors, warning_level, + warnings_as_errors, warnings_not_as_errors) Compile a C# DLL @@ -46,6 +46,7 @@ Compile a C# DLL | override_warnings_not_as_errors | Whether or not to override the warnings_not_as_errors attribute. | Boolean | optional | False | | project_sdk | The project SDK that is being targeted. See https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview | String | optional | "default" | | resources | A list of files to embed in the DLL as resources. | List of labels | optional | [] | +| run_analyzers | Controls whether analyzers run at build time. | Boolean | optional | True | | runtime_identifier | The runtime identifier that is being targeted. See https://docs.microsoft.com/en-us/dotnet/core/rid-catalog | String | required | | | srcs | The source files used in the compilation. | List of labels | optional | [] | | strict_deps | Whether to use strict dependencies or not.

This attribute mirrors the DisableTransitiveProjectReferences in MSBuild. The default setting of this attribute can be overridden in the toolchain configuration | Boolean | optional | True | diff --git a/docs/csharp_test.md b/docs/csharp_test.md index 596cbad2..68328f88 100644 --- a/docs/csharp_test.md +++ b/docs/csharp_test.md @@ -16,9 +16,9 @@ csharp_test(name, defines, deps, generate_documentation_file, internals_visible_to, keyfile, langversion, nullable, out, override_strict_deps, override_treat_warnings_as_errors, override_warning_level, override_warnings_as_errors, override_warnings_not_as_errors, - project_sdk, resources, runtime_identifier, srcs, strict_deps, target_frameworks, - targeting_packs, treat_warnings_as_errors, warning_level, warnings_as_errors, - warnings_not_as_errors, winexe) + project_sdk, resources, run_analyzers, runtime_identifier, srcs, strict_deps, + target_frameworks, targeting_packs, treat_warnings_as_errors, warning_level, + warnings_as_errors, warnings_not_as_errors, winexe) Compiles a C# executable and runs it as a test @@ -49,6 +49,7 @@ Compiles a C# executable and runs it as a test | override_warnings_not_as_errors | Whether or not to override the warnings_not_as_errors attribute. | Boolean | optional | False | | project_sdk | The project SDK that is being targeted. See https://learn.microsoft.com/en-us/dotnet/core/project-sdk/overview | String | optional | "default" | | resources | A list of files to embed in the DLL as resources. | List of labels | optional | [] | +| run_analyzers | Controls whether analyzers run at build time. | Boolean | optional | True | | runtime_identifier | The runtime identifier that is being targeted. See https://docs.microsoft.com/en-us/dotnet/core/rid-catalog | String | required | | | srcs | The source files used in the compilation. | List of labels | optional | [] | | strict_deps | Whether to use strict dependencies or not.

This attribute mirrors the DisableTransitiveProjectReferences in MSBuild. The default setting of this attribute can be overridden in the toolchain configuration | Boolean | optional | True | diff --git a/dotnet/private/rules/common/attrs.bzl b/dotnet/private/rules/common/attrs.bzl index c36689dd..1c5f057e 100644 --- a/dotnet/private/rules/common/attrs.bzl +++ b/dotnet/private/rules/common/attrs.bzl @@ -233,6 +233,11 @@ CSHARP_COMMON_ATTRS = dicts.add( default = "disable", values = ["disable", "enable", "warnings", "annotations"], ), + "run_analyzers": attr.bool( + doc = "Controls whether analyzers run at build time.", + mandatory = False, + default = True, + ), }, ) diff --git a/dotnet/private/rules/csharp/actions/csharp_assembly.bzl b/dotnet/private/rules/csharp/actions/csharp_assembly.bzl index 724bd684..b39ee0ee 100644 --- a/dotnet/private/rules/csharp/actions/csharp_assembly.bzl +++ b/dotnet/private/rules/csharp/actions/csharp_assembly.bzl @@ -81,7 +81,8 @@ def AssemblyAction( warning_level, project_sdk, allow_unsafe_blocks, - nullable): + nullable, + run_analyzers): """Creates an action that runs the CSharp compiler with the specified inputs. This macro aims to match the [C# compiler](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/listed-alphabetically), with the inputs mapping to compiler options. @@ -117,6 +118,7 @@ def AssemblyAction( project_sdk: The project sdk being targeted allow_unsafe_blocks: Compiles the target with /unsafe nullable: Enable nullable context, or nullable warnings. + run_analyzers: Enable analyzers. Returns: The compiled csharp artifacts. """ @@ -176,6 +178,7 @@ def AssemblyAction( warning_level, allow_unsafe_blocks, nullable, + run_analyzers, out_dll = out_dll, out_ref = out_ref, out_pdb = out_pdb, @@ -220,6 +223,7 @@ def AssemblyAction( warning_level, allow_unsafe_blocks, nullable, + run_analyzers, out_ref = out_iref, out_dll = out_dll, out_pdb = out_pdb, @@ -253,6 +257,7 @@ def AssemblyAction( warning_level, allow_unsafe_blocks, nullable, + run_analyzers, out_dll = None, out_ref = out_ref, out_pdb = None, @@ -311,6 +316,7 @@ def _compile( warning_level, allow_unsafe_blocks, nullable, + run_analyzers, out_dll = None, out_ref = None, out_pdb = None, @@ -389,8 +395,9 @@ def _compile( format_ref_arg(args, depset(framework_files, transitive = [refs])) # analyzers - args.add_all(analyzer_assemblies, format_each = "/analyzer:%s") - args.add_all(additionalfiles, format_each = "/additionalfile:%s") + if run_analyzers: + args.add_all(analyzer_assemblies, format_each = "/analyzer:%s") + args.add_all(additionalfiles, format_each = "/additionalfile:%s") # .cs files args.add_all(srcs) diff --git a/dotnet/private/rules/csharp/binary.bzl b/dotnet/private/rules/csharp/binary.bzl index 0881bde1..f1b48ed6 100644 --- a/dotnet/private/rules/csharp/binary.bzl +++ b/dotnet/private/rules/csharp/binary.bzl @@ -46,6 +46,7 @@ def _compile_action(ctx, tfm): project_sdk = ctx.attr.project_sdk, allow_unsafe_blocks = ctx.attr.allow_unsafe_blocks, nullable = ctx.attr.nullable, + run_analyzers = ctx.attr.run_analyzers, ) def _binary_private_impl(ctx): diff --git a/dotnet/private/rules/csharp/library.bzl b/dotnet/private/rules/csharp/library.bzl index db4384d6..a1e6aba4 100644 --- a/dotnet/private/rules/csharp/library.bzl +++ b/dotnet/private/rules/csharp/library.bzl @@ -45,6 +45,7 @@ def _compile_action(ctx, tfm): project_sdk = ctx.attr.project_sdk, allow_unsafe_blocks = ctx.attr.allow_unsafe_blocks, nullable = ctx.attr.nullable, + run_analyzers = ctx.attr.run_analyzers, ) def _library_impl(ctx): diff --git a/dotnet/private/rules/csharp/test.bzl b/dotnet/private/rules/csharp/test.bzl index e02df0a0..9cb53c5f 100644 --- a/dotnet/private/rules/csharp/test.bzl +++ b/dotnet/private/rules/csharp/test.bzl @@ -48,6 +48,7 @@ def _compile_action(ctx, tfm): project_sdk = ctx.attr.project_sdk, allow_unsafe_blocks = ctx.attr.allow_unsafe_blocks, nullable = ctx.attr.nullable, + run_analyzers = ctx.attr.run_analyzers, ) def _csharp_test_impl(ctx):