Skip to content

Ensure IsExternalInit is type forwarded on NET builds #619

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NET
using ModelContextProtocol;

namespace System.Collections.Generic;
Expand All @@ -18,4 +19,5 @@ public static TValue GetValueOrDefault<TKey, TValue>(this IReadOnlyDictionary<TK

public static Dictionary<TKey, TValue> ToDictionary<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> source) =>
source.ToDictionary(kv => kv.Key, kv => kv.Value);
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down Expand Up @@ -162,3 +163,4 @@ internal enum DynamicallyAccessedMemberTypes
/// </summary>
All = ~None
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down Expand Up @@ -48,3 +49,4 @@ public DynamicallyAccessedMembersAttribute(DynamicallyAccessedMemberTypes member
/// </summary>
public DynamicallyAccessedMemberTypes MemberTypes { get; }
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Diagnostics.CodeAnalysis
{
/// <summary>Specifies that null is allowed as an input even if the corresponding type disallows it.</summary>
Expand Down Expand Up @@ -137,3 +138,4 @@ public MemberNotNullWhenAttribute(bool returnValue, params string[] members)
public string[] Members { get; }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down Expand Up @@ -36,3 +37,4 @@ public RequiresDynamicCodeAttribute(string message)
/// </summary>
public string? Url { get; set; }
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down Expand Up @@ -37,3 +38,4 @@ public RequiresUnreferencedCodeAttribute(string message)
/// </summary>
public string? Url { get; set; }
}
#endif
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if !NET
namespace System.Diagnostics.CodeAnalysis
{
[AttributeUsage(AttributeTargets.Constructor, AllowMultiple = false, Inherited = false)]
internal sealed class SetsRequiredMembersAttribute : Attribute;
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Diagnostics.CodeAnalysis;

/// <summary>Specifies the syntax used in a string.</summary>
Expand Down Expand Up @@ -65,4 +66,5 @@ public StringSyntaxAttribute(string syntax, params object?[] arguments)

/// <summary>The syntax identifier for strings containing XML.</summary>
public const string Xml = nameof(Xml);
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Diagnostics.CodeAnalysis;

/// <summary>
Expand Down Expand Up @@ -81,4 +82,5 @@ public UnconditionalSuppressMessageAttribute(string category, string checkId)
/// Gets or sets the justification for suppressing the code analysis message.
/// </summary>
public string? Justification { get; set; }
}
}
#endif
4 changes: 3 additions & 1 deletion src/Common/Polyfills/System/IO/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Runtime.InteropServices;
using System.Text;

#if !NET
namespace System.IO;

internal static class StreamExtensions
Expand Down Expand Up @@ -61,4 +62,5 @@ static async ValueTask<int> ReadAsyncCore(Stream stream, Memory<byte> buffer, Ca
}
}
}
}
}
#endif
4 changes: 3 additions & 1 deletion src/Common/Polyfills/System/IO/TextWriterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NET
namespace System.IO;

internal static class TextWriterExtensions
Expand All @@ -7,4 +8,5 @@ public static async Task FlushAsync(this TextWriter writer, CancellationToken ca
cancellationToken.ThrowIfCancellationRequested();
await writer.FlushAsync();
}
}
}
#endif
4 changes: 3 additions & 1 deletion src/Common/Polyfills/System/Net/Http/HttpClientExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NET
using ModelContextProtocol;

namespace System.Net.Http;
Expand All @@ -19,4 +20,5 @@ public static async Task<string> ReadAsStringAsync(this HttpContent content, Can
cancellationToken.ThrowIfCancellationRequested();
return await content.ReadAsStringAsync();
}
}
}
#endif
4 changes: 3 additions & 1 deletion src/Common/Polyfills/System/PasteArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// https://github.com/dotnet/runtime/blob/d2650b6ae7023a2d9d2c74c56116f1f18472ab04/src/libraries/System.Private.CoreLib/src/System/PasteArguments.cs
// and changed from using ValueStringBuilder to StringBuilder.

#if !NET
using System.Text;

namespace System;
Expand Down Expand Up @@ -98,4 +99,5 @@ private static bool ContainsNoWhitespaceOrQuotes(string s)

private const char Quote = '\"';
private const char Backslash = '\\';
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Runtime.CompilerServices;

[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
Expand All @@ -13,3 +14,4 @@ public CallerArgumentExpressionAttribute(string parameterName)

public string ParameterName { get; }
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
namespace System.Runtime.CompilerServices
{
/// <summary>
Expand Down Expand Up @@ -30,3 +31,4 @@ public CompilerFeatureRequiredAttribute(string featureName)
public const string RequiredMembers = nameof(RequiredMembers);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// https://github.com/dotnet/runtime/blob/dd75c45c123055baacd7aa4418f425f412797a29/src/libraries/System.Private.CoreLib/src/System/Runtime/CompilerServices/DefaultInterpolatedStringHandler.cs
// and then modified to build on netstandard2.0.

#if !NET
using System.Buffers;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -614,4 +615,5 @@ private static uint Clamp(uint value, uint min, uint max)
return value;
}
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
using System.ComponentModel;

namespace System.Runtime.CompilerServices
Expand All @@ -12,3 +13,8 @@ namespace System.Runtime.CompilerServices
[EditorBrowsable(EditorBrowsableState.Never)]
internal static class IsExternalInit;
}
#else
// The compiler emits a reference to the internal copy of this type in the non-.NET builds,
// so we must include a forward to be compatible.
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.CompilerServices.IsExternalInit))]
#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#if !NET
using System.ComponentModel;

namespace System.Runtime.CompilerServices
Expand All @@ -10,3 +11,4 @@ namespace System.Runtime.CompilerServices
[EditorBrowsable(EditorBrowsableState.Never)]
internal sealed class RequiredMemberAttribute : Attribute;
}
#endif
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NET
using ModelContextProtocol;

namespace System.Threading.Tasks;
Expand All @@ -11,4 +12,5 @@ public static Task CancelAsync(this CancellationTokenSource cancellationTokenSou
cancellationTokenSource.Cancel();
return Task.CompletedTask;
}
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NET
using System.Runtime.CompilerServices;

namespace System.Threading.Channels;
Expand All @@ -14,4 +15,5 @@ public static async IAsyncEnumerable<T> ReadAllAsync<T>(this ChannelReader<T> re
}
}
}
}
}
#endif
4 changes: 3 additions & 1 deletion src/Common/Polyfills/System/Threading/ForceYielding.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NET
using System.Runtime.CompilerServices;

namespace System.Threading;
Expand All @@ -14,4 +15,5 @@ namespace System.Threading;
public void OnCompleted(Action continuation) => ThreadPool.QueueUserWorkItem(a => ((Action)a!)(), continuation);
public void UnsafeOnCompleted(Action continuation) => ThreadPool.UnsafeQueueUserWorkItem(a => ((Action)a!)(), continuation);
public void GetResult() { }
}
}
#endif
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#if !NET
using ModelContextProtocol;

namespace System.Threading.Tasks;
Expand Down Expand Up @@ -49,4 +50,5 @@ public static async Task WaitAsync(this Task task, TimeSpan timeout, Cancellatio

await task.ConfigureAwait(false);
}
}
}
#endif
4 changes: 4 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<PackageReference Include="Microsoft.SourceLink.GitHub" PrivateAssets="All" />
</ItemGroup>

<ItemGroup>
<Compile Include="..\Common\Polyfills\**\*.cs" />
</ItemGroup>

<ItemGroup>
<None Include="$(RepoRoot)\logo.png" Pack="true" PackagePath="\" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

<!-- Dependencies only needed by netstandard2.0 -->
<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="..\Common\Polyfills\**\*.cs" />
<Compile Include="..\Common\CancellableStreamReader\**\*.cs" />
<PackageReference Include="Microsoft.Bcl.Memory" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
Expand Down
4 changes: 0 additions & 4 deletions src/ModelContextProtocol/ModelContextProtocol.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@
<Compile Include="..\Common\Throw.cs" Link="Throw.cs" />
</ItemGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'netstandard2.0'">
<Compile Include="..\Common\Polyfills\**\*.cs" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ModelContextProtocol.Core\ModelContextProtocol.Core.csproj" />
</ItemGroup>
Expand Down
Loading