dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.67k stars 1.06k forks source link

Make it possible to use newer ILCompiler packages than what SDK comes with #26268

Closed tomrus88 closed 2 years ago

tomrus88 commented 2 years ago

Describe the bug

Publishing app with NativeAOT results in broken binary.

To Reproduce

Presence of both **PublishAot** and **PackageReference** to Microsoft.DotNet.ILCompiler is important.
- Publish app (win-x64) and then run it
- You will get exception like this

Unhandled Exception: System.InvalidOperationException: Operation is not valid due to the current state of the object. at Internal.Reflection.Core.Execution.ReflectionCoreExecution.InitializeExecutionDomain(ReflectionDomainSetup, ExecutionEnvironment) + 0x94 at Internal.Reflection.Execution.ReflectionExecution.Initialize() + 0x35 at ConsoleApp5!+0x1f0d6a


### Further technical details
- Tested with .NET 7 SDK Preview 5 and also .NET 7 SDK daily build
- Here is why it doesn't work: build task that generates AppName.ilc.rsp file for ilc compiler adds duplicate **--initassembly** arguments

--initassembly:System.Private.CoreLib --initassembly:System.Private.StackTraceMetadata --initassembly:System.Private.TypeLoader --initassembly:System.Private.Reflection.Execution --initassembly:System.Private.CoreLib --initassembly:System.Private.StackTraceMetadata --initassembly:System.Private.TypeLoader --initassembly:System.Private.Reflection.Execution

AntonLapounov commented 2 years ago

You should not add the PackageReference for Microsoft.DotNet.ILCompiler as it is now a part of the SDK, which will be used whenever the PublishAot property is set to true. When you explicitly add the PackageReference, you end up with duplicate Microsoft.NETCore.Native.targets files and duplicate entries in AutoInitializedAssemblies item group.

@MichalStrehovsky @LakshanF We probably need to add some protection for this case.

MichalStrehovsky commented 2 years ago

We would like to make it possible to "upgrade" the ILCompiler package that is in use by directly referencing a newer package. So the fix will be along the lines of not adding the built-in ILCompiler targets when they're already added from the package. I think IL Linker package does that too.

Let's use this issue to track that.

Cc @dotnet/ilc-contrib

agocke commented 2 years ago

@LakshanF Can you take a look at this? It will probably be necessary for your work to boostrap ILC as we may reasonably need to upgrade the version of ILC sooner than SDK updates