Subject: bug#63300: 29.0.90; Tramp keeps customizing variables on every connection

From: Michael Albinus <[email-masked]>
Date: Tue, 06 Jun 2023 13:46:52 +0200

Jimmy Wong <[email-masked]> writes:

Hi,

> Actually, I take it back. customize-save-variable only saves variables
> that have the saved-value symbol property set. This property is
> normally set by customize when it is saved to the custom file.
> Something in Tramp is setting this property without saving the
> variable values to the custom file or going through customize. In
> fact, you can see it via M-x customize-option
> connection-local-criteria-alist. The value will be shown as SAVED.

This property is set as well in custom-set-variables.

> In addition, setting `enable-connection-local-variables` to nil early
> in early-init.el has no effect, the variable is still set on load.

This is a different game. enable-connection-local-variables has no
effect on setting / saving connection-local-criteria-alist and
connection-local-profile-alist. e-c-l-v is used only in
hack-connection-local-variables in order to control, whether
connection-local variables shall be set as buffer-local variables. And
the documentation is clear about, that it isn't intended to be used on
user level, see (info "(elisp) Applying Connection Local Variables")

--8<---------------cut here---------------start------------->8---
 -- Variable: enable-connection-local-variables
     If ‘nil’, connection-local variables are ignored.  This variable
     shall be changed temporarily only in special modes.
--8<---------------cut here---------------end--------------->8---

> Both of this behavior violate what is documented in the docstring and
> https://www.gnu.org/software/emacs/manual/html_node/elisp/Standard-Properties.html.

No.

> If you must, enable-conneciton-local-variables should be respected,
> and if you must, set the theme-value property on these
> connection-local variables instead of saved-value so custom-save-all
> will not save them.

No.

In connection-local-set-profiles and connection-local-set-profile-variables
it should be checked, whether the saved-value property is set. If not,
it shall be reset to nil, after custom-set-variables has been called.

The appended patch shall do the trick. Could you, please, test?

Best regards, Michael.

diff --git a/lisp/files-x.el b/lisp/files-x.el
index 548d9efc193..bae535da550 100644
--- a/lisp/files-x.el
+++ b/lisp/files-x.el
@@ -674,15 +674,18 @@ connection-local-set-profiles
   (dolist (profile profiles)
     (unless (assq profile connection-local-profile-alist)
       (error "No such connection profile `%s'" (symbol-name profile))))
-  (let* ((criteria (connection-local-normalize-criteria criteria))
+  (let* ((saved-value (get 'connection-local-criteria-alist 'saved-value))
+         (criteria (connection-local-normalize-criteria criteria))
          (slot (assoc criteria connection-local-criteria-alist)))
     (if slot
         (setcdr slot (delete-dups (append (cdr slot) profiles)))
       (setq connection-local-criteria-alist
             (cons (cons criteria (delete-dups profiles))
-		  connection-local-criteria-alist))))
-  (custom-set-variables
-   `(connection-local-criteria-alist ',connection-local-criteria-alist now)))
+		  connection-local-criteria-alist)))
+    (custom-set-variables
+     `(connection-local-criteria-alist ',connection-local-criteria-alist now))
+    (unless saved-value
+      (put 'connection-local-criteria-alist 'saved-value nil))))

 (defsubst connection-local-get-profile-variables (profile)
   "Return the connection-local variable list for PROFILE."
@@ -701,9 +704,12 @@ connection-local-set-profile-variables
 variables are set in the server's process buffer according to the
 VARIABLES list of the connection profile.  The list is processed
 in order."
-  (setf (alist-get profile connection-local-profile-alist) variables)
-  (custom-set-variables
-   `(connection-local-profile-alist ',connection-local-profile-alist now)))
+  (let ((saved-value (get 'connection-local-profile-alist 'saved-value)))
+    (setf (alist-get profile connection-local-profile-alist) variables)
+    (custom-set-variables
+     `(connection-local-profile-alist ',connection-local-profile-alist now))
+    (unless saved-value
+      (put 'connection-local-profile-alist 'saved-value nil))))

 ;;;###autoload
 (defun connection-local-update-profile-variables (profile variables)