-
Notifications
You must be signed in to change notification settings - Fork 5.3k
[WIP] Update entry assembly #100935
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
Closed
Closed
[WIP] Update entry assembly #100935
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>$(NetCoreAppCurrent)</TargetFramework> | ||
| <!-- always set SelfContained when running to use Mono on desktop --> | ||
| <SelfContained>true</SelfContained> | ||
| </PropertyGroup> | ||
|
|
||
| <UsingTask TaskName="MonoAOTCompiler" | ||
| AssemblyFile="$(MonoAOTCompilerTasksAssemblyPath)" /> | ||
|
|
||
| <Target Name="AOTCompileApp" Condition="'$(RunAOTCompilation)' == 'true'" AfterTargets="CopyFilesToPublishDirectory"> | ||
| <PropertyGroup> | ||
| <_AotOutputType>Library</_AotOutputType> | ||
| <_AotLibraryFormat>Dylib</_AotLibraryFormat> | ||
| <UseAotDataFile>false</UseAotDataFile> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <AotInputAssemblies Include="$(PublishDir)\*.dll" /> | ||
| </ItemGroup> | ||
|
|
||
| <MonoAOTCompiler | ||
| CompilerBinaryPath="@(MonoAotCrossCompiler->WithMetadataValue('RuntimeIdentifier','$(TargetOS)-$(TargetArchitecture.ToLowerInvariant())'))" | ||
| OutputType="$(_AotOutputType)" | ||
| LibraryFormat="$(_AotLibraryFormat)" | ||
| Assemblies="@(AotInputAssemblies)" | ||
| OutputDir="$(PublishDir)" | ||
| CollectTrimmingEligibleMethods="$(StripILCode)" | ||
| TrimmingEligibleMethodsOutputDirectory="$(TrimmingEligibleMethodsOutputDirectory)" | ||
| IntermediateOutputPath="$(IntermediateOutputPath)" | ||
| UseAotDataFile="$(UseAotDataFile)" | ||
| MibcProfilePath="$(MibcProfilePath)" | ||
| UseLLVM="$(MonoEnableLLVM)" | ||
| LLVMPath="$(MonoAotCrossDir)"> | ||
| <Output TaskParameter="CompiledAssemblies" ItemName="BundleAssemblies" /> | ||
| </MonoAOTCompiler> | ||
| </Target> | ||
|
|
||
| <UsingTask TaskName="ILStrip" | ||
| AssemblyFile="$(MonoTargetsTasksAssemblyPath)" /> | ||
|
|
||
| <Target Name="StripILCode" Condition="'$(StripILCode)' == 'true'" AfterTargets="AOTCompileApp"> | ||
| <PropertyGroup> | ||
| <TrimIndividualMethods>true</TrimIndividualMethods> | ||
| </PropertyGroup> | ||
|
|
||
| <ILStrip | ||
| TrimIndividualMethods="$(TrimIndividualMethods)" | ||
| Assemblies="@(BundleAssemblies)"> | ||
| <Output TaskParameter="TrimmedAssemblies" ItemName="TrimmedAssemblies" /> | ||
| </ILStrip> | ||
|
|
||
| <Copy | ||
| SourceFiles="@(TrimmedAssemblies->Metadata('TrimmedAssemblyFileName'))" | ||
| DestinationFiles="@(TrimmedAssemblies)" | ||
| OverwriteReadOnlyFiles="true" | ||
| /> | ||
| <Delete Files="@(TrimmedAssemblies->Metadata('TrimmedAssemblyFileName'))" /> | ||
| </Target> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| TOP=../../../../ | ||
| DOTNET:=$(TOP)dotnet.sh | ||
| DOTNET_Q_ARGS=--nologo -v:q -consoleloggerparameters:NoSummary | ||
|
|
||
| MONO_CONFIG?=Debug | ||
| MONO_ARCH?=$(shell . $(TOP)eng/common/native/init-os-and-arch.sh && echo $${arch}) | ||
| TARGET_OS?=$(shell . $(TOP)eng/common/native/init-os-and-arch.sh && echo $${os}) | ||
| AOT?=false | ||
| USE_LLVM?=false | ||
| StripILCode?=false | ||
| TrimmingEligibleMethodsOutputDirectory?= #<path-to-a-writable-directory> | ||
|
|
||
| #MIBC_PROFILE_PATH=<path-to-mibc-for-sample> | ||
|
|
||
| MONO_ENV_OPTIONS ?= | ||
|
|
||
| publish: | ||
| $(DOTNET) publish \ | ||
| -c $(MONO_CONFIG) \ | ||
| -r $(TARGET_OS)-$(MONO_ARCH) \ | ||
| /p:RunAOTCompilation=$(AOT) \ | ||
| /p:MonoEnableLLVM=$(USE_LLVM) \ | ||
| /p:StripILCode=$(StripILCode) \ | ||
| /p:TrimmingEligibleMethodsOutputDirectory=$(TrimmingEligibleMethodsOutputDirectory) \ | ||
| '/p:MibcProfilePath="$(MIBC_PROFILE_PATH)"' \ | ||
| /bl | ||
|
|
||
| run: publish | ||
| DOTNET_DebugWriteToStdErr=1 \ | ||
| MONO_ENV_OPTIONS="$(MONO_ENV_OPTIONS)" \ | ||
| $(TOP)artifacts/bin/HelloWorld_2/$(MONO_ARCH)/$(MONO_CONFIG)/$(TARGET_OS)-$(MONO_ARCH)/publish/HelloWorld_2 | ||
|
|
||
| clean: | ||
| rm -rf $(TOP)artifacts/bin/HelloWorld_2/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
|
|
||
| namespace HelloWorld_2 | ||
| { | ||
| internal class Program | ||
| { | ||
| private static void Main(string[] args) | ||
| { | ||
| Console.WriteLine($"Hello World 2"); | ||
| } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you trying to achieve by this refactoring?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What we are trying to determine is the best place to allow swapping the entry assembly and it having a real effect. The goal in #99883 is to allow some / all of the TPA assemblies to load, sleep/wake up, and allow the entry assembly to be swapped.
We've identified two ways to do this. Make the runtime aware, which is what this PR is attempting to do. And the second is another export in the host that doesn't shut the runtime down after it's done (the onus would be put on the custom host to do that).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternative approach we've discussed in the past is this:
This is more of the "do this in managed code" approach. Not sure how this would fit with the host changes to allow for TPA modifications after startup.
There are other similar ways - for example the above could be done without startup hooks, where the original
Mainwould perform the operations and effectively "replace itself".There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The way the runtime appears to work right now is that the original main will be executed regardless of what you do in the hook. Also, aren't there limitations executing code from the hook?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes - but nothing is stopping the hook from calling Environment.Exit ;-)
I think the idea of doing this from the main is better anyway. That's what
dotnet watchuses for ASP.NET anyway. BTW that would be another interesting thing to look at - they're one of the teams asking for the entry point replacement as the watch breaks some apps which rely on it.Not really - AFAIK there are no restrictions other than "you should not do that" - but mostly those are there to not break the "Main", but if the "Main" is empty then they're moot.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TPA additions after startup can be done via the AssemblyLoadContext.Default.
I do not think we should be enabling modifications of TPA after startup. It would be a pit of failure when an assembly that wanted to be overridden by the copy in the app got loaded too early. Instead, we should figure out how to startup the runtime with TPA subset that is never going to be modified.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we take the Main approach, then the real app main could be invoked by reflection. I wonder what is the need of creating a new API
Assembly.SetEntryPoint? It seems that what Functions team would like to do could be achieved with existing API's. Am I missing anything?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without the new API for example this
Assembly.GetEntryAssemblywould return wrong value (it would point to the "host's templateMain"). I think there are other places that this shows up in some way or form as well. Some apps use this to mostly get to the location of app's files (Assembly.GetEntryAssembly().Location) for example.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ASP.NET Core has number of places that call Assembly.GetEntryAssembly: https://github.com/search?q=repo%3Adotnet%2Faspnetcore+GetEntryAssembly&type=code
As Vitek said, it is important for Assembly.GetEntryAssembly to return the actual application assembly to avoid breaking these places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Gotcha!