Subject: bug#70136: 30.0.50; compilation-mode [was: comint-mode] doesn't call hack-dir-local-variables-non-file-buffer

From: Augusto Stoffel <[email-masked]>
Date: Sun, 14 Apr 2024 11:27:28 +0200

Since compilation buffers go as far as to print the directory they're
running on at the top of the buffer, I think it's pretty clear they
should receive dir-local variables.

So I'd suggest the attached patch, which does that and also removes a
more limited mechanism I added some time ago to allow compilation with
project-specific settings.  I've CC'ed Stefan since at the time he kind
of supported the changes I'm now suggesting to remove.

>From e29f1849c278ebf55aa67470b5f35263ecf989f3 Mon Sep 17 00:00:00 2001
From: Augusto Stoffel <[email-masked]>
Date: Sun, 14 Apr 2024 11:07:02 +0200
Subject: [PATCH] Add dir-local variables to compilation buffers

* lisp/progmodes/compile.el (compilation-mode): Arrange for dir-local
variables to be hacked into to non-file compilation buffers.
(compilation-start): Remove a less flexible solution to set up the
compilation environment (cf. discussion in bug#50607).
---
 lisp/progmodes/compile.el | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)

diff --git a/lisp/progmodes/compile.el b/lisp/progmodes/compile.el
index b18eb81fee1..f81c1edb8c3 100644
--- a/lisp/progmodes/compile.el
+++ b/lisp/progmodes/compile.el
@@ -1929,9 +1929,6 @@ compilation-start
 	    (replace-regexp-in-string "-mode\\'" "" (symbol-name mode))))
 	 (thisdir default-directory)
 	 (thisenv compilation-environment)
-         (buffer-path (and (local-variable-p 'exec-path) exec-path))
-         (buffer-env (and (local-variable-p 'process-environment)
-                          process-environment))
 	 outwin outbuf)
     (with-current-buffer
 	(setq outbuf
@@ -2004,12 +2001,6 @@ compilation-start
         ;; NB: must be done after (funcall mode) as that resets local variables
         (setq-local compilation-directory thisdir)
         (setq-local compilation-environment thisenv)
-        (if buffer-path
-            (setq-local exec-path buffer-path)
-          (kill-local-variable 'exec-path))
-        (if buffer-env
-            (setq-local process-environment buffer-env)
-          (kill-local-variable 'process-environment))
 	(if highlight-regexp
             (setq-local compilation-highlight-regexp highlight-regexp))
         (if (or compilation-auto-jump-to-first-error
@@ -2372,6 +2363,11 @@ compilation-mode
   ;; some other input event happens.
   (setq-local jit-lock-defer-time nil)
   (setq buffer-read-only t)
+  (unless (buffer-file-name)
+    (let ((sym (make-symbol "hack-dir-local-variables-non-file-buffer")))
+      (set sym #'hack-dir-local-variables-non-file-buffer)
+      ;; Ensure hack-dir-locals is called only after a derived mode is set.
+      (push sym delayed-mode-hooks)))
   (run-mode-hooks 'compilation-mode-hook))
 
 ;;;###autoload
-- 
2.44.0