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

Open
#361 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
csharp
Domain
build-system

Research direction

Start with Microsoft.Build.Locator.DotNetSdkLocationHelper.ResolveDotnetPathCandidates and the environment-variable probing described in the stack trace. Reproduce the launchd scenario using a macOS GUI app, then verify that MSBuildLocator can find an installed .NET SDK without inheriting the shell PATH; the issue does not name a test file.

Written by the indexing model from the issue text.

Description

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.

Dominant language
C#
Stars
262
Forks
91
Avg merge
11d 22h
Merged PRs (30d)
3

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/MSBuildLocator

All issues in microsoft/MSBuildLocator

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.