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

SDK version of hostfxr.dll cannot initialize self-contained app #56968

Closed
tippisum opened this issue Aug 6, 2021 · 7 comments
Closed

SDK version of hostfxr.dll cannot initialize self-contained app #56968

tippisum opened this issue Aug 6, 2021 · 7 comments
Milestone

Comments

@tippisum
Copy link

tippisum commented Aug 6, 2021

Description

When a native host uses the default .NET SDK version of hostfxr.dll (the one returned by get_hostfxr_path(buffer, size, nullptr)) to initialize a self-contained app, hostfxr_initialize_for_dotnet_command_line fails with error:

A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
Failed to run as a self-contained app.

If the native host instead directly loads the hostfxr.dll from the app path then the error does not occur and the app is initialized properly.
If the app is executed by dotnet.exe <app.dll> then the app can start too.

Configuration

.NET 5
Windows 10 21H1 x64

Trace

Tracing enabled @ Fri Aug 6 00:00:00 2021 GMT
Using global installation location [C:\Program Files\dotnet] as runtime location.
Reading fx resolver directory=[C:\Program Files\dotnet\host\fxr]
Considering fxr version=[5.0.8]...
Detected latest fxr version=[C:\Program Files\dotnet\host\fxr\5.0.8]...
Resolved fxr [C:\Program Files\dotnet\host\fxr\5.0.8\hostfxr.dll]...
Tracing enabled @ Fri Aug 6 00:00:00 2021 GMT
--- Invoked hostfxr_initialize_for_dotnet_command_line [commit hash: 35964c9]
Checking if CoreCLR path exists=[C:\Program Files\dotnet\host\fxr\5.0.8\coreclr.dll]
--- Executing in muxer mode...
Using the provided arguments to determine the application to execute.
App runtimeconfig.json from [C:\app\app.dll]
Runtime config is cfg=C:\app\app.runtimeconfig.json dev=C:\app\app.runtimeconfig.dev.json
Attempting to read runtime config: C:\app\app.runtimeconfig.json
Attempting to read dev runtime config: C:\app\app.runtimeconfig.dev.json
Runtime config [C:\app\app.runtimeconfig.json] is valid=[1]
Executing as a self-contained app as per config file [C:\app\app.runtimeconfig.json]
--- Resolving hostpolicy.dll version from deps json [C:\app\app.deps.json]
Resolved version from dependency manifest file [C:\app\app.deps.json]
Dependency manifest C:\app\app.deps.json does not contain an entry for runtime.win-x64.Microsoft.NETCore.DotNetHostPolicy
The expected hostpolicy.dll directory is [C:\Program Files\dotnet]
The hostpolicy.dll was not found in [C:\Program Files\dotnet]
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
Failed to run as a self-contained app.

  • The application was run as a self-contained app because 'C:\app\app.runtimeconfig.json' did not specify a framework.
  • If this should be a framework-dependent app, specify the appropriate framework in 'C:\app\app.runtimeconfig.json'.
@dotnet-issue-labeler dotnet-issue-labeler bot added area-Host untriaged New issue has not been triaged by the area owner labels Aug 6, 2021
@ghost
Copy link

ghost commented Aug 6, 2021

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

Issue Details

Description

When a native host uses the default .NET SDK version of hostfxr.dll (the one returned by get_hostfxr_path(buffer, size, nullptr)) to initialize a self-contained app, hostfxr_initialize_for_dotnet_command_line fails with error:

A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
Failed to run as a self-contained app.

If the native host instead directly loads the hostfxr.dll from the app path then the error does not occur and the app is initialized properly.
If the app is executed by dotnet.exe <app.dll> then the app can start too.

Configuration

.NET 5
Windows 10 21H1 x64

Trace

Tracing enabled @ Fri Aug 6 00:00:00 2021 GMT
Using global installation location [C:\Program Files\dotnet] as runtime location.
Reading fx resolver directory=[C:\Program Files\dotnet\host\fxr]
Considering fxr version=[5.0.8]...
Detected latest fxr version=[C:\Program Files\dotnet\host\fxr\5.0.8]...
Resolved fxr [C:\Program Files\dotnet\host\fxr\5.0.8\hostfxr.dll]...
Tracing enabled @ Fri Aug 6 00:00:00 2021 GMT
--- Invoked hostfxr_initialize_for_dotnet_command_line [commit hash: 35964c9]
Checking if CoreCLR path exists=[C:\Program Files\dotnet\host\fxr\5.0.8\coreclr.dll]
--- Executing in muxer mode...
Using the provided arguments to determine the application to execute.
App runtimeconfig.json from [C:\app\app.dll]
Runtime config is cfg=C:\app\app.runtimeconfig.json dev=C:\app\app.runtimeconfig.dev.json
Attempting to read runtime config: C:\app\app.runtimeconfig.json
Attempting to read dev runtime config: C:\app\app.runtimeconfig.dev.json
Runtime config [C:\app\app.runtimeconfig.json] is valid=[1]
Executing as a self-contained app as per config file [C:\app\app.runtimeconfig.json]
--- Resolving hostpolicy.dll version from deps json [C:\app\app.deps.json]
Resolved version from dependency manifest file [C:\app\app.deps.json]
Dependency manifest C:\app\app.deps.json does not contain an entry for runtime.win-x64.Microsoft.NETCore.DotNetHostPolicy
The expected hostpolicy.dll directory is [C:\Program Files\dotnet]
The hostpolicy.dll was not found in [C:\Program Files\dotnet]
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'C:\Program Files\dotnet'.
Failed to run as a self-contained app.

  • The application was run as a self-contained app because 'C:\app\app.runtimeconfig.json' did not specify a framework.
  • If this should be a framework-dependent app, specify the appropriate framework in 'C:\app\app.runtimeconfig.json'.
Author: tippisum
Assignees: -
Labels:

area-Host, untriaged

Milestone: -

@vitek-karas vitek-karas added this to the 7.0.0 milestone Aug 9, 2021
@vitek-karas vitek-karas removed the untriaged New issue has not been triaged by the area owner label Aug 9, 2021
@vitek-karas
Copy link
Member

This was never supposed to work. The call to get_hostfxr_path(buffer, size, nullptr) is never going to work on self-contained. It will only ever return the hostfxr from the SDK/runtime install, but that is not the one which can/should be used to run self-contained anything. Self-contained by definition has everything in it, and should only be used with the libraries it carries, not with anything external.

The call to get_hostfxr_path should look like this:

        get_hostfxr_parameters params;
        params.size = sizeof(params);
        params.assembly_path = pathToTheManagedDll;
        params.dotnet_root = NULL;
        int rc = get_hostfxr_path(buffer, &buffer_size, &params);

This will:

  • If there's already hostfxr in the process, it will return path to that one
  • Otherwise if in the directory of pathToTheManagedDlld there's hostfxr.dll, it will return that one (assuming it's a self-contained app, since it carries its own hostfxr).
  • Otherwise it will look for hostfxr in the install location (SDK/runtime) and return that one.

With this, the followup call to hostfxr_initialize_for_dotnet_command_line will work as expected, for both self-contained or framework-dependent apps.

I understand that this can be a bit confusing - but self-contained really assumes that the native hosting knows about it to a point.

@freddyrios
Copy link

Self-contained by definition has everything in it, and should only be used with the libraries it carries, not with anything external.

@vitek-karas does that statement applies to plugins? I am trying an upgrade to avoid a memory leak in .net 5, but I am also facing a "InvalidOperationException: Cannot load hostpolicy library". I imagine plugins has not been broken for self contained apps in .net 6, right?

@vitek-karas
Copy link
Member

@freddyrios Plugins should work just fine with self-contained app - I assume by "plugins" you mean using AssemblyDependencyResolver, right?

If you're running into problems, please create a new issue on the topic, I would be definitely very interested if something's broken.

@vitek-karas
Copy link
Member

Closing this as effectively by design.

@freddyrios
Copy link

freddyrios commented Oct 4, 2021

@vitek-karas yes. I reproduced it with a new app following https://docs.microsoft.com/en-us/dotnet/core/tutorials/creating-app-with-plugin-support, but still making sure I am not doing something wrong.

Edit: effectively, did something wrong. Won't be able to check if something similar was happening with the full app until tomorrow at work.

@freddyrios
Copy link

@vitek-karas unfortunately I only made the mistake when confirming it is a regression, got a clean reproduction and filed a bug: #59961

@ghost ghost locked as resolved and limited conversation to collaborators Nov 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants