1

On my ArchLinux number cruncher I have two accounts: A user account (benj) and the root account.

For administrative tasks I ssh into the machine using my user account. With the shell open, I switch to root using su .

Running emacs now shows an error that something went wrong during initialisation

Warning (initialization): An error occurred while loading ‘/home/benj/.emacs.d/init.el’:

File is missing: Cannot open load file, No such file or directory, ~/.emacs.d/init-modules/emacs-lisp-package-archive.el

To ensure normal operation, you should investigate and remove the cause of the error in your initialization file.  Start Emacs with the ‘--debug-init’ option to view a complete error backtrace.

My ~benj/.emacs.d/init.el is modular while ~root/.emacs.d/init.el is not. I don't understand why emacs is trying to load my user's configuration file.

echo $HOME              # Shows /root
alias | grep emacs      # Shows nothing
which emacs             # /user/bin/emacs
emacs --version         # GNU Emacs 26.1
                        # Copyright (C) 2018 Free Software Foundation, Inc.
                        # GNU Emacs comes with ABSOLUTELY NO WARRANTY.
                        # You may redistribute copies of GNU Emacs
                        # under the terms of the GNU General Public License.
                        # For more information about these matters, see the file named COPYING.

However, when logging into root on TTY1 , M-x describe-variable on user-init-file returns the expected /root/.emacs.d/init.el

Drew
  • 29,895
  • 7
  • 74
  • 104
Benj
  • 889
  • 1
  • 14
  • 31
  • _I don't understand why emacs is trying to load my user's configuration file_. Did you mean _it is trying to load the root config file_? You've said the root does not contain the `init-modules` folder, right? The error says emacs cannot find the `init-modules` folder, basically. – mihai May 16 '19 at 11:52
  • The error message says: `An error occurred while loading '/home/benj/.emacs.d/init.el` . so he is trying to load the configuration file for user `benj` instead of the file for `root` . However, he loads the configuration for `root` when logging in through TTY1 – Benj May 16 '19 at 12:21

1 Answers1

1

C-hig (emacs)Find Init says:

How Emacs Finds Your Init File

Normally Emacs uses the environment variable ‘HOME’ (*note HOME: General Variables.) to find ‘.emacs’; that’s what ‘~’ means in a file name. If ‘.emacs’ is not found inside ‘~/’ (nor ‘.emacs.el’), Emacs looks for ‘~/.emacs.d/init.el’ (which, like ‘~/.emacs.el’, can be byte-compiled).

However, if you run Emacs from a shell started by ‘su’, Emacs tries to find your own ‘.emacs’, not that of the user you are currently pretending to be. The idea is that you should get your own editor customizations even if you are running as the super user.

More precisely, Emacs first determines which user’s init file to use. It gets your user name from the environment variables ‘LOGNAME’ and ‘USER’; if neither of those exists, it uses effective user-ID. If that user name matches the real user-ID, then Emacs uses ‘HOME’; otherwise, it looks up the home directory corresponding to that user name in the system’s data base of users.

phils
  • 71,335
  • 11
  • 153
  • 198
  • How does emacs even do that? – Benj May 19 '19 at 03:13
  • I'm not sure I understand the question. Are you asking about something which isn't detailed in the final paragraph of the quote? – phils May 19 '19 at 05:21
  • this is very annoying. I use path with ~ in my user init file so after su root, emacs loads the user init file but expands ~ to root so it gets all the paths wrong :( – knarf Oct 16 '19 at 13:30
  • @knarf Are you saying that the above-quoted documentation is incorrect in your case? Which version of Emacs? Does the manual in your version say the same things that I've quoted? Have you checked all of the environment variables mentioned, after using `su`? – phils Oct 16 '19 at 18:41