Skip to content
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

Microsoft.NET.StringTools hijacks System.MemoryExtensions in .NET Framework <4.7.2 #8325

Closed
RussKie opened this issue Jan 20, 2023 · 2 comments
Labels
bug needs-triage Have yet to determine what bucket this goes in.

Comments

@RussKie
Copy link
Member

RussKie commented Jan 20, 2023

Issue Description

#5663 introduced public extensions methods:

namespace System
{
/// <summary>
/// A bare minimum and inefficient version of MemoryExtensions as provided in System.Memory on .NET 4.5.
/// </summary>
public static class MemoryExtensions
{
public static string AsSpan<T>(this T[] array, int start, int length)
{
if (array is char[] charArray)
{
return new string(charArray, start, length);
}
throw new ArgumentException(nameof(array));
}
}
}

This is a really bad idea... I have a project that is targeting net462 and I get this error because of this code:

error CS0433: The type 'MemoryExtensions' exists in both 'Microsoft.NET.StringTools.net35, Version=1.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'

The trouble is that we have control over the dependency tree:
image

...and the code that's failing is source generated.

Steps to Reproduce

  1. Run dotnet new console
  2. Update the project file as follows:
    <Project Sdk="Microsoft.NET.Sdk">
    
      <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net462</TargetFramework>
        <LangVersion>latest</LangVersion>
      </PropertyGroup>
      <ItemGroup>
        <PackageReference Include="MessagePack" Version="2.4.35" />
        <PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="2.2.0" />
        <PackageReference Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
      </ItemGroup>
    
    </Project>
  3. Add the following line to Program.cs:
    MemoryExtensions.AsSpan("bla");
@RussKie RussKie added bug needs-triage Have yet to determine what bucket this goes in. labels Jan 20, 2023
@KalleOlaviNiemitalo
Copy link

Duplicate of #8295?

@RussKie
Copy link
Member Author

RussKie commented Jan 20, 2023

Yes, it is. I should have searched, shouldn't have I? :)

@RussKie RussKie closed this as completed Jan 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug needs-triage Have yet to determine what bucket this goes in.
Projects
None yet
Development

No branches or pull requests

2 participants