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

DOTNET_ROOT does not work as the doc says. #64244

Closed
walterlv opened this issue Jan 25, 2022 · 5 comments
Closed

DOTNET_ROOT does not work as the doc says. #64244

walterlv opened this issue Jan 25, 2022 · 5 comments

Comments

@walterlv
Copy link

walterlv commented Jan 25, 2022

Description

This document below says that the DOTNET_ROOT environment variables can be set for the AppHost to determine the .NET Runtime. But I've tested that it does not work.

PS C:\Users\walterlv\Desktop> .\win-x64\dotnetCampus.AppHost.exe
A fatal error occurred. The required library hostfxr.dll could not be found.
If this is a self-contained application, that library should exist in [C:\Users\liquid\Desktop\win-x64\].
If this is a framework-dependent application, install the runtime in the global location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x64\InstallLocation].

The .NET runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=win10-x64&apphost_version=6.0.1

Reproduction Steps

Prepare#1

  1. Prepare a hello world sample project targeting net6.0 or net5.0.
  2. On a Windows 11 virtual machine, run the output exes above.
  3. Download the .NET 6.0 Runtime using the link provided by the output exe's error message.
  4. Back up the virtual machine.

Prepare#2

  1. Install the .NET 6.0 runtime using the file downloaded before.
  2. Back up the installed .NET 6.0 runtime at C:\Program Files\dotnet to any place outside the virtual machine.
  3. Restore the virtual machine to the point backed up before the .NET 6.0 runtime installation.

Reproduction

  1. Copy back the .NET 6.0 runtime folder from the place backed up before to another location such as C:\Users\walterlv\Desktop\dotnet.
  2. Add an environment variable named DOTNET_ROOT to point to the new runtime directory.
  3. Start a new windows terminal to run the hello world sample output exes.

Expected behavior

The output exe can run successfully.

Actual behavior

The output exe still prints the .NET Runtime error message. But it runs successfully if we copy the dotnet runtime folder to its default location. This means that the DOTNET_ROOT seems not to work.

Regression?

It doesn't work properly either on .NET 6.0 or on .NET 5.0.

Known Workarounds

I have no workaround currently and waiting for one to customize the dotnet root directory.

What I need is to share the dotnet runtime among multiple exes targeting the same version of .NET 6.0 in a single solution but ignoring the system installed one which is often affected by some other software.

Configuration

No response

Other information

No response

@walterlv
Copy link
Author

@walterlv
Copy link
Author

walterlv commented Jan 25, 2022

Related codes:

https://github.com/dotnet/runtime/blob/v6.0.1/src/native/corehost/fxr_resolver.cpp#L55

bool fxr_resolver::try_get_path(const pal::string_t& root_path, pal::string_t* out_dotnet_root, pal::string_t* out_fxr_path)
{
#if defined(FEATURE_APPHOST) || defined(FEATURE_LIBHOST)
    // For apphost and libhost, root_path is expected to be a directory.
    // For libhost, it may be empty if app-local search is not desired (e.g. com/ijw/winrt hosts, nethost when no assembly path is specified)
    // If a hostfxr exists in root_path, then assume self-contained.
    if (root_path.length() > 0 && library_exists_in_dir(root_path, LIBFXR_NAME, out_fxr_path))
    {
        trace::info(_X("Resolved fxr [%s]..."), out_fxr_path->c_str());
        out_dotnet_root->assign(root_path);
        return true;
    }

    // For framework-dependent apps, use DOTNET_ROOT_<ARCH>
    pal::string_t default_install_location;
    pal::string_t dotnet_root_env_var_name;
    if (get_dotnet_root_from_env(&dotnet_root_env_var_name, out_dotnet_root))
    {
        trace::info(_X("Using environment variable %s=[%s] as runtime location."), dotnet_root_env_var_name.c_str(), out_dotnet_root->c_str());
    }
    else
    {
        if (pal::get_dotnet_self_registered_dir(&default_install_location) || pal::get_default_installation_dir(&default_install_location))
        {
            trace::info(_X("Using global installation location [%s] as runtime location."), default_install_location.c_str());
            out_dotnet_root->assign(default_install_location);
        }
        else
        {
            trace::error(_X("A fatal error occurred, the default install location cannot be obtained."));
            return false;
        }
    }

    // Other codes...
}

It detects the hostfxr.dll file. If it's in the APP_ROOT directory, it returns true without detecting the environment variables. But in this sample, it does not exist.

@ghost
Copy link

ghost commented Jan 25, 2022

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

Issue Details

Description

This document below says that the DOTNET_ROOT environment variables can be set for the AppHost to determine the .NET Runtime. But I've tested that it does not work.

PS C:\Users\walterlv\Desktop> .\win-x64\dotnetCampus.AppHost.exe
A fatal error occurred. The required library hostfxr.dll could not be found.
If this is a self-contained application, that library should exist in [C:\Users\liquid\Desktop\win-x64\].
If this is a framework-dependent application, install the runtime in the global location [C:\Program Files\dotnet] or use the DOTNET_ROOT environment variable to specify the runtime location or register the runtime location in [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x64\InstallLocation].

The .NET runtime can be found at:
  - https://aka.ms/dotnet-core-applaunch?missing_runtime=true&arch=x64&rid=win10-x64&apphost_version=6.0.1

Reproduction Steps

Prepare#1

  1. Prepare a hello world sample project targeting net6.0 or net5.0.
  2. On a Windows 11 virtual machine, run the output exes above.
  3. Download the .NET 6.0 Runtime using the link provided by the output exe's error message.
  4. Back up the virtual machine.

Prepare#2

  1. Install the .NET 6.0 runtime using the file downloaded before.
  2. Back up the installed .NET 6.0 runtime at C:\Program Files\dotnet to any place outside the virtual machine.
  3. Restore the virtual machine to the point backed up before the .NET 6.0 runtime installation.

Reproduction

  1. Copy back the .NET 6.0 runtime folder from the place backed up before to another location such as C:\Users\walterlv\Desktop\dotnet.
  2. Add an environment variable named DOTNET_ROOT to point to the new runtime directory.
  3. Start a new windows terminal to run the hello world sample output exes.

Expected behavior

The output exe can run successfully.

Actual behavior

The output exe still prints the .NET Runtime error message. But it runs successfully if we copy the dotnet runtime folder to its default location. This means that the DOTNET_ROOT seems not to work.

Regression?

It doesn't work properly either on .NET 6.0 or on .NET 5.0.

Known Workarounds

I have no workaround currently and waiting for one to customize the dotnet root directory.

What I need is to share the dotnet runtime among multiple exes targeting the same version of .NET 6.0 in a single solution but ignoring the system installed one which is often affected by some other software.

Configuration

No response

Other information

No response

Author: walterlv
Assignees: -
Labels:

area-Host

Milestone: -

@vitek-karas
Copy link
Member

Can you please try to collect host traces when it fails:

set COREHOST_TRACE=1
set COREHOST_TRACEFILE=host.txt
<run the app an let it fail>

Note that the produced host.txt may contain information about the machine/user it runs on, like env. variables , paths and so on. If you're OK with it, please share it here.

The traces will contain detailed information on why the host decided to probe a specific directory, if it actually read an env. variable and so on.

@walterlv
Copy link
Author

walterlv commented Jan 25, 2022

@vitek-karas Thank you for your suggestions and I get the host.txt file to find out the issue I've met.

The key reason is that:

  • I was using PowerShell to set the Environment Variables but it didn't work. The correct way to set an Environment Variable is to use a cmd.

After I use the cmd instead, the .NET 6.0 application runs successfully.

Download host.txt

@ghost ghost locked as resolved and limited conversation to collaborators Feb 25, 2022
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