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

Source Generators: Allow loading referenced assemblies #45060

Open
AndriySvyryd opened this issue Jun 10, 2020 · 5 comments
Open

Source Generators: Allow loading referenced assemblies #45060

AndriySvyryd opened this issue Jun 10, 2020 · 5 comments

Comments

@AndriySvyryd
Copy link
Member

AndriySvyryd commented Jun 10, 2020

Background

I'm investigating whether Source Generators can be used to make Entity Framework Core AOT-compatible. Currently EF needs to use reflection to create its model and then compiles lambda expressions to manipulate user-provided types efficiently. We could do this at design time and generate a model that would be compiled along with the user's code instead.

Problem

Users configure the model by overriding the OnModelCreating method on DbContext. To be able to generate a compilable model we first need to run that code, looking at the source code is not enough.

Currently there is no way to get the full file paths of the assemblies referenced from the code being analyzed to be able to load them and run the configuration code.

Proposal

To limit the impact and prevent a leaky abstraction exposing the assembly file paths can be avoided. We only need to be able to run the source generator in an AppDomain that has all the referenced assemblies loaded. So the details can be hidden away behind a method or by implementing an interface.

void ExecuteWithReferencedAssemblies(this SourceGeneratorContext context, Action<SourceGeneratorContext> executeAction);
@eostarman

This comment has been minimized.

@jmarolf jmarolf self-assigned this Aug 18, 2020
@roji

This comment has been minimized.

@jmarolf jmarolf removed their assignment Jan 16, 2021
@fiseni
Copy link

fiseni commented Apr 3, 2021

I have the same scenario. I have to run fluent-like configurations and based on that produce some code.

The full file paths of the referenced assemblies can be found under GeneratorExecutionContext.Compilation.References.Display.
For now, this is how I do it. It's a terrible hack, but I have no better way of doing it.

@ViRuSTriNiTy
Copy link

ViRuSTriNiTy commented Jan 17, 2022

@fiseni Nice approach. I can confirm that this workaround breaks VS as I did give this a try and it breaks everything from VS to the compiler process running in the background as you have mentioned in the comments of your source. I even tried to use reflection only context as this would be sufficient for my case but to no avail, things get even messier there.

@fiseni
Copy link

fiseni commented Jan 17, 2022

Oh, that was just an experimental project. That approach shouldn't be used in any case. Refer to this comment here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants