We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 2243

Summary: SDLMain.m in OSX 10.9 fails to set working directory
Product: SDL Reporter: Jason Rohrer <jasonrohrer>
Component: mainAssignee: Ryan C. Gordon <icculus>
Status: RESOLVED FIXED QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: icculus
Version: 1.2.15   
Hardware: x86   
OS: Other   

Description Jason Rohrer 2013-11-15 23:32:06 UTC
This is a change to the way OSX sets argv in 10.9 (Mavericks) that breaks the way SDLMain.m (cocoa stub code) sets the app's current working directory.


In 10.8 or older, OSX would pass a "-psn_XXXX" style argument to the app if the app was launched by the Finder, and would NOT pass such an argument if the app was launched from the Terminal.

Starting in 10.9, this behavior has changed, in that the "-psn" argument is now omitted when launching from the Finder in most cases (quarantined apps still get the -psn flag, strangely).


The SDLMain.m file uses this -psn argument to detect the difference between a Finder and Terminal launch, and then goes on to use this detection to either switch the working directory (Finder) or not switch it (Terminal).

SDL Apps that depend on their working directory being set properly break in 10.9 because of this (essentially, all of my games!).

See line 360 here where the -psn flag is checked:

http://hg.libsdl.org/SDL/file/fba40d9f4a73/Xcode/TemplatesForXcodeSnowLeopard/SDL%20Cocoa%20Application/SDLMain.m


See line 292 where gFinderLaunch is used to either set the working directory or not.

This may also break other behavior, like drag-and-dropped files being passed in as arguments to the app, but I'm not sure.  That's the other thing that uses gFinderLaunch.


I'm also not sure how this should be fixed, because you don't ALWAYS want to set the working dir.... in the case of a true Terminal launch, you'd want to leave the working dir unchanged.

At some point, OSX started defaulting every Finder app's working dir to "/" which is pretty useless.


Also, I looked at the 2.0.1 source, and I don't see this handled anywhere in there... so are you you just leaving the "/" set as the cwd on Mac in 2.0.1?
Comment 1 Ryan C. Gordon 2013-11-15 23:42:06 UTC
I'll fix this tonight.

--ryan.
Comment 2 Ryan C. Gordon 2013-11-16 01:49:10 UTC
(In reply to Jason Rohrer from comment #0)
> Also, I looked at the 2.0.1 source, and I don't see this handled anywhere in
> there... so are you you just leaving the "/" set as the cwd on Mac in 2.0.1?

This was sort of an accidental quirk of SDL 1.2 on the Mac; no other platform changed the working directory (as far I as recall), so when we got rid of SDLmain for SDL2, this functionality went away with it.

That being said, in SDL 2.0.1, you can do this at the start of your own main()...

    chdir(SDL_GetBasePath());  // with error checking, of course.

...which should work consistently on all platforms to figure out where your app is running.

Be sure to read the Remarks section on this, though, since you can tweak what one considers the "base" directory on Mac OS X...

    https://wiki.libsdl.org/SDL_GetBasePath

--ryan.
Comment 3 Ryan C. Gordon 2013-11-16 03:46:26 UTC
This issue in SDL 1.2 is now fixed here:

    https://hg.libsdl.org/SDL/rev/cfec3f86fba7

Unfortunately, being part of SDLmain, you'll need to recompile your app with the updated SDLmain.m (or the static library containing it) to pick up the fix, instead of just dropping in a new SDL.

This would be a good time to recommend you move to SDL2!  :)  If you want to do that at some point, and want help with that, drop me an email: icculus@icculus.org

--ryan.