core icon indicating copy to clipboard operation
core copied to clipboard

System.Collection "Invalid Typeref token" PublishReadyToRun

Open DualBrain opened this issue 7 years ago • 10 comments

When attempting to utilize PublishReadyToRun on a VB console project that I have, I get:

C:\Program Files\dotnet\sdk\3.1.100-preview1-014459\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(306,5): error : Error compiling obj\release\netcoreapp3.0\win-x64\linked\System.Collections.dll: [D:\GitHub\VbRogue\Rogue\Rogue\obj\release\netcoreapp3.0\win-x64\linked\System.Collections.dll] Invalid Typeref token. [D:\GitHub\VbRogue\Rogue\Rogue\Rogue.vbproj]
C:\Program Files\dotnet\sdk\3.1.100-preview1-014459\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(306,5): error : Error: compilation failed for "obj\release\netcoreapp3.0\win-x64\linked\System.Collections.dll" (0x8007000b) [D:\GitHub\VbRogue\Rogue\Rogue\Rogue.vbproj]
C:\Program Files\dotnet\sdk\3.1.100-preview1-014459\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.Publish.targets(219,5): error NETSDK1096: Optimizing assemblies for performance failed. You can either exclude the failing assemblies from being optimized, or set the PublishReadyToRun property to false. [D:\GitHub\VbRogue\Rogue\Rogue\Rogue.vbproj]

If I add the following to the vbproj file:

  <ItemGroup>
    <PublishReadyToRunExclude Include="System.Collections.dll" />
  </ItemGroup>

The problem is resolved... but my question is... Why wouldn't System.Collection be something that wouldn't "just work"? Am I missing something here?

Using the command line:

dotnet publish -r win-x64 -c release

Although this has been happening for several version... the current version of .NET Core I'm using is:

3.1.100-preview1-014459

The project that I'm using for this can be found at:

https://github.com/DualBrain/VbRogue/tree/master/Rogue

If this isn't the right place to ask / report this, please feel free to direct me in the correct direction.

Thanks.

DualBrain avatar Nov 06 '19 20:11 DualBrain

@dagood

scalablecory avatar Nov 07 '19 01:11 scalablecory

I'm not really familiar with these publish options. /cc @nguerrera

Have you tried this with 3.1.100-preview2-014569 (preview1 is old) and 3.0 to compare the behavior?

I tried a minimal repro looking at your project, but publish went fine with this, with 3.1.100-preview2-014569:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.0</TargetFramework>
    <PublishTrimmed>true</PublishTrimmed>
    <PublishReadyToRun>true</PublishReadyToRun>
    <PublishSingleFile>true</PublishSingleFile>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <DebugType>None</DebugType>
  </PropertyGroup>
</Project>
Imports System
Module Program
    Sub Main(args As String())
        Dim useCollection = New HashSet(Of String) ' Prevent trimming System.Collections.
        Console.WriteLine("Hello World!")
    End Sub
End Module

dagood avatar Nov 07 '19 17:11 dagood

cc @fadimounir

nguerrera avatar Nov 07 '19 18:11 nguerrera

@dagood Not sure what could be different; however, I did just try it with the latest preview with the same results as originally described.

However, using the HashSet line as you did in the actual project does work... but this still seems to be a work around for the underlying issue (whatever that is).

Thanks.

DualBrain avatar Nov 07 '19 19:11 DualBrain

Oh, that was actually my attempt to make it more likely to repro, because PublishTrimmed removes System.Collections.dll if it's not used. Since the error involves System.Collections.dll I wanted to make sure it is included, and referencing a type in that assembly was a simple way of doing that.

But... I noticed it looks like the only System.Collections type you normally use in your project is Stack, so I changed my code to try to use that type to prevent trimming:

Imports System
Module Program
    Sub Main(args As String())
        Dim useCollection = New Stack(Of String) ' Prevent trimming.
        Console.WriteLine("Hello World!")
    End Sub
End Module

That causes a repro on my machine, with 3.1.100-preview2-014569:

  • error : Error compiling obj\release\netcoreapp3.0\win-x64\linked\System.Collections.dll: [D:\temp\vbconsole\obj\release\netcoreapp3.0\win-x64\linked\System.Collections.dll] Invalid Typeref token. [D:\temp\vbconsole\vbconsole.vbproj]
  • error : Error: compilation failed for "obj\release\netcoreapp3.0\win-x64\linked\System.Collections.dll" (0x8007000b) [D:\temp\vbconsole\vbconsole.vbproj]
  • error NETSDK1096: Optimizing assemblies for performance failed. You can either exclude the failing assemblies from being optimized, or set the PublishReadyToRun property to false. [D:\temp\vbconsole\vbconsole.vbproj]

So it seems that something goes wrong if you only use Stack? Strange!

This also repros in a csproj with the same project content and analogous code:

using System;
using System.Collections.Generic;
namespace csconsole
{
    class Program
    {
        static void Main(string[] args)
        {
            var s = new Stack<string>();
            Console.WriteLine("Hello World!");
        }
    }
}

dagood avatar Nov 07 '19 19:11 dagood

Tagging @KathleenDollard since I mentioned this to her in a passing conversation. (I also want to share that even with having to add the exclusion - this stuff is AWESOME!!!!! Keep it up!)

DualBrain avatar Nov 07 '19 20:11 DualBrain

cc @marek-safar @sbomer

This is a bug with the IL trimmer. The produced assemblies contain invalid tokens (not just with System.Collections.dll)

There are 2 bugs here: first, with regards to the invalid tokens, a pretty simple diagnosis can be found by looking at the metadata of the linked Rogue.dll (ildasm output):

TypeRef #51 (01000033)
-------------------------------------------------------
Token:             0x01000033
ResolutionScope:   0x23000006
TypeRefName:       System.Drawing.Color

The scope here is assemblyref token 0x23000006, but take a look at the ref declaration:

.assembly extern /*23000006*/ System.IO.FileSystem
{
  .publickeytoken = (B0 3F 5F 7F 11 D5 0A 3A )                         // .?_....:
  .ver 4:1:1:0
}

The ref is wrong here. This type should be in the System.Drawing.dll, forwarded to System.Drawing.Primitives.dll (which is missing in the assembly refs), and not in System.IO.FileSystem.dll.

Second, the failure in crossgen when compiling System.Collections.dll is a bug that I will take a look at and fix. This should be a warning, not an error, and again, this is caused by the invalid tokens emitted in the trimmed assemblies.

This is related to https://github.com/mono/linker/issues/628. @sbomer can provide more guidance here.

fadimounir avatar Nov 07 '19 20:11 fadimounir

I'm not following the issue here, but this seems to have gone quiet for a while. But I'm seeing a similar issue with System.Security.Principal.Windows.dll, too. I'm using 3.1.101. Is this something that needs to be fixed with the Trim feature, or the dlls being trimmed (System.Security.Principal.Windows.dll in this case)?

richbern avatar Mar 03 '20 18:03 richbern

This reproes for me as well - repro: https://github.com/asklar/clipview/tree/pinvoke

asklar avatar Apr 02 '20 19:04 asklar

The issue is stale and was reported for 3.0 which is out of support. Closing, but if the problem persists in 6.0 or 7.0, please open a new issue.

carlossanlop avatar Jan 30 '23 22:01 carlossanlop