corehost: Make shared framework and deps paths available to activated app
In MVC and other contexts where we want to do dynamic compilation and loading, we re-read the deps file in managed code in order to properly identify all the necessary compilation/runtime dependencies. However, to fully resolve this, we need to do the same thing corehost does, namely merging this deps data with the one defined in the shared framework.
In the current design, corehost simply uses it's location to determine where the shared framework lives (i.e. in muxer mode, it reads the framework name and version and looks in OWNDIR/../shared/[name]/[version]).
Similarly, when corehost is invoked with --deps, we want our managed APIs to be able to identify the path to that deps file. This is needed for xunit to be able to load properly. /cc @piotrpMSFT
We need to look for a way for corehost to provide this data to the managed application. Perhaps through a P/Invoke (managed app P/Invokes back into corehost?) or other shared state? Environment variables would also be a simple way to do this for now (we did this with DNX and it was somewhat problematic, but did work)
/cc @gkhanna79 @schellap @pakrym @davidfowl
This wold become blocker for MVC when we move to "fat" apps
Ping @gkhanna79 @schellap @davidfowl @piotrpMSFT @Petermarcu
This is going to block MVC. Since the only one who knows the location of the shared framework and the deps file are is corehost, we need to try and come up some something to propagate that data to managed code so we can properly load additional assemblies or create compilations based on that data.
There are some hacks we could use, but none of them cover anything, and my hope is that since corehost has this data readily available, we could just stuff it in environment variables or something.
We can have an util function exported out of a global storage for this. Or as a property on appdomain appcontext similar to basedirectory.
Not sure that a property off the AppContext would be accessible to managed code, since I think those are not exported in CoreCLR.
A util function would be OK. We'd have to P/Invoke back into corehost to get it, but it would be fairly safe.
What is the exact info you need? Paths to the fx dir and app dir isn't sufficient? You should be able to load up the deps from there.
would be accessible to managed code, since I think those are not exported in CoreCLR
@weshaggard, is it non trivial to expose this if set as a property to the CLR like app context base?
- Path to the Shared Framework
.deps.jsonfile (if any) - Path to the application's
.deps.jsonfile (since--depsfilemay mean it is NOT the same as the app base). - Path to the application's
.dll(already available inAppContext.BaseDirectory, so we're good there)
Basically, the managed code in Microsoft.Extensions.DependencyModel has to be able to perform all the same resolution as corehost does in native code, so we need all the inputs.
AppDomain is not accessible but AppContext is accessible however AppContext doesn't have any of this information right now and no generic data bag to put it into either. I also don't think putting a specific API on AppContext for this is the correct thing to do.
It sounds like the best answer would be to expose an export from corehost that would give back the list of deps files that it is using.
So something like the following would probably be ideal:
wchar_t* corehost_get_deps_file_list()
Note: Since we want to read this from managed code, I assume we'll need it to return wchar_t.
Which we could P/Invoke with:
[DllImport("hostpolicy.dll?")]
public unsafe extern char* corehost_get_deps_file_list()
The exact DLL in which it lives doesn't really matter, as long as it's something that is already loaded, so the CoreCLR can find it to P/Invoke.
The value could just be an ordered (we need to know which was applied first, second, etc. so we can figure out which is the shared framework one, etc.) PATH-separator (; on Windows, : on Unix) delimited list of paths to deps files.
Thoughts @davidfowl @pakrym ? It's not pretty to have to P/Invoke, but the alternative is basically to have an environment variable (which has it's own problems due to inheritance of the env vars).
PInvoking to the host to get this data is a bad design and dependency. The host is an implementation detail that managed code should be unaware of this. It should be free to change underneath or even disappear (e.g. In CoreRT, the host maybe different artifact, or incases, none, at all).
AppContext doesn't have any of this information right now and no generic data bag to put it into either.
@weshaggard , currently AppDomain initialization gets the Key/Value pairs from the host. For CLI, these are initialized in https://github.com/dotnet/cli/blob/328e806069252f3fff3854c9c451d98ae71100da/src/corehost/cli/hostpolicy.cpp and passed for AppDomain initialization where other things like AppContext Compat mode, etc get passed. Is there a reason these values cannot be passed in the same manner to the AppDomain initialization and AppContext can pick it from there for its initialization and make it available for others to consume?
I,d prefer that actually. As long as we can round trip the data to managed code properly
@anurse, just a thought, would a combination of dladdr(coreclr.dll), similar for windows in conjunction with BaseDirectory, give you the two directories you need and then getting the deps file is based on GetExecutingAssembly.deps.json and netcoreapp.deps.json if coreclr dir is different from basedir?
Of course app will break if the app activation model changes, but that would be rare.
Here's the library:
https://github.com/dotnet/cli/tree/rel/1.0.0/src/Microsoft.Extensions.DependencyModel
This is the code that needs to do the right thing and get both deps file so that applications can have a full view of dependencies.
@schellap we can not get location passed via --deps corehost argument this way.
@weshaggard What are your thoughts on AppContext delivering these values based upon my suggestion earlier?
@pakrym --depsfile arg to the host will set the appcontext base to app location. GetExecutingAssembly at app entry should give you app name or you can just hard code this in the app too. This should give you the deps file. I don't think you would ever rename (EDIT: move) the deps file and pass it to --depsfile.
Coreclr should givr you fx dir for Netcoreapp.deps.json.
I don't think you would ever rename (EDIT: move) the deps file and pass it to --depsfile
We are passing test library deps file along with xunit.runner.dll as entry point dll.
We are passing test library deps file along with xunit.runner.dll as entry point dll.
Don't follow where this breaks, could you please let me know who will fail to know about the deps file? The app being tested? Or xunit?
You still have the deps file's directory at appcontext basedirectory. The app can get to it using its own name.
I am totally in favor of a solution that involves passing the data through AppContext rather than a P/Invoke. The P/Invoke is absolutely not great layering, I'm just trying to get us a solution.
At the core, we need to know about all the deps files that were used. In the standard scenario, we can use typeof(object).Assembly.Location to get the one next to mscorlib (shared framework) and AppContext.BaseDirectory to get the one next to the app. However, in tests and dotnet-[toolname] activations (/cc @piotrpMSFT), the deps file is not co-located with the entry-point assembly, it is passed in via the --depsfile switch to corehost.
Even if corehost were to ensure the AppContext.BaseDirectory is pointing at the location containing the deps file, we don't know the name of the deps file since it is derived from the test assembly name, which is not the same as the entry assembly name (which is the xunit runner).
we don't know the name of the deps file since it is derived from the test assembly name, which is not the same as the entry assembly name (which is the xunit runner).
The host can pass the paths to the respective .deps.json and AppContext exposes them.