Skip to content

Recognize IsApplePlatform through OS guard attributes - #56576

Open
makazeu wants to merge 8 commits into
dotnet:mainfrom
makazeu:u/makazeu/isappleplatform-analyzer-support
Open

makazeu wants to merge 8 commits into
dotnet:mainfrom
makazeu:u/makazeu/isappleplatform-analyzer-support

Conversation

@makazeu

@makazeu makazeu commented Oct 10, 2026 •

Copy link
Copy Markdown

Summary

Teach CA1416 to recognize OperatingSystem.IsApplePlatform() using the API's SupportedOSPlatformGuard attributes, supporting the public API added in dotnet/runtime#135475.

  • No hardcoded platform list: reuse the existing attribute-driven guard analysis instead of interpreting IsApplePlatform as a single platform name.
  • Preserve existing behavior: bypass dependency inference for the aggregate guard without changing the iOS/MacCatalyst relationship.
  • Test the real API: C# and VB API-usage tests use the reference pack resolved by the test project's build. Its TFM and version are passed through runtime configuration, so these tests follow repository dependency updates without a hardcoded package version. NuGet configuration is copied to build/publish output and explicitly supplied to the resolver; other tests retain their existing reference baselines.

Coverage

The Apple guard tests cover positive and negated branches, Windows negative controls, and the iOS/MacCatalyst relationship. Tests that deliberately vary guard annotations retain a source stub to verify that analysis follows metadata rather than a fixed Apple platform list.

Validation

  • Built the NetAnalyzers solution and passed the targeted IsApplePlatform tests using the build-resolved Microsoft.NETCore.App.Ref 12.0.0-alpha.1.26564.101, brought into SDK main by dotnet/sdk#57118.

Prepared with GitHub Copilot.

Decode OperatingSystem.IsApplePlatform as macOS, MacCatalyst, iOS, or tvOS instead of a fictitious ApplePlatform OS. Preserve existing builtin guard registration and flow-analysis negation.

Add C# and VB regressions for allow lists, deny lists, each Apple platform, Windows, watchOS, and negation. Document builtin guard test setup.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: ee6e48ad-6898-45d7-98ed-9850afb3bd30
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Restore AGENTS.md to the original baseline as requested, preserving IsApplePlatform analyzer changes and regression tests.

Co-authored-by: Copilot App <[email protected]>

Copilot-Session: ee6e48ad-6898-45d7-98ed-9850afb3bd30
@jkotas

jkotas commented Nov 4, 2026

Copy link
Copy Markdown
Member

The existing test helpers use .NET 6 reference assemblies,

Should the tests for this API use .NET 12 reference assemblies (once the runtime change is merged) so that they test the actual user experience?

Route IsApplePlatform through the existing guard attribute decoder instead of inferring a platform from its name or hardcoding the Apple platform set. Preserve iOS/MacCatalyst relationships and cover metadata-driven guards in regression tests.

Co-authored-by: Copilot App <[email protected]>
@makazeu makazeu changed the title Recognize IsApplePlatform as a four-platform OS guard Recognize IsApplePlatform through OS guard attributes Nov 4, 2026
@makazeu

makazeu commented Nov 5, 2026

Copy link
Copy Markdown
Author

The existing test helpers use .NET 6 reference assemblies,

Should the tests for this API use .NET 12 reference assemblies (once the runtime change is merged) so that they test the actual user experience?

Once dotnet/runtime#135475 is merged and a reference pack containing the API is available, I'll switch the C# and VB API-usage tests to .NET 12 reference assemblies and remove their OperatingSystem stubs, so they exercise the real API and its guard annotations. Only the tests exercising the real API will move to .NET 12 reference assemblies; unrelated tests will continue using their existing references.

@makazeu

makazeu commented Nov 7, 2026

Copy link
Copy Markdown
Author

Should the tests for this API use .NET 12 reference assemblies (once the runtime change is merged) so that they test the actual user experience?

The C# and VB API-usage tests now use real reference assemblies instead of OperatingSystem stubs. They use the reference pack resolved by the test build, so they follow the SDK dependency updates automatically; unrelated tests keep their existing references.

I'm waiting for the SDK dependency flow to bring in Microsoft.NETCore.App.Ref version 12.0.0-alpha.1.26556.115 or a newer build containing the API. Once that lands, I'll mark this PR ready for review.

@makazeu
makazeu marked this pull request as ready for review November 19, 2026 16:04
@makazeu
makazeu requested review from a team, lbussell and mthalman as code owners November 19, 2026 16:04
Copilot AI balanced review requested due to automatic review settings November 19, 2026 16:04

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the Apple-platform guard analysis changes.

No open issues 🎉

What changed in this PR

Updates CA1416 platform compatibility analysis to recognize OperatingSystem.IsApplePlatform() through its guard attributes, rather than treating it as a single platform name.

Changes:

  • Routes the aggregate Apple guard through existing attribute-based analysis while preserving iOS/MacCatalyst dependency handling.
  • Adds C# and Visual Basic coverage for positive and negated guards, Windows controls, and metadata-driven behavior.
  • Enables selected tests to use the build-resolved reference pack while retaining existing test baselines.
File Description
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​Microsoft.NetCore.Analyzers/​InteropServices/​PlatformCompatibilityAnalyzerTests.GuardedCallsTests.cs Adds Apple guard tests and current-reference-pack setup.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​Microsoft.NetCore.Analyzers/​InteropServices/​PlatformCompatibilityAnalyzerTests.cs Adds verification overloads accepting explicit reference assemblies.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​tests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests/​Microsoft.CodeAnalysis.NetAnalyzers.UnitTests.csproj Exposes reference-pack metadata through runtime configuration and copies NuGet configuration.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​src/​Microsoft.CodeAnalysis.NetAnalyzers/​Microsoft.NetCore.Analyzers/​InteropServices/​PlatformCompatibilityAnalyzer.cs Excludes IsApplePlatform from name-based platform inference.
src/​Microsoft.CodeAnalysis.NetAnalyzers/​AGENTS.md Documents the current-reference-pack test setup and incremental-build requirements.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants