Subject: bug#42836: 27.1; default-directory is set to "/" after startup

From: Paul Eggert <[email-masked]>
Date: Thu, 13 Aug 2020 12:54:00 -0700

On 8/13/20 7:52 AM, Alan Third wrote:
> I suspect the best solution here is to move the code at line
> emacs.c:1623 to before emacs_wd is set. What do you think, Paul?

It's probably safer to leave the code where it is, and to update emacs_wd if 
Emacs chdirs successfully.

Does the attached patch work for you? If so, I can install it into the emacs-27 
branch. (I don't use macOS so can't easily test it myself.)
>From 5bf06fd09669d4b48dc0e3f3f756006d0ca2059b Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email-masked]>
Date: Thu, 13 Aug 2020 12:48:57 -0700
Subject: [PATCH] Fix startup working dir bug on NeXTSTEP

* src/emacs.c (main) [NS_IMPL_COCOA]: Update emacs_wd
after a NS GUI chdirs successfully (Bug#42836).
---
 src/emacs.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/src/emacs.c b/src/emacs.c
index 1dcf195891..11dcdb33fe 100644
--- a/src/emacs.c
+++ b/src/emacs.c
@@ -1638,23 +1638,27 @@ main (int argc, char **argv)
     {
 #ifdef NS_IMPL_COCOA
       /* Started from GUI? */
-      /* FIXME: Do the right thing if get_homedir returns "", or if
-         chdir fails.  */
-      if (! inhibit_window_system && ! isatty (STDIN_FILENO) && ! ch_to_dir)
-        chdir (get_homedir ());
+      bool go_home = (!ch_to_dir && !inhibit_window_system
+		      && !isatty (STDIN_FILENO));
       if (skip_args < argc)
         {
           if (!strncmp (argv[skip_args], "-psn", 4))
             {
               skip_args += 1;
-              if (! ch_to_dir) chdir (get_homedir ());
+	      go_home |= !ch_to_dir;
             }
           else if (skip_args+1 < argc && !strncmp (argv[skip_args+1], "-psn", 4))
             {
               skip_args += 2;
-              if (! ch_to_dir) chdir (get_homedir ());
+	      go_home |= !ch_to_dir;
             }
         }
+      if (go_home)
+	{
+	  char const *home = get_homedir ();
+	  if (*home && chdir (home) == 0)
+	    emacs_wd = emacs_get_current_dir_name ();
+	}
 #endif  /* COCOA */
     }
 #endif /* HAVE_NS */
-- 
2.17.1