Fix OSConditionAttribute to detect actual OS instead of assuming .NET Framework is Windows-only #6616
+135
−5
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The
OSConditionAttribute
incorrectly assumed that .NET Framework (NET462
) always runs on Windows. However, .NET Framework can run on Linux/Mac via Mono, causing the attribute to incorrectly include/exclude tests based on compilation target rather than the actual runtime operating system.Consider this scenario:
Before this fix:
Root Cause
The problematic code used conditional compilation that forced Windows detection for .NET Framework builds:
This assumption breaks when .NET Framework runs via Mono on non-Windows platforms.
Solution
Removed the conditional compilation and made the attribute consistently use
RuntimeInformation.IsOSPlatform()
to detect the actual operating system across all target frameworks:Changes Made
#if NET462
conditional compilation and unified OS detection logicBenefits
After this fix:
Fixes the issue where
OSConditionAttribute
would incorrectly run Windows-only tests when .NET Framework executes via Mono on non-Windows platforms.Warning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
tphvsblobprodcus375.vsblob.vsassets.io
dotnet build src/TestFramework/TestFramework/TestFramework.csproj -v m --no-restore
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.