Xcode custom working directory ignored

It appears that macOS applications are ignoring the current working directory set by Xcode. Here is a simple Xcode project that demonstrates the problem: https://www.ultraengine.com/downloads/xcodeworkingdir.zip

The application starting directory retrieved by NSFileManager:currentDirectoryPath is always "/Users/josh/Library/Containers/whatever.test/Data" even though in the product schema I have a custom working directory set to $(PROJECT_DIR).

Tested on macOS Catalina with Xcode 12.2 and 11.3.1.

If I change the current directory using either NSFileManager:changeCurrentDirectoryPath or _wchdir, both functions succeed, but a following call to get the current directory fails. If getcwd is used, it produces errno 1 (operation not permitted), and NFFileManager.currentDicrectoryPath is equal to nil.

The Xcode project location is in a subfolder of the user Documents directory.

I enabled full disk access to the application in the security and privacy system settings, but it seems to make no difference.

Additionally, I have observed that fopen fails to read a file in the target directory. To me, this indicates some security feature may be blocking the program from accessing the hard drive, even though I have full disk access enabled for this application.

Now it's getting really weird. If I add an OpenPanel in the application code and use that to select a file when the application runs, a subsequent call to fopen will successfully open the file! If the OpenPanel cancel button is pressed without selecting that file, or if a different file is selected, then fopen will return NULL.

Xcode custom working directory ignored
 
 
Q