Skip to content

Developers need more flexibility to annotate their code for platform supported/unsupported scenarios to reduce issues warnings noise #44922

Closed
@jeffhandley

Description

@jeffhandley

The Platform Compatibility Analyzer was introduced in .NET 5.0, raising new diagnostics when APIs are referenced that are unsupported on targeted platforms. The analyzer relies on the [SupportedOSPlatform] and [UnsupportedOSPlatform] attributes, which can be applied to assemblies, types, and members. These annotations only provide a mechanism for marking an API as entirely supported/unsupported--there is currently no way to annotate an API as partially or conditionally supported on a platform.

Conditionally-Supported Platform Annotations

With a mechanism for marking an API as being conditionally supported/unsupported on a platform, we could improve annotations for APIs such as MemoryMappedFile.CreateNew(), where calls with a named memory mapped file (that is, a non-null mapName) are supported on Windows operating systems only.

This could involve creating new attributes such as [SupportedOSPlatformIfNull], [SupportedOSPlatformIfNotNull], [UnsupportedOSPlatformIfNull], and [UnsupportedOSPlatformIfNotNull]. Occurrences of conditional platform support should be researched to determine what types of conditions need to be modeled in such attributes.

Platform-Guard Assertion Annotations

The analyzer already recognizes platform guards using the methods on OperatingSystem, such as OperatingSystem.IsWindows and OperatingSystem.IsWindowsVersionAtLeast. However, the analyzer does not recognize other guard methods or even helper methods that assert platform guards. Expanding this support could involve creating new attributes that indicate that an API asserts platform checks the same way the APIs on OperatingSystem do, potentially even conditionally.

One example is in Thread, there is an internal field for IsThreadStartSupported that indicates whether or not the current platform supports calls to Thread.Start, regardless of which platforms are targeted. Annotations could allow this to be modeled for the analyzer to warn if Threat.Start is called without first checking the corresponding IsThreadStartSupported member.

Additionally, it's expected that projects will commonly create helper methods that wrap around the OperatingSystem calls and these are not currently recognized through the flow analysis in the analyzer. Helper methods that perform platform assertion could be annotated to inform the analyzer that invocation or specific return values indicate platform support.

  • Conditionally-Supported Platform Annotations removed from scope
  • Platform-Guard Assertion Annotations

Metadata

Metadata

Labels

Cost:MWork that requires one engineer up to 2 weeksPriority:2Work that is important, but not critical for the releaseTeam:LibrariesUser StoryA single user-facing feature. Can be grouped under an epic.area-Meta

Type

No type

Projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions