1

I'm using Emacs 28.2 on Debian 12.1 with just some org-mode settings in the init file, and it suddenly produces an eln-cache/ every time I start it and populates it with a lot of files. It's annoying as I regularly synchronise my ~/.emacs.d/ across a number of machines.

I checked my init.el but there is no reason for this behaviour to be found.

What is an eln-cache/, and is there a way to get rid of it again?

shynur
  • 4,065
  • 1
  • 3
  • 23
luciljus
  • 11
  • 2

1 Answers1

2

The eln-cache directory stores the native-compiled *.eln files for your configuration's *.el files.

This feature was added in Emacs 28.1, and is optional at build-time. It is not enabled by default, so you have either installed a version of Emacs which someone else built with this feature enabled, or else you have compiled Emacs yourself with this feature enabled.

If you install a version of Emacs which was not built --with-native-compilation then no *.eln files will be generated anywhere.


If you still want native-compilation, then I suggest something along the lines of:

  • Exit Emacs
  • mv ~/.emacs.d/eln-cache ~/.eln-cache
  • ln -s ../.eln-cache ~/.emacs.d/eln-cache
  • Start Emacs

Assuming that when you synchronise your config to another machine the symlink gets copied as a symlink.

You would need to create ~/.eln-cache on each of your machines, of course. (I'm not actually sure what Emacs will do in that situation if eln-cache is a broken symlink.)


Starting from Emacs 29, you can alternatively call the following function in your early-init.el file to move the cache outside of the directory that you are synchronising:

(defun startup-redirect-eln-cache (cache-directory)
  "Redirect the user's eln-cache directory to CACHE-DIRECTORY.
CACHE-DIRECTORY must be a single directory, a string.
This function destructively changes `native-comp-eln-load-path'
so that its first element is CACHE-DIRECTORY.  If CACHE-DIRECTORY
is not an absolute file name, it is interpreted relative
to `user-emacs-directory'.
For best results, call this function in your early-init file,
so that the rest of initialization and package loading uses
the updated value."
  ;; Remove the original eln-cache.
  (setq native-comp-eln-load-path (cdr native-comp-eln-load-path))
  ;; Add the new eln-cache.
  (push (expand-file-name (file-name-as-directory cache-directory)
                          user-emacs-directory)
        native-comp-eln-load-path))

I've included the definition in full in case you want to experiment with that in Emacs 28. (I haven't tried doing that, so I have no idea whether or not it would work.)

phils
  • 48,657
  • 3
  • 76
  • 115
  • Check to see if there's an alternative package without it. If not, there *should* be, as there are scenarios (including very low-powered machines and the presence of certain virus scanners) where native-compilation is problematic. So chase that up if necessary. I suspect there may already be an alternative package though. – phils Jul 26 '23 at 09:59
  • Thanks, with that help I found the relating bug report at Debian. Obviously it won't be fixed. https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1041767&mboxmaint=no – luciljus Jul 26 '23 at 10:04
  • I suggest raising another one requesting an alternative package without native-compilation. They might say no, but it doesn't hurt to ask. They should understand that some of their users may be using such low-powered hardware that native-compilation is not reasonable (I saw a report from one user who estimated that their machine would take an entire day to finish native-compiling everything, and the traditional byte-code interpreter was good enough, so the performance hit of native-comp was insane for them. They weren't running Debian but the principle is the same). – phils Jul 26 '23 at 11:02
  • It's really a mess, because all my workflow is based on Emacs and its org-mode. So far there is no alternative package. As anticipated, the workaround with a softlink breaks during synchronisation and is, hence, no solution. Applying `(defun startup-redirect-eln-cache (cache-directory)` has no effect, Emacs 28.1 still creates `.emacs.d/eln-cache` and populates it with files. – luciljus Jul 27 '23 at 09:28
  • You might use https://github.com/kelleyk/ppa-emacs as an interim solution, but that hasn't been updated since Emacs 28.1 unfortunately. It has a non-native-comp debian package though. Otherwise you can always start compiling Emacs yourself -- once you have it figured out it's pretty easy to use that approach everywhere you go. Or figure out how to adjust your sync process so that it will ignore that directory. – phils Jul 27 '23 at 19:44
  • As anticipated, the maintainers declined the plea to provide a package without `--with-native-compilation`. For me, this is a rather sad lesson in Unix principles, because a complex programme uniting different functions finally broke my neck. The advantages of Emacs and org-mode are simply vanishing, when I have to take care of an `eln-cache` prior to every synchronisation. – luciljus Jul 29 '23 at 05:54
  • Sorry to hear that. I disagree with their stance, but in the end it's their call. FWIW I imagine there's a reasonable solution to synchronising everything except that directory, so you could always ask a question about that on superuser.com. If you're syncing with `rsync` for example, it's trivial to add an `--exclude` pattern. – phils Jul 29 '23 at 06:49
  • I managed to exclude the path from syncing. Is there an easy way to clear the cache (with a keystroke or so) from time to time? They mentioned `M-x native-compile-prune-cache` but Emacs 28.1 doesn't know it. – luciljus Aug 07 '23 at 10:59
  • I don't know whether it's ok to use the code for that command in 28.1, but also it doesn't clear the whole cache; its purpose is to "Remove .eln files that aren’t applicable to the current Emacs invocation." But honestly, if syncing the directory is no longer a concern then I wouldn't worry about its contents too much -- if you delete it all, you'll just cause Emacs to burn loads of CPU to recreate the .eln files that it still wants. I'd suggest on the occasions when you upgrade Emacs itself that you manually delete it, and otherwise maybe just leave it. – phils Aug 07 '23 at 11:21
  • Thanks, I will learn how to compile my own package, seems the most meaningful solution to me. – luciljus Aug 31 '23 at 07:40