Skip to content

Commit

Permalink
Avoid S.R.I.RuntimeInformation package dependency (#85642)
Browse files Browse the repository at this point in the history
Contributes to #85641

System.Runtime.InteropServices.RuntimeInformation/4.3.0 is being
referenced in a few .NET Framework builds. The reference to that package
is undesirable as it brings in the entire netstandard1.x dependency
graph.

Instead, use System.Environment.OSVersion which returns "Microsoft
Windows NT" on .NET Framework, Mono and .NETCoreApp runtimes.
  • Loading branch information
ViktorHofer authored May 3, 2023
1 parent 0b04dfd commit 32a16d0
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 11 deletions.
1 change: 0 additions & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@
<SystemIOFileSystemAccessControlVersion>5.0.0</SystemIOFileSystemAccessControlVersion>
<SystemMemoryVersion>4.5.5</SystemMemoryVersion>
<SystemReflectionMetadataVersion>6.0.1</SystemReflectionMetadataVersion>
<SystemRuntimeInteropServicesRuntimeInformationVersion>4.3.0</SystemRuntimeInteropServicesRuntimeInformationVersion>
<SystemSecurityAccessControlVersion>6.0.0</SystemSecurityAccessControlVersion>
<SystemSecurityCryptographyAlgorithmsVersion>4.3.1</SystemSecurityCryptographyAlgorithmsVersion>
<SystemSecurityCryptographyCngVersion>5.0.0</SystemSecurityCryptographyCngVersion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System;
#if !NETFRAMEWORK
using System.Runtime.InteropServices;
#endif

namespace Microsoft.Extensions.DependencyModel
{
Expand All @@ -14,6 +16,11 @@ internal sealed class EnvironmentWrapper : IEnvironment

public object? GetAppContextData(string name) => AppDomain.CurrentDomain.GetData(name);

public bool IsWindows() => RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
public bool IsWindows() =>
#if NETFRAMEWORK
Environment.OSVersion.Platform == PlatformID.Win32NT;
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ By default, the dependency manifest contains information about the application's

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<Reference Include="System.Runtime" />
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="$(SystemRuntimeInteropServicesRuntimeInformationVersion)" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NETFRAMEWORK
using System.Runtime.InteropServices;
#endif

namespace Microsoft.Extensions.DependencyModel.Resolution
{
Expand All @@ -27,12 +29,21 @@ public class DotNetReferenceAssembliesPathResolver

private static string? GetDefaultDotNetReferenceAssembliesPath(IFileSystem fileSystem)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (
#if NETFRAMEWORK
System.Environment.OSVersion.Platform == System.PlatformID.Win32NT
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
return null;
}

if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
if (
#if !NETFRAMEWORK
RuntimeInformation.IsOSPlatform(OSPlatform.OSX) &&
#endif
fileSystem.Directory.Exists("/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/xbuild-frameworks"))
{
return "/Library/Frameworks/Mono.framework/Versions/Current/lib/mono/xbuild-frameworks";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ public static class WindowsServiceHelpers
[SupportedOSPlatformGuard("windows")]
public static bool IsWindowsService()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (
#if NETFRAMEWORK
Environment.OSVersion.Platform != PlatformID.Win32NT
#else
!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,15 @@ public static IServiceCollection AddWindowsService(this IServiceCollection servi

private static void AddWindowsServiceLifetime(IServiceCollection services, Action<WindowsServiceLifetimeOptions> configure)
{
#if !NETFRAMEWORK
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
#endif

services.AddLogging(logging =>
{
#if !NETFRAMEWORK
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
#endif
logging.AddEventLog();
});
services.AddSingleton<IHostLifetime, WindowsServiceLifetime>();
Expand All @@ -129,7 +133,9 @@ public EventLogSettingsSetup(IHostEnvironment environment)

public void Configure(EventLogSettings settings)
{
#if !NETFRAMEWORK
Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows));
#endif

if (string.IsNullOrEmpty(settings.SourceName))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,13 @@ internal static void SetDefaultContentRoot(IConfigurationBuilder hostConfigBuild
// any trailing directory separator characters. I'm not even sure the casing can ever be different from these APIs, but I think it makes sense to
// ignore case for Windows path comparisons given the file system is usually (always?) going to be case insensitive for the system path.
string cwd = Environment.CurrentDirectory;
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) || !string.Equals(cwd, Environment.SystemDirectory, StringComparison.OrdinalIgnoreCase))
if (
#if NETFRAMEWORK
Environment.OSVersion.Platform != PlatformID.Win32NT ||
#else
!RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ||
#endif
!string.Equals(cwd, Environment.SystemDirectory, StringComparison.OrdinalIgnoreCase))
{
hostConfigBuilder.AddInMemoryCollection(new[]
{
Expand Down Expand Up @@ -270,6 +276,8 @@ internal static void AddDefaultServices(HostBuilderContext hostingContext, IServ
bool isWindows =
#if NETCOREAPP
OperatingSystem.IsWindows();
#elif NETFRAMEWORK
Environment.OSVersion.Platform == PlatformID.Win32NT;
#else
RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="$(SystemRuntimeInteropServicesRuntimeInformationVersion)" />
<Reference Include="System.Runtime" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ public ConsoleLoggerProvider(IOptionsMonitor<ConsoleLoggerOptions> options, IEnu
[UnsupportedOSPlatformGuard("windows")]
private static bool DoesConsoleSupportAnsi()
{
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
if (
#if NETFRAMEWORK
Environment.OSVersion.Platform != PlatformID.Win32NT
#else
!RuntimeInformation.IsOSPlatform(OSPlatform.Windows)
#endif
)
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,6 @@
</ItemGroup>

<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
<PackageReference Include="System.Runtime.InteropServices.RuntimeInformation" Version="$(SystemRuntimeInteropServicesRuntimeInformationVersion)" />
<PackageReference Include="System.ValueTuple" Version="$(SystemValueTupleVersion)" />
<Reference Include="System.Runtime" />
</ItemGroup>

Expand Down

0 comments on commit 32a16d0

Please # to comment.