caldwell / build-emacs

Build scripts for www.emacsformacosx.com
http://www.emacsformacosx.com/about
GNU General Public License v3.0
364 stars 61 forks source link

When Emacs is launched from Finder C-x C-f defaults to / directory #98

Closed lange1 closed 3 years ago

lange1 commented 4 years ago

Running on Mojave. Just upgraded from 26.3 to 27.1.

When I launch Emacs from the GUI (e.g., Finder or Spotlight) and do a C-x C-f the default directory is now / instead of my home directory.

Under 26.3 and before, doing the above defaulted to ~/ my home directory.

I have made no configuration changes other than the package upgrade (through homebrew).

imrehorvath commented 4 years ago

Yes, but you can set the default-directory variable as you like, eg. to be your home directory. I did the following in my ~/.emacs.d/init.el

;; Set default directory to user home (setq default-directory (concat (getenv "HOME") "/"))

lange1 commented 4 years ago

The drawback of using default-directory is, if I'm using emacs from a command line (e.g., emacs -nw) then I actually want the default directory to be the current working directory, not hard-coded to ~/.

Can I assume that you didn't change anything in your build scripts between 26.3 and 27.1 that would cause this? In that case, I think my issue is upstream.

imrehorvath commented 4 years ago

I'm not the maintainer, just a plain user experienced the same issue like you did, so to answer your question, I didn't change anything in the build scripts.

Since I don't really use it from the command line, the explicit setting of default-directory has solved the issue for me. But you're right, this solution will definitely do wrong if you want to use it from the command line.

Yes, I think the issue is upstream. Today, I downloaded the tarball from the official GNU Emacs site and built Emacs from the official source, and I can confirm that the vanilla Emacs version 27.1 behaves the same! So if you launch the Cocoa app, then it will have "/" as the default directory. However if you run it from the command line eg. emacs -nw, then it will have the cwd as the default directory. So it seems like it has nothing to do with the emacsformacosx build, but with the new Emacs version itself.

lange1 commented 4 years ago

Until I've figured this out, I'm using this in the macOS-specific portion of my config files. It only changes the default directory if it's /, which I feel is an okay compromise as I basically never launch emacs from the root directory.

(if (string= default-directory "/")
    (progn (setq default-directory (concat (getenv "HOME") "/"))
           (setq command-line-default-directory default-directory)))
imrehorvath commented 4 years ago

And I came up with the following workaround for the rare occasions I wan't to run it from the command line.

;; Set the default directory to user home, when we launch the Cocoa app, and the default-directory is "/".
(when (and (eq window-system 'ns)
       (string= default-directory "/"))
  (setq default-directory (concat (getenv "HOME") "/")))

It sets the default-directory only, when we are on macOS, run the Cocoa app and the default-directory is set to "/" at startup. Don't bother setting otherwise.

I'm also curious what has happened in this new version of Emacs. Do you mind posting a short note here is you find out?

rptb1 commented 3 years ago

This issue has also been raised at https://apple.stackexchange.com/questions/399187/how-to-set-default-directory-for-emacs-27-1-app

rptb1 commented 3 years ago

If you launch the app from the command line with /Applications/Emacs.app/Contents/MacOS/Emacs then you get the correct behaviour. Something has changed between 26 and 27 in the way that Emacs inherits its directory from the Finder.

rptb1 commented 3 years ago

Also M-x shell no longer runs my .profile correctly since upgrading to 27. I believe this is part of the same issue, because if I launch it using the command line as mentioned https://github.com/caldwell/build-emacs/issues/98#issuecomment-720579758 then the problem is corrected.

rptb1 commented 3 years ago

If I launch the binary named Emacs-x86_64-10_14 from the application package using the Finder, the directory is correct again, and consistent with 26. This suggests that there is a problem in the Emacs script that selects the binary, whether it has changed, or something else has caused it to work differently.

rptb1 commented 3 years ago

I have worked around this issue by editing /Applications/Emacs.app/Contents/MacOS/Emacs, inserting

Dir.chdir(ENV['HOME'])

before the call to exec at https://github.com/caldwell/build-emacs/blob/0b7456860759418ff5b2ebf309f034c94d0e961f/launch.rb#L69 . This is the simplest consistent workaround I can think of, but isn't a proper bug fix because I don't understand how this has gone wrong between 26 and 27.

rptb1 commented 3 years ago

This looks likely to be the cause of the problem https://github.com/emacs-mirror/emacs/commit/900276502fbb4dcabdabc5d7d24b4bc5645f2cf3

I have sent an Emacs bug report http://debbugs.gnu.org/cgi/bugreport.cgi?bug=44446

rptb1 commented 3 years ago

“Looks like the fix will be in 27.2.” https://debbugs.gnu.org/cgi/bugreport.cgi?bug=44446#11

I think this issue could be closed on https://github.com/caldwell/build-emacs .

lange1 commented 3 years ago

Closing here as the issue seems to be fixed upstream.

caldwell commented 3 years ago

@rptb1 Thanks for tracking this down. I was able to verify that it was broken the other day but didn't have any good info about why. It's good to know it's not a build issue.