dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.72k stars 3.99k forks source link

Assembly references order causes different compiler output #5714

Open marek-safar opened 8 years ago

marek-safar commented 8 years ago

Class1.cs file is declared as following

public class Class1
{
    System.Diagnostics.Tracing.EventCommand x = System.Diagnostics.Tracing.EventCommand.SendManifest;
}

This compiles fine

csc /noconfig /nostdlib+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll" /reference:"PCL\System.Diagnostics.Tracing.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Facades\System.Diagnostics.Tracing.dll" Class1.cs /t:library

Reversing order of System.Diagnostics.Tracing.dll like

csc /noconfig /nostdlib+ /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\mscorlib.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Facades\System.Diagnostics.Tracing.dll" /reference:"PCL\System.Diagnostics.Tracing.dll" Class1.cs /t:library

produces compiler error

Class1.cs(3,29): error CS0433: The type 'EventCommand' exists in both 'System.Diagnostics.Tracing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' and 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'

PCL/System.Diagnostics.Tracing.dll can be found at https://gist.github.com/marek-safar/cfc7fd657c80581f76ab

gafter commented 8 years ago

Assigning to the 1.1 milestone for evaluation.

jaredpar commented 8 years ago

Thanks for the great repro!

Took a look and it appears the native compiler actually rejects both of these command lines with the following:

System.Diagnostics.Tracing.dll: error CS1703: An assembly with the same identity 'System.Diagnostics.Tracing,
        Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' has already been imported. Try
        removing one of the duplicate references.
c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5.2\Facades\System.Diagnostics.Tracing.dll: (Location of symbol related to previous error)

There were some rule changes to relax how we merge assemblies with similar identities. @tmat was this an intended consequence of those changes?