macOS Incorrect Working Directory

I’m in the process of porting my game to macOS. I’m cross compiling from a DesktopGL application built in .NET Framework 4.7.2 with the mono-5.18.0-osx-10.9-x64 runtime via mkbundle. On Windows and Linux everything is okay, but on macOS it has an incorrect working directory only when double-clicking to run the executable. If I run it via the terminal, it’s fine. The incorrect working directory causes it to crash since it’s looking in the wrong place to load SDL or any other native libraries/files.

I’ve tried about everything:

  • System.IO.Directory.GetCurrentDirectory() - home folder
  • System.AppContext.BaseDirectory - home folder
  • System.Diagnostics.Process.GetCurrentProcess().StartInfo.WorkingDirectory - empty string
  • System.IO.Path.GetDirectoryName(typeof(CrashHandler).Assembly.Location - empty string
  • AppDomain.CurrentDomain.DynamicDirectory - empty string
  • System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) - home folder

The actual executable is in ~/Desktop/Folder, but everything either points to the home folder or is an empty string for some reason beyond me. For reference, I’m calling all of these in Main() in Program.cs (nowhere else would work due to the crash). Any ideas on fixing this issue?

I managed to partly resolve this by calling macOS’ proc_pidpath using the process ID, so now the game starts. However, MonoGame is attempting to load content using AppDomain.CurrentDomain.BaseDirectory which is still wrong. Here’s what I tried after I got the path:

Environment.CurrentDirectory = path;
AppDomain.CurrentDomain.SetupInformation.ApplicationBase = path;

Unfortunately, this did not seem to change the BaseDirectory or another AppDomain was loaded, causing content to still fail.

I got it working by using MonoKickstart instead, which didn’t have the same issue.