nuke icon indicating copy to clipboard operation
nuke copied to clipboard

UnityTasks GetToolPath fails to resolve Unity in 9.0 (regression)

Open yewnyx opened this issue 1 year ago • 0 comments

Usage Information

9.0.0 / Any / Any / Windows

Description

The latest version of Nuke 8 implemented UnityTasks.GetToolPath() as:

    public static string GetToolPath(string hubVersion = null)
    {
        return hubVersion != null
            ? GetToolPathViaHubVersion(hubVersion)
            : GetToolPathViaManualInstallation();
    }

The first version of Nuke 9 implemented UnityTasks.GetToolPath() as:

    protected override string GetToolPath(ToolOptions options = null)
    {
        var unityOptions = options as UnityOptionsBase;
        var programFiles = EnvironmentInfo.IsWin
            ? EnvironmentInfo.SpecialFolder(EnvironmentInfo.Is32Bit ? SpecialFolders.ProgramFilesX86 : SpecialFolders.ProgramFiles)
            : null;
        return unityOptions?.HubVersion ?? GetEditorVersionFromProject() switch
        {
            { } version => EnvironmentInfo.Platform switch
            {
                PlatformFamily.Windows => $@"{programFiles}\Unity\Hub\Editor\{version}\Editor\Unity.exe",
                PlatformFamily.OSX => $"/Applications/Unity/Hub/Editor/{version}/Unity.app/Contents/MacOS/Unity",
                _ => throw new Exception($"Cannot determine Unity Hub installation path for '{version}'.")
            },
            null => EnvironmentInfo.Platform switch
            {
                PlatformFamily.Windows => $@"{programFiles}\Unity\Editor\Unity.exe",
                PlatformFamily.OSX => "/Applications/Unity/Unity.app/Contents/MacOS/Unity",
                _ => null
            }
        };

The problem is demonstrated with a minimal example:

using System;
					
public class Program
{
	public static void Main()
	{
		var x = "Foo";
		Console.WriteLine(testSwitch(x)); // Outputs "Foo"
	}
	
	public static string testSwitch(string aString) {
	  return aString ?? "Bar" switch {
			  {} something => "asdf " + something,
			  null => "nothing"
	  };
	}
}

Whereas previously setting the HubVersion would invoke GetToolPathViaHubVersion(hubVersion), now GetToolPath() avoids the switch statement and returns HubVersion directly.

Reproduction Steps

            UnityTasks.Unity(_ => _
                .SetHubVersion("6000.0.45f1")
                .SetProjectPath(RootDirectory)
                .SetExecuteMethod(method)

Where method is any static method to be invoked in a build step; it fails prior to that by failing to resolve Unity

Expected Behavior

Expected behavior is that UnityTasks.GetToolPath() resolves the path to the Unity editor version for Windows or OSX as implemented in the { } version branch of the switch case.

Actual Behavior

10:15:44 [ERR] Target SwitchProfile has thrown an exception
System.ArgumentException: Could not find '6000.0.45f1' via 'C:\Windows\System32\where.exe'.
   at Nuke.Common.Assert.NotNull[T](T obj, String message, String argumentExpression) in /_/source/Nuke.Utilities/Assert.cs:line 73
   at Nuke.Common.Tooling.ToolPathResolver.GetPathExecutable(String pathExecutable) in /_/source/Nuke.Tooling/ToolPathResolver.cs:line 57
   at Nuke.Common.Tooling.ProcessTasks.StartProcess(String toolPath, String arguments, String workingDirectory, IReadOnlyDictionary`2 environmentVar
iables, Nullable`1 timeout, Nullable`1 logOutput, Nullable`1 logInvocation, Action`2 logger, Func`2 outputFilter) in /_/source/Nuke.Tooling/ProcessTasks.cs:line 93
   at Nuke.Common.Tooling.ToolTasks.Run[T](T options) in /_/source/Nuke.Tooling/ToolTasks.Run.cs:line 24
   at Nuke.Common.Tools.Unity.UnityTasks.Run[T](T options) in /_/source/Nuke.Common/Tools/Unity/UnityTasks.cs:line 81
   at Nuke.Common.Tools.Unity.UnityTasks.Unity(Configure`1 configurator) in /_/source/Nuke.Common/Tools/Unity/Unity.Generated.cs:line 46
   at Magnet.Build.Build.<get_SwitchProfile>b__6_2() in D:\Projects\MagnetGame\Nuke\Build.cs:line 23
   at Nuke.Common.Execution.BuildExecutor.<>c.<Execute>b__4_2(Action x) in /_/source/Nuke.Build/Execution/BuildExecutor.cs:line 120
   at System.Collections.Generic.List`1.ForEach(Action`1 action)
   at Nuke.Common.Execution.BuildExecutor.Execute(NukeBuild build, ExecutableTarget target, IReadOnlyCollection`1 previouslyExecutedTargets, Boolean failureMode) in /_/source/Nuke.Build/Execution/BuildExecutor.cs:line 120

Regression?

Yes. It works in Nuke 8; it does not work in Nuke 9.

Known Workarounds

Remove .SetHubVersion("6000.0.45f1") and rely on automatic version detection

Could you help with a pull-request?

No

yewnyx avatar Apr 29 '25 01:04 yewnyx