Skip to content

Broken type references when using copyused mode #2138

@vitek-karas

Description

@vitek-karas

Repro is pretty simple:

New console app:

using System;
using System.Runtime.InteropServices;

namespace ReproCopyUsedBug
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(RuntimeInformation.FrameworkDescription);
            Console.WriteLine(RuntimeEnvironment.GetRuntimeDirectory());
        }
    }
}

Publish with:

dotnet publish -r win-x64 -p:PublishTrimmed=true -p:TrimMode=copyused

Try to run:

Unhandled exception.
   Cannot print exception string because Exception.ToString() failed.

The problem is in System.Runtime.InteropServices.RuntimeInformation.dll, the RuntimeInformation.get_FrameworkDescription calls CustomAttributeExtensions.GetCustomAttribute<T>(...). The type ref on that dll is pointing to System.Runtime.dll, which has a type forwarder for it which points to CoreLib.

When trimmed in copyused mode, we rewrite the typereference to point directly to CoreLib and remove the typeforwarder from System.Runtime.dll, but then when deciding how to output the System.Runtime.InteropServices.RuntimeInformation we mark it as "copy" and effectively throw away any changes (including the rewrite of the type ref).

Note that this works if you omit the call to RuntimeEnvironment since now we will remove System.Runtime.InteropServices.dll completely, which will force us to rewrite System.Runtime.InteropServices.RuntimeInformation as that has an assembly ref to it.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions