Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 29, 2025

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:

[TestMethod]
[OSCondition(OperatingSystems.Windows)]
public void WindowsOnlyTest()
{
    // This test should only run on Windows
}

Before this fix:

  • When running .NET Framework on Linux via Mono: Test runs ❌ (incorrect - assumes Windows)
  • Expected behavior: Test should be skipped ✅ (not actually Windows)

Root Cause

The problematic code used conditional compilation that forced Windows detection for .NET Framework builds:

public override bool ShouldRun
#if NET462
    // On .NET Framework, we are sure we are running on Windows.
    => (_operatingSystems & OperatingSystems.Windows) != 0;
#else
    // Use RuntimeInformation for other frameworks...
#endif

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:

public override bool ShouldRun
{
    get
    {
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            return (_operatingSystems & OperatingSystems.Windows) != 0;
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            return (_operatingSystems & OperatingSystems.Linux) != 0;
        }
        // ... other OS checks
    }
}

Changes Made

  • OSConditionAttribute.cs: Removed #if NET462 conditional compilation and unified OS detection logic
  • OSConditionAttributeTests.cs: Added comprehensive tests covering various OS scenarios and edge cases

Benefits

  • Mono compatibility: .NET Framework tests now work correctly when run via Mono on Linux/Mac
  • Cross-platform consistency: Same OS detection logic across all target frameworks
  • No breaking changes: Existing API and correct behavior preserved
  • Minimal scope: Only removed the problematic assumption, all other logic intact

After this fix:

  • When running .NET Framework on Linux via Mono: Test is skipped ✅ (correctly detects Linux)
  • All existing correct scenarios continue to work as expected

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
    • Triggering command: 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

This section details on the original issue you should resolve

<issue_title>OSConditionAttribute include windows doesn't work for netfx Linux</issue_title>
<issue_description>## Describe the bug

When running a test marked with [OSCondition(OperatingSystems.Windows)] with netfx on Linux or Mac, the test will be run via mono and the attribute won't exclude the test.

The attribute currently assumes netfx to be Windows only which is not always right.</issue_description>

Comments on the Issue (you are @copilot in this section)

Fixes #6615

💡 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.

Copilot AI and others added 2 commits September 29, 2025 12:21
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
… Framework is Windows-only

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] OSConditionAttribute include windows doesn't work for netfx Linux Fix OSConditionAttribute to detect actual OS instead of assuming .NET Framework is Windows-only Sep 29, 2025
Copilot finished work on behalf of Evangelink September 29, 2025 12:37
@Copilot Copilot AI requested a review from Evangelink September 29, 2025 12:37
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.

OSConditionAttribute include windows doesn't work for netfx Linux
2 participants