MSBuildLocator
MSBuildLocator copied to clipboard
MSBuildLocator fails in macOS GUI apps due to missing /usr/local/share/dotnet in PATH
On macOS, GUI applications launched via launchd (such as apps opened from Finder or the Dock) do not inherit the shell's PATH environment variable. As a result, the directory /usr/local/share/dotnet is typically missing from the PATH, causing MSBuildLocator to fail to find .NET installations.
System.InvalidOperationException: Path to dotnet executable is not set. The probed variables are: DOTNET_ROOT, DOTNET_HOST_PATH, DOTNET_MSBUILD_SDK_RESOLVER_CLI_DIR and PATH. Make sure, that at least one of the listed variables points to the existing dotnet executable.
at Microsoft.Build.Locator.DotNetSdkLocationHelper.ResolveDotnetPathCandidates()
at Microsoft.Build.Locator.DotNetSdkLocationHelper.<>c.<.cctor>b__17_0()
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at Microsoft.Build.Locator.DotNetSdkLocationHelper.<GetInstances>g__GetSdkFromGlobalSettings|5_2(String workingDirectory)
at Microsoft.Build.Locator.DotNetSdkLocationHelper.GetInstances(String workingDirectory, Boolean allowQueryAllRuntimes, Boolean allowAllDotnetLocations)+MoveNext()
My current workaround:
private static void FixMacosPath()
{
var processStartInfo = new ProcessStartInfo
{
FileName = "/bin/zsh",
ArgumentList = { "-l", "-c", "printenv PATH" },
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
using var process = Process.Start(processStartInfo);
var output = process!.StandardOutput.ReadToEnd().Trim();
process.WaitForExit();
Environment.SetEnvironmentVariable("PATH", output, EnvironmentVariableTarget.Process);
}
Ideally the shell PATH, or another relevant environment variable would be set for GUI apps.