-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Assembly.GetCallingAssembly() needs IL2026-like warning for Native AOT (and self contained) builds #94200
Comments
Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas Issue DetailsDescriptionI am trying to get Native AOT build working with NET 8 RC2 and run into the following situation: Assembly.GetCallingAssembly() throws System.PlatformNotSupportedException: Operation is not supported on this platform. Once I tracked it down to this it became apparent that this is due to self-contained file not having calling assembly per se, which is fine, but it would have been VERY helpful if compiler/linker produced IL2026-like warning that this usage will fail, right now there is none, so that took extra time to locate what was going on. IL2026 warnings are very helpful pointing out likely issues for Native AOT builds and this instance deserves to be added to the list. It's possible there are other similar exceptions that would deserve similar warning for Native AOT or even just self-contained builds. Reproduction Steps// Native AOT build - Assembly.GetCallingAssembly(); Expected behaviorI expected to see IL2026-like warning from compiler, maybe even elevated to error that needs to be actively supressed if user desires so. Actual behaviorNo warning during compile/link time. Regression?No Known WorkaroundsNo response ConfigurationWindows 11 Pro on Intel x64 Other informationNo response
|
#71973 has a related discussion. We don't currently have a good way to annotate this API. We'd ideally want to obsolete this API instead and replace it with a language feature. What is your use case? Would the language feature be a good replacement? |
The use case is making it easier for the users to get Native AOT (or just self-contained) working - there are already similar warnings (IL2026) that help achieve that aim, they just need to be expanded to make it easy - I have no opinion on replacement feature, but since you've asked in my case I used it to get assembly for retrieval of embedded into file resources, something that does not seem to have easy solution for Native AOT builds as from what I can see EmbeddedFileProvider alternative also requires Assembly. |
|
Yep, that works, thank you! Is there an easy way (like defined conditional) to know during execution (or build time) if:
I can of course define my own conditionals for multiple projects, but since multiple builds happen at the same time it seems rather error-prone way of doing it. |
We don't have a detection API for the various deployment options (AOT, single file, ready to run, etc.). The app should ideally not have code that does different things based on these. The development experience is not optimized for this (e.g. F5 debugging the app from VS/VS Code will always run the version of code without any of these, so will unit tests, etc.). The app will be a lot harder to maintain with such conditionals. If you can avoid GetCallingAssembly on NativeAOT, avoid it everywhere. |
The problem with this API has always been the possible non-existence of reflection metadata about the calling method. But I realized that the reflection metadata can be supplemented by stack trace metadata that also knows assemblies of all methods on the stack. So this API is supportable as long as stack traces are not disabled. It's also conveniently easy to implement with the new `DiagnosticMethodInfo` API we added in .NET 9. I'm not sure if we want to go as far as make this API not work on CoreCLR with JIT if `StackTraceSupport` is false, but we could do that. It might be preferable, but a bit breaking. Resolves dotnet#94200.
Won't fixing this. dotnet/csharplang#4984 needs to be implemented. |
Description
I am trying to get Native AOT build working with NET 8 RC2 and run into the following situation:
Assembly.GetCallingAssembly() throws System.PlatformNotSupportedException: Operation is not supported on this platform.
Once I tracked it down to this it became apparent that this is due to self-contained file not having calling assembly per se, which is fine, but it would have been VERY helpful if compiler/linker produced IL2026-like warning that this usage will fail, right now there is none, so that took extra time to locate what was going on. IL2026 warnings are very helpful pointing out likely issues for Native AOT builds and this instance deserves to be added to the list.
It's possible there are other similar exceptions that would deserve similar warning for Native AOT or even just self-contained builds.
Reproduction Steps
// Native AOT build -
Assembly.GetCallingAssembly();
Expected behavior
I expected to see IL2026-like warning from compiler, maybe even elevated to error that needs to be actively supressed if user desires so.
Actual behavior
No warning during compile/link time.
Regression?
No
Known Workarounds
No response
Configuration
Windows 11 Pro on Intel x64
Visual Studio 2022 17.8 Preview 5
Other information
No response
The text was updated successfully, but these errors were encountered: