Subject: Re: bug#76950: tramp caches non-existing podman containers

From: Michael Albinus <[email-masked]>
Date: Wed, 12 Mar 2025 13:42:50 +0100

Konstantin Kharlamov <[email-masked]> writes:

Hi Konstantin,

> Figured, I'd clarify for people who don't work with containerization:
> the docker and podman completion is about auto-generated names like
> vibrant_hopper, frosty_northcutt, etc. Caching them has no use
> whatsoever, because:

That's not always the case. You can run a container with the
'--name="NAME"' argument.

> 1. The command that receives possible completions always fetches the
> current list, so even if cached containers still exist, the cache only
> wastes space.

Right. It doesn't make sense to offer container names which do not run,
but are cached.

> 2. When containers quit, the names become useless (the bug).

Not always (see above).

> So cache should not be working at all for both Podman and Docker.

Well, I've assembled a patch with disables completion of cached container
names for the docker, dockercp, podman and podmancp methods. See
appended. Could you, pls test?

It can be extended for other methods easily, which are offered by
tramp-container.el.

Best regards, Michael.

diff --git a/lisp/tramp-cache.el b/lisp/tramp-cache.el
index 2694b8f4..06b47fa6 100644
--- a/lisp/net/tramp-cache.el
+++ b/lisp/net/tramp-cache.el
@@ -86,6 +86,8 @@
 (require 'tramp-compat)
 (require 'time-stamp)

+(declare-function tramp-get-method-parameter "tramp")
+
 ;;; -- Cache --

 ;;;###tramp-autoload
@@ -635,15 +637,16 @@ your laptop to different networks frequently."
   "Return a list of (user host) tuples allowed to access for METHOD.
 This function is added always in `tramp-get-completion-function'
 for all methods.  Resulting data are derived from connection history."
-  (and tramp-completion-use-cache
-       (mapcar
-	(lambda (key)
-	  (and (tramp-file-name-p key)
-	       (string-equal method (tramp-file-name-method key))
-	       (not (tramp-file-name-localname key))
-	       (list (tramp-file-name-user key)
-		     (tramp-file-name-host key))))
-	(hash-table-keys tramp-cache-data))))
+  (mapcar
+   (lambda (key)
+     (and (tramp-file-name-p key)
+	  (string-equal method (tramp-file-name-method key))
+	  (not (tramp-file-name-localname key))
+	  (tramp-get-method-parameter
+	   key 'tramp-completion-use-cache tramp-completion-use-cache)
+	  (list (tramp-file-name-user key)
+		(tramp-file-name-host key))))
+   (hash-table-keys tramp-cache-data)))

 ;; When "emacs -Q" has been called, both variables are nil.  We do not
 ;; load the persistency file then, in order to have a clean test environment.
diff --git a/lisp/tramp-container.el b/lisp/tramp-container.el
index 0f1ce71c..64965ae0 100644
--- a/lisp/net/tramp-container.el
+++ b/lisp/net/tramp-container.el
@@ -556,7 +556,8 @@ see its function help for a description of the format."
 		(tramp-direct-async (,tramp-default-remote-shell "-c"))
                 (tramp-remote-shell ,tramp-default-remote-shell)
                 (tramp-remote-shell-login ("-l"))
-                (tramp-remote-shell-args ("-i" "-c"))))
+                (tramp-remote-shell-args ("-i" "-c"))
+		(tramp-completion-use-cache nil)))

  (add-to-list 'tramp-methods
               `(,tramp-dockercp-method
@@ -573,7 +574,8 @@ see its function help for a description of the format."
 		(tramp-copy-program ,tramp-docker-program)
 		(tramp-copy-args (("cp")))
 		(tramp-copy-file-name (("%h" ":") ("%f")))
-                (tramp-copy-recursive t)))
+                (tramp-copy-recursive t)
+		(tramp-completion-use-cache nil)))

  (add-to-list 'tramp-methods
               `(,tramp-podman-method
@@ -586,7 +588,8 @@ see its function help for a description of the format."
 		(tramp-direct-async (,tramp-default-remote-shell "-c"))
                 (tramp-remote-shell ,tramp-default-remote-shell)
                 (tramp-remote-shell-login ("-l"))
-                (tramp-remote-shell-args ("-i" "-c"))))
+                (tramp-remote-shell-args ("-i" "-c"))
+		(tramp-completion-use-cache nil)))

  (add-to-list 'tramp-methods
               `(,tramp-podmancp-method
@@ -603,7 +606,8 @@ see its function help for a description of the format."
 		(tramp-copy-program ,tramp-podman-program)
 		(tramp-copy-args (("cp")))
 		(tramp-copy-file-name (("%h" ":") ("%f")))
-                (tramp-copy-recursive t)))
+                (tramp-copy-recursive t)
+		(tramp-completion-use-cache nil)))

  (add-to-list 'tramp-methods
               `(,tramp-kubernetes-method