microsoft / node-api-dotnet

Advanced interoperability between .NET and JavaScript in the same process.
MIT License
495 stars 53 forks source link

Specifying non-standard location dotnet install via `DOTNET_ROOT` not working #282

Closed Sharpiro closed 5 months ago

Sharpiro commented 5 months ago

Windows 10 dotnet 8

I have a dotnet install location that I have acquired from the zipped binaries that are made available.

I specified via environment variable DOTNET_ROOT that I want it pointed there. However, regardless of if I point it to a fresh dotnet 8 runtime or sdk install, I always get the error Failed to find an installed .NET host compatible with target version 8.0. It is indeed finding the install, as otherwise the error is different, but for whatever reason it deems it to be invalid.

I would ideally like to use the environment variable to point to a portable dotnet 8 runtime.

jasongin commented 5 months ago

@Sharpiro, Can you set the environment variable NODE_API_TRACE_HOST=1, then capture the output? It includes some details about the process of finding and loading .NET.

Sharpiro commented 5 months ago

I actually figured out the problem and the workaround is to ensure there are no hyphens in the dotnet root path.

https://github.com/microsoft/node-api-dotnet/blob/389703cac317ef98d4db8ab35c2b4eae65f3bff5/src/NodeApi/DotNetHost/HostFxr.cs#L81

This is problematic considering the default download of a portable dotnet install contains hyphens: dotnet-runtime-8.0.4-win-x64.

jasongin commented 5 months ago

That line is meant to strip off an optional prerelease suffix from the version number. It is parsing only the version directories under $DOTNET_ROOT/host/fxr, so it shouldn't be handling any string of the form dotnet-runtime-8.0.4-win-x64.

string fxrDir = Path.Combine(dotnetRoot, "host", "fxr");
string[] versionDirs = Directory.GetDirectories(fxrDir);

When I extract dotnet-sdk-8.0.204-linux-x64.tar.gz, I only see an 8.0.4 directory under the host/fxr directory.

What is the value of your DOTNET_ROOT variable, and what directories do you see under $DOTNET_ROOT/host/fxr ?

Sharpiro commented 5 months ago

Consider the scenario wheredotnetRoot and thus fxrDir is an absolute path, then the hyphen check will use the entire absolute path which may contain unexpected hyphens.

jasongin commented 5 months ago

Oh, I see the problem is that code was assuming Directory.GetDirectories() returns just the simple directory names, but it returns full paths. I'll fix that.

jasongin commented 5 months ago

Fixed in version 0.7.8. Packages should be published within a couple hours.