Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 27, 2025

OSConditionAttribute assumed .NET Framework always means Windows, causing [OSCondition(OperatingSystems.Windows)] tests to run on Linux/Mac when using Mono.

Changes

  • Use RuntimeInformation.IsOSPlatform via reflection for NETFRAMEWORK builds

    • Loads RuntimeInformation from System.Runtime.InteropServices.RuntimeInformation assembly or mscorlib (Mono)
    • Falls back to assuming Windows if API unavailable (older .NET Framework without the API)
  • Cache detected OS at class load time to avoid repeated reflection overhead

  • Add exception handling for reflection invocations

// Before: Always assumed Windows for .NET Framework
#if NET462
    => (_operatingSystems & OperatingSystems.Windows) != 0;

// After: Detects actual OS via reflection, falls back to Windows if unavailable
#if NETFRAMEWORK
    {
        get
        {
            OperatingSystems currentOS = s_detectedOS ?? OperatingSystems.Windows;
            return (_operatingSystems & currentOS) != 0;
        }
    }

Fixes #6615

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:

  • 8xbvsblobprodcus382.vsblob.vsassets.io
    • Triggering command: /usr/bin/dotnet dotnet msbuild /home/REDACTED/work/testfx/testfx/src/TestFramework/TestFramework/TestFramework.csproj /t:PrintTargetFrameworks /p:TargetFramework=netstandard2.0 /nologo (dns block)
    • Triggering command: /usr/bin/dotnet dotnet msbuild /home/REDACTED/work/testfx/testfx/src/TestFramework/TestFramework/TestFramework.csproj /pp (dns block)
    • Triggering command: /home/REDACTED/work/testfx/testfx/.dotnet/dotnet /home/REDACTED/work/testfx/testfx/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/testfx/testfx/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/testfx/testfx/artifacts/toolset/11.0.0-beta.25575.1.txt t.IsInstanceOfType.cs (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>

<agent_instructions>Comment from jkotas should be followed carefully</agent_instructions>

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

@Youssef1313 @jkotas Is there a good way to detect the OS when compiling for .NET Framework and running with `mono`?

What we have today is:

#if NET462
// On .NET Framework, we are sure we are running on Windows.
=> (_operatingSystems & OperatingSystems.Windows) != 0;
</comment_new>


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits November 27, 2025 09:58
… .NET Framework

Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Co-authored-by: Evangelink <11340282+Evangelink@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix OSConditionAttribute to work for netfx on Linux Fix OSConditionAttribute to detect OS via reflection for .NET Framework on Mono Nov 27, 2025
Copilot finished work on behalf of Evangelink November 27, 2025 10:07
Copilot AI requested a review from Evangelink November 27, 2025 10:07
@Evangelink Evangelink closed this Nov 28, 2025
@Evangelink Evangelink reopened this Nov 28, 2025
#if NET462
// On .NET Framework, we are sure we are running on Windows.
=> (_operatingSystems & OperatingSystems.Windows) != 0;
#if NETFRAMEWORK
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#if NETFRAMEWORK
#if NET462

#endif
#endif

#if NETFRAMEWORK
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
#if NETFRAMEWORK
#if NET462


if (runtimeInformationType is null)
{
return null;
Copy link
Member

Choose a reason for hiding this comment

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

Instead of being nullable, should we just return OperatingSystems.Windows?

return null;
}

MethodInfo? isOSPlatformMethod = runtimeInformationType.GetMethod("IsOSPlatform", BindingFlags.Public | BindingFlags.Static);
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure of the declared IsOSPlatform accessibility in this case.

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

3 participants