Subject: bug#64124: 29.0.91; Loading ‘tramp’ unconditionally loads ‘ls-lisp’

From: Michael Albinus <[email-masked]>
Date: Sat, 17 Jun 2023 16:14:41 +0200

Sebastian Miele <[email-masked]> writes:

> Hi Micheal,

Hi Sebastian,

> the patch does not apply to my Emacs 29 checkout.  I suspect that the
> patch is relative to another repository (probably a/the Tramp repository
> outside of Emacs).

The patch was based on the tramp-2-6-stable branch of the Tramp git
repostory.

Appended is the same patch, but now based on the emacs-29 branch of the
Emacs git repository.

Best regards, Michael.

diff --git a/lisp/net/tramp-compat.el b/lisp/net/tramp-compat.el
index f176476a73a..3d3ba7a4f43 100644
--- a/lisp/net/tramp-compat.el
+++ b/lisp/net/tramp-compat.el
@@ -31,11 +31,13 @@

 (require 'auth-source)
 (require 'format-spec)
-(require 'ls-lisp) ;; Due to `tramp-handle-insert-directory'.
 (require 'parse-time)
 (require 'shell)
 (require 'subr-x)

+(when (memq system-type '(ms-dos windows-nt))
+  (require 'ls-lisp)) ;; Due to `tramp-handle-insert-directory'.
+
 (declare-function tramp-compat-rx "tramp")
 (declare-function tramp-error "tramp")
 (declare-function tramp-file-name-handler "tramp")
diff --git a/lisp/net/tramp-sh.el b/lisp/net/tramp-sh.el
index 392a654df21..48ba3d09e3e 100644
--- a/lisp/net/tramp-sh.el
+++ b/lisp/net/tramp-sh.el
@@ -38,6 +38,7 @@
 (declare-function dired-compress-file "dired-aux")
 (declare-function dired-remove-file "dired-aux")
 (defvar dired-compress-file-suffixes)
+(defvar ls-lisp-use-insert-directory-program)
 ;; Added in Emacs 28.1.
 (defvar process-file-return-signal-string)
 (defvar vc-handled-backends)
@@ -2659,7 +2660,7 @@ tramp-sh-handle-insert-directory
     (access-file filename "Reading directory"))
   (with-parsed-tramp-file-name (expand-file-name filename) nil
     (if (and (featurep 'ls-lisp)
-	     (not (symbol-value 'ls-lisp-use-insert-directory-program)))
+	     (not ls-lisp-use-insert-directory-program))
 	(tramp-handle-insert-directory
 	 filename switches wildcard full-directory-p)
       (when (stringp switches)
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index 7df455954d8..b46eeb8e374 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -63,6 +63,7 @@
 (declare-function file-notify-rm-watch "filenotify")
 (declare-function netrc-parse "netrc")
 (defvar auto-save-file-name-transforms)
+(defvar ls-lisp-use-insert-directory-program)

 ;; Reload `tramp-compat' when we reload `tramp-autoloads' of the GNU
 ;; ELPA package.
@@ -4181,6 +4182,7 @@ tramp-handle-find-backup-file-name
 (defun tramp-handle-insert-directory
   (filename switches &optional wildcard full-directory-p)
   "Like `insert-directory' for Tramp files."
+  (require 'ls-lisp)
   (unless switches (setq switches ""))
   ;; Mark trailing "/".
   (when (and (directory-name-p filename)
@@ -4193,7 +4195,6 @@ tramp-handle-insert-directory
     (with-tramp-progress-reporter v 0 (format "Opening directory %s" filename)
       (let (ls-lisp-use-insert-directory-program start)
 	;; Silence byte compiler.
-	(ignore ls-lisp-use-insert-directory-program)
 	(tramp-run-real-handler
 	 #'insert-directory
 	 (list filename switches wildcard full-directory-p))