-
-
Notifications
You must be signed in to change notification settings - Fork 276
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
ILTemplateWithTempAssembly interferes with AppContext switches initialization #633
Comments
First of all, great research and great way to report a ticket, much appreciated. If there is a valid workaround (which appears there is), we won't be making changes on this front. We don't want to go into a rabbit hole causing a lot of work for things we don't use ourselves. As a side note: Costura is in maintenance mode: https://github.com/fody/costura#-read-this--package-is-in-maintenance-mode--read-this- We will still maintain it, but very carefully select what changes we make to the code. |
Thanks for your reply, Geert. I totally understand that Costura is in maintenance mode and that it's probably not worth trying to fix this, especially given that a rather clean workaround exists. It might still be worth documenting that caveat in the README under the CreateTemporaryAssemblies and Native Libraries and PreloadOrder sections, what do you think? |
Great idea. Interested in doing a PR with these improvements? |
Explain how to workaround AppContext switches interference and mention issue Fody#633.
Yeah, done in #634. |
Thank you very much. I'm sorry we can't fix this in any other way, but we need to carefully review any resources we put into libraries supported by any of us (for free). |
Since Costura.Template targets .NET Standard 2.0 we test `AppContext.TargetFrameworkName == null` which translates to `AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName == null` on .NET Framework and `Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName == null` on .NET Core. Fixes Fody#633
Since Costura.Template targets .NET Standard 2.0 we test `AppContext.TargetFrameworkName == null` which translates to `AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName == null` on .NET Framework and `Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName == null` on .NET Core. Fixes Fody#633
Since Costura.Template targets .NET Standard 2.0 we test `AppContext.TargetFrameworkName == null` which translates to `AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName == null` on .NET Framework and `Assembly.GetEntryAssembly()?.GetCustomAttribute<TargetFrameworkAttribute>()?.FrameworkName == null` on .NET Core. Fixes Fody#633
Please check all of the platforms you are having the issue on (if platform is not listed, it is not supported)
Actually, a console app on .NET Framework is enough to reproduce the issue.
Component
What component is this issue occurring in?
The Costura module initializer.
Version of Library
4.1.0
Version of OS(s) listed above with issue
Windows 10 Version 1909
Steps to Reproduce
Costura.Fody
4.1.0 andFody
6.3.0 package references.Here is the output which shows the default AppContext switch values when targeting .NET Framework 4.7.2
Expected Behavior
The AppContext switch values should be the same as before, using
CreateTemporaryAssemblies="true"
should not impact the AppContext switches.Actual Behavior
The AppContext switch values are different:
What's happening
When using
CreateTemporaryAssemblies=true
,ILTemplateWithTempAssembly
is used instead ofILTemplate
.ILTemplateWithTempAssembly
callsPath.GetTempPath()
in itsAttach()
method which is executed during module initialization. The call toPath.GetTempPath()
eventually kicks in the AppContext switches initialization procedure, as shown in this stack trace:The
ParseTargetFrameworkName()
method accessesAppDomain.CurrentDomain.SetupInformation.TargetFrameworkName
. But this is too early: during the module initializer,AppDomain.CurrentDomain.SetupInformation.TargetFrameworkName
is not yet set and is still null. This causes the AppContextPopulateDefaultValues()
to execute with a default (fallback) value of .NET Framework 4.0.0 setting wrong values for the AppContext switches.Workaround
Using
LoadAtModuleInit="false"
in the Costura configuration in addition toCreateTemporaryAssemblies="true"
then explicitly callingCosturaUtility.Initialize()
in the Program static constructor works around this issue becauseAppDomain.CurrentDomain.SetupInformation.TargetFrameworkName
is properly initialized at that point.Ideally, though, Costura should not interfere the the AppContext switches initialization code but I'm not sure what's the best way. Maybe the code that calls
Path.GetTempPath()
should be executed the first timeResolveAssembly
is called? Maybe at that pointAppDomain.CurrentDomain.SetupInformation.TargetFrameworkName
is properly set?Additional information
I wrote some notes while investigating this issue. This issue became visible to me because of the wrong initialization of
Switch.System.Diagnostics.IgnorePortablePDBsInStackTraces
which resulted in the loss of source information in stack traces when embedding the debug symbols with<DebugType>embedded</DebugType>
.The text was updated successfully, but these errors were encountered: