Skip to content
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

Closed
HighPerfDotNet opened this issue Oct 31, 2023 · 7 comments
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Milestone

Comments

@HighPerfDotNet
Copy link

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Oct 31, 2023
@ghost
Copy link

ghost commented Oct 31, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: HighPerfDotNet
Assignees: -
Labels:

untriaged, area-NativeAOT-coreclr

Milestone: -

@MichalStrehovsky
Copy link
Member

#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?

@HighPerfDotNet
Copy link
Author

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.

@MichalStrehovsky
Copy link
Member

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.

Assembly.GetExecutingAssembly will work, and so do things like typeof(SomeType).Assembly if that's an option. The problem is specifically in getting information about the method that called the current method (which is what GetCallingAssembly does).

@HighPerfDotNet
Copy link
Author

Yep, that works, thank you! Is there an easy way (like defined conditional) to know during execution (or build time) if:

  1. this is a self-contained file (therefore can't use GetCallingAssembly())
  2. on top of 1 this is also a Native AOT build (therefore other limitations apply)

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.

@MichalStrehovsky
Copy link
Member

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.

@MichalStrehovsky MichalStrehovsky added this to the Future milestone Nov 13, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Nov 13, 2023
MichalStrehovsky added a commit to MichalStrehovsky/runtime that referenced this issue Jul 1, 2024
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.
@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Jul 1, 2024
@MichalStrehovsky
Copy link
Member

Won't fixing this. dotnet/csharplang#4984 needs to be implemented.

@MichalStrehovsky MichalStrehovsky closed this as not planned Won't fix, can't repro, duplicate, stale Jul 10, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Aug 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-NativeAOT-coreclr in-pr There is an active PR which will close this issue when it is merged
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants