Skip to content

Commit

Permalink
Don't try and use old runtimes (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smaug123 authored Jun 9, 2024
1 parent a201cf4 commit 5720b97
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@ let info = DotnetEnvironmentInfo.Get ()
// or, if you already know a path to the `dotnet` executable...
let info = DotnetEnvironmentInfo.Get "/path/to/dotnet"
```

## Troubleshooting

If you have a *very* strange setup, we may be unable to locate the `libhostfxr` library we use to find the runtimes.
In that case, you can supply the environment variable `WOOFWARE_DOTNET_LOCATOR_LIBHOSTFXR`,
which should be a full path to a `libhostfxr` DLL on your system.
(Normally this is in `/usr/share/dotnet/host/fxr/{runtime}/libhostfxr.so`; you must make sure your version is from runtime 6 or greater, because the required symbols were not added until then.)
20 changes: 19 additions & 1 deletion WoofWare.DotnetRuntimeLocator/DotnetEnvironmentInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ public record DotnetEnvironmentInfo(
{
private static readonly Lazy<FileInfo> HostFxr = new(() =>
{
switch (Environment.GetEnvironmentVariable("WOOFWARE_DOTNET_LOCATOR_LIBHOSTFXR"))
{
case null:
break;
case var s:
{
return new FileInfo(s);
}
}

// First, we might be self-contained: try and find it next to us.
var selfContainedAttempt = Directory.GetParent(Assembly.GetExecutingAssembly().Location);
if (selfContainedAttempt != null)
Expand All @@ -42,7 +52,15 @@ public record DotnetEnvironmentInfo(
var parent3 = parent2.Parent ??
throw new Exception("Unable to locate the host/fxr directory in the .NET runtime");
var fxrDir = new DirectoryInfo(Path.Combine(parent3.FullName, "host", "fxr"));
return fxrDir.EnumerateDirectories().First().EnumerateFiles("*hostfxr*").First();
Func<DirectoryInfo, bool> isAcceptableName =
di =>
{
// Until net6, libhostfxr did not contain the entrypoint we use, and I can't be bothered to reimplement
// it on those runtimes. I'm just going to assume you have no runtimes earlier than 3 installed.
return !di.Name.StartsWith("3.", StringComparison.Ordinal) &&
!di.Name.StartsWith("5.", StringComparison.Ordinal);
};
return fxrDir.EnumerateDirectories().First(isAcceptableName).EnumerateFiles("*hostfxr*").First();
});

private static FileInfo ResolveAllSymlinks(FileInfo f)
Expand Down

0 comments on commit 5720b97

Please sign in to comment.