Xcode allows to set the CWD from its scheme options, but it will just set the PWD environment variable, and if you set a custom directory there, and execute File::getCurrentWorkingDirectory it will return an empty File() because getcwd will return nullptr.
I’ve extended the function (for posix) to take into account the PWD env variable when getcwd fails:
There are cases where getcwd returning nullptr is the correct thing to do, like if the directory you are currently in has been deleted. Silently falling back to PWD after a long chain of chdirs would be very surprising.
Unfortunately I can’t think of an automatic way around this.
Yeah indeed, i’m experiencing a weird behaviour in Xcode where you override the CWD in the options for the binary, then when the app starts, getpwd returns nullptr and chdir consistently fails, so it’s impossible to get or set the CWD, and that one is only available in the PWD env variable. By default when you launch debugging from xcode without setting the cwd, the apps is set to start from /, so i wanted to change that to be able to be executed from a writable directory at least. Not sure how to workaround it tho
I opted for a specific environment variable, so my app can test it and set the working directory to that variable instead of using Xcode current directory, because that is preventing getCurrentWorkingDirectory and setAsCurrentWorkingDirectory to work:
juce::File currentWorkingDirectory = juce::File(juce::SystemStats::getEnvironmentVariable("MYAPP_PWD", {}).trim());
if (currentWorkingDirectory.isDirectory())
currentWorkingDirectory.setAsCurrentWorkingDirectory();