Skip to content

MSBuildLocator fails in macOS GUI apps due to missing /usr/local/share/dotnet in PATH #361

@MattParkerDev

Description

@MattParkerDev

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions