emacs

2025-07-22T14:17:57.256169Z

Does anyone use .dir-locals regularly/extensively? I feel like I get the concept but every time I try to use it to do something basic it ends up taking an hour due to something dumb on my part like having the data structure wrong, but also reloading on active buffers is very confusing, and the security warnings I seemed to have issues with (like I thought I would tell it to always trust some variable but it will still prompt me every time)? It's functionality I want but always struggle with. Is this something I just haven't learned properly or is it just one of the bad parts of emacs and I should use an alternative/roll my own thing?

dpsutton 2025-07-22T14:19:18.755779Z

agree it can be complicated

👀 1
ag 2025-07-22T15:10:27.095719Z

I have this in my config, I can't say how reliable it is, haven't had to use it for a long time:

(defun +reload-dir-locals (proj)
  "Read values from the current project's .dir-locals file and
apply them in all project file buffers as if opening those files
for the first time.

Signals an error if there is no current project."
  (interactive (list (project-current)))
  (unless proj
    (user-error "There doesn't seem to be a project here"))
  ;; Load the variables; they are stored buffer-locally, so...
  (hack-dir-local-variables)
  ;; Hold onto them...
  (let ((locals dir-local-variables-alist))
    (dolist (buffer (buffer-list))
      (with-current-buffer buffer
        (when (and (equal proj (project-current))
                   buffer-file-name)
          ;; transfer the loaded values to this buffer...
          (setq-local dir-local-variables-alist locals)
          ;; and apply them.
          (hack-local-variables-apply))))))

Ed 2025-07-22T15:19:05.380179Z

I tend to use find-alternative-file (C-x C-v by default I think) to close and reopen the buffer to reload dir-locals, and ibuffer when it's working 😉

Ed 2025-07-22T15:19:43.011989Z

but I agree - it's difficult to get right.

Ovi Stoica 2025-07-22T18:32:34.317899Z

I just change something in the .dir-local and then just M-x revert-buffer. This usually loads again the .dir-locals. You can check the variables you altered with C-h v changed-variable and inspect if it has the correct new value. Most of the time, all you need is to tell cider what aliases to use on startup and what deps tool

((nil . ((cider-preferred-build-tool       . clojure-cli)
         (cider-clojure-cli-aliases        . ":dev:test:with-examples:clj-reload")
         (cider-clojure-cli-parameters     . "--port 8000"))))
In this case, if I changed the aliases used at startup, after the change, I’d do revert-buffer -> C-h v cider-clojure-cli-aliases to check if the cider variable has the new desired value. I also had difficulties with .dir-locals.el management in the past but now it is fairly stable. Note here: When you change things in .dir-locals, it is important to call M-x revert-buffer from the clojure buffer from which you will start cider (let’s say) Here’s another (hard earned) example of starting cider with both clj & cljs repls at the same time
((nil . ((cider-preferred-build-tool       . clojure-cli)
         (cider-clojure-cli-aliases        . ":dev:test:snitch")
         (cider-clojure-cli-parameters     . "--port 7888")
         (cider-default-cljs-repl          . custom)
         (cider-custom-cljs-repl-init-form . "(do (user/cljs-repl))")
         (eval . (progn
                   (make-variable-buffer-local 'cider-jack-in-nrepl-middlewares)
                   (add-to-list 'cider-jack-in-nrepl-middlewares "shadow.cljs.devtools.server.nrepl/middleware"))))))
Appologies if some of the things I said are obvious or basic. It’s good to cover all the ground 😄

dpsutton 2025-07-22T18:37:37.125779Z

i think that’s what the hack-local-variables trick does above. reloads these. i always make sure to eval one in a buffer covered by this to see if it has the new definitions. c-shift-m cider-preferred-build-tool and see what you get

2025-07-22T18:52:34.788189Z

Thanks all, very helpful!

practicalli-johnny 2025-07-23T07:37:07.266719Z

I have a .dir-locals in every project but its usually quite simple

((clojure-mode . ((cider-preferred-build-tool . clojure-cli)
                  (cider-clojure-cli-aliases . ":test/env:dev/reloaded"))))
I found it https://practical.li/spacemacs/clojure-development/project-configuration/ but I assume its fairly easy to overcomplicate it 🤣

👍 2
Sam 2025-07-23T11:25:15.295829Z

@jjttjj have you used M-x add-dir-local-variable and other nice helper functions?

👍 1