Bug 13930 - Directory.GetCurrentDirectory() returns "/" for Xamarin.Mac apps
Summary: Directory.GetCurrentDirectory() returns "/" for Xamarin.Mac apps
Alias: None
Product: Xamarin.Mac
Classification: Desktop
Component: Other ()
Version: 1.4.x
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Aaron Bockover [MSFT]
Depends on:
Reported: 2013-08-12 15:34 UTC by PJ
Modified: 2013-09-26 12:43 UTC (History)
1 user (show)

Is this bug a regression?: ---
Last known good build:

Patch to set PWD to $HOME when running under launcher (2.62 KB, patch)
2013-09-26 11:58 UTC, Aaron Bockover [MSFT]

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description PJ 2013-08-12 15:34:29 UTC
I'm not sure if this is even a bug, but it was a small issue for me when I was running mac-samples/MicroSamples/generate-pdf.cs

It's trying to create a PDF with:

> var pdf = new CGContextPDF (NSUrl.FromFilename("demo.pdf"), new RectangleF (0, 0, 617, 792));

demo.pdf somehow resolves to /demo.pdf, which the application doesn't clearly doesn't have permission to write to.Setting to ~/Users/pjbeaman/WorkWork/demo.pdf works just fine.

So then I tried just using Directory.GetCurrentDirectory(), and that seems to be returning a "/". 

Should Directory.GetCurrentDirectory() return "/" by default for Xamarin.Mac? The same for a console project spits out "/Users/pjbeaman/Projects/waffwfaw/waffwfaw/bin/Debug" as expected. I don't like to bother with invalid bugs if at all possible, but I asked Jeff and he wasn't sure.

This happens with both 1.4.22 and 1.6.9 Xamarin.Mac, so it's not a regression in X.M at least.
Comment 1 Aaron Bockover [MSFT] 2013-09-26 10:44:05 UTC
If running as a .app, I would expect this. There is no current directory because the launcher (e.g. 'open') clears parts of the environment environment (notably, PWD).

When running the process directly, the environment is preserved.

The only thing I can think of is a fix in Mono where we return $HOME when $PWD is unset. This is also not strictly a Xamarin.Mac bug.
Comment 2 Aaron Bockover [MSFT] 2013-09-26 11:39:56 UTC
After some more thought and looking into all the code paths, this really isn't a bug at all. This is expected behavior down to the OS.

Essentially, getcwd returns '/' presumably because the launcher itself (e.g. 'open') called chdir to set it so. That means this is the expected working directory in OS X for .apps that are launched "properly".

So I'm conflicted on what to do: on one hand, '/' is lame, since it's useless so we might as well set it to $HOME. On the other hand, the concept of a current working directory is useless in OS X in the .app context and it just shouldn't be used. So why bother changing the status-quo?
Comment 4 Aaron Bockover [MSFT] 2013-09-26 12:01:39 UTC
Yeah, closing as FEATURE since this behavior is consistent with the rest of the OS and native apps that run on it. There is nothing in Mono or Xamarin.Mac that messes with these expectations.

Applications running from the launcher (open) shouldn't even expect to have a working directory since they are not launched from one in the first place.
Comment 5 PJ 2013-09-26 12:43:55 UTC
Thanks for the explanation. Makes sense. VERIFYING as FEATURE.