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

PolySharp with InternalsVisibleTo produces CS0433 #103

Open
Bouke opened this issue Jul 15, 2024 · 2 comments
Open

PolySharp with InternalsVisibleTo produces CS0433 #103

Bouke opened this issue Jul 15, 2024 · 2 comments

Comments

@Bouke
Copy link

Bouke commented Jul 15, 2024

Description (optional)

Using PolySharp in a netstandard2.0 project that uses InternalsVisibleToAttribute to expose internals to another net80 project, will cause that other project to fail to compile. This is because the types generated by PolySharp have internal visibility and collide with the types net80 provides.

Reproduction Steps

I have a library project that targets netstandard2.0 and I need to consume it from both net48 and net80 targets:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>netstandard2.0</TargetFramework>
        <RootNamespace>Logging.Adapters</RootNamespace>
        <LangVersion>12</LangVersion>
    </PropertyGroup>
    <ItemGroup>
        <PackageReference Include="PolySharp" Version="1.14.1" />
    </ItemGroup>
</Project>

There are classes internal to the library that the test project needs to know about:

using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("Library.Test")]

There is a test project that multi-target net48 and net80. For simplicity we'll ignore the multi-targeting and only look at the net80 consumer:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <RootNamespace>Logging.Test</RootNamespace>
        <LangVersion>12</LangVersion>
        <PackageId>Library.Test</PackageId>
    </PropertyGroup>
    <ItemGroup>
      <ProjectReference Include="..\Library\Library.csproj" />
    </ItemGroup>
</Project>

Expected Behavior

Compiles.

Actual Behavior

System.Runtime.CompilerServices.IsExternalInit.g.cs(4,115): Error CS0433 : The type 'IsExternalInit' exists in both 'Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
System.Runtime.CompilerServices.RequiresLocationAttribute.g.cs(4,115): Error CS0433 : The type 'RequiresLocationAttribute' exists in both 'Library, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' and 'System.Runtime, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

System info

This section should contain useful info such as:

  • PolySharp NuGet version: 1.14.1
  • Operating system version: macOS 14.5
  • Visual Studio version, or .NET SDK/runtime version: 8.0.302

Additional context (optional)

N/A

@Bouke
Copy link
Author

Bouke commented Jul 15, 2024

After adding PrivateAssets="all" to the package reference, the repro is fixed: <PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />.

However that didn't solve the problem, but was in fact a result of me trying to produce a minimal repro. After adding net48 to the test project and adding PolySharp as a dependency, the problem re-appears:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFrameworks>net48;net8.0</TargetFrameworks>
        <RootNamespace>Logging.Test</RootNamespace>
        <LangVersion>12</LangVersion>
        <PackageId>Library.Test</PackageId>
    </PropertyGroup>
    <ItemGroup>
      <ProjectReference Include="..\Library\Library.csproj" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />
    </ItemGroup>
</Project>

This build error could then be fixed by adding a condition on the package reference: <PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" Condition="$(TargetFramework) != 'net8.0'" />.

Removing the multi-targetting, but keeping PolySharp as a dependency, the error can be reproduced like this:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <RootNamespace>Logging.Test</RootNamespace>
        <LangVersion>12</LangVersion>
        <PackageId>Library.Test</PackageId>
    </PropertyGroup>
    <ItemGroup>
      <ProjectReference Include="..\Library\Library.csproj" />
    </ItemGroup>
    <ItemGroup>
        <PackageReference Include="PolySharp" Version="1.14.1" PrivateAssets="all" />
    </ItemGroup>
</Project>

Is this something PolySharp would be able to support: consuming a library targeting netstandard2.0 which uses PolySharp, and then a project itself targeting net8.0 also using PolySharp?

@YoshiRulz
Copy link

Duplicate of #50?

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

No branches or pull requests

2 participants