Skip to content

Commit e1c93a0

Browse files
committed
Fix problems in Tramp's async-shell-command
* lisp/net/tramp-adb.el (tramp-adb-handle-make-process): * lisp/net/tramp-cache.el (top): * lisp/net/tramp-sh.el (tramp-sh-handle-make-process): Use `insert-file-contents-literally'. * lisp/net/tramp.el (tramp-parse-file): Use `insert-file-contents-literally'. (tramp-handle-shell-command): Reorganize error-buffer handling. (tramp-handle-start-file-process): Use `consp' instead of `listp'. * test/lisp/net/tramp-tests.el (tramp-test31-interrupt-process): Bind `delete-exited-processes'. (tramp--test-async-shell-command): Bind `delete-exited-processes'. Add additional `accept-process-output'. Move cleanup of output buffer ... (tramp-test32-shell-command): ... here. Test error buffer also for `async-shell-command'.
1 parent 5020594 commit e1c93a0

File tree

5 files changed

+61
-47
lines changed

5 files changed

+61
-47
lines changed

lisp/net/tramp-adb.el

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,13 +1065,15 @@ PRESERVE-UID-GID and PRESERVE-EXTENDED-ATTRIBUTES are completely ignored."
10651065
;; until the process is deleted.
10661066
(when (bufferp stderr)
10671067
(with-current-buffer stderr
1068-
(insert-file-contents remote-tmpstderr 'visit))
1068+
(insert-file-contents-literally
1069+
remote-tmpstderr 'visit))
10691070
;; Delete tmpstderr file.
10701071
(add-function
10711072
:after (process-sentinel p)
10721073
(lambda (_proc _msg)
10731074
(with-current-buffer stderr
1074-
(insert-file-contents remote-tmpstderr 'visit))
1075+
(insert-file-contents-literally
1076+
remote-tmpstderr 'visit nil nil 'replace))
10751077
(delete-file remote-tmpstderr))))
10761078
;; Return process.
10771079
p))))

lisp/net/tramp-cache.el

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ for all methods. Resulting data are derived from connection history."
504504
tramp-cache-read-persistent-data)
505505
(condition-case err
506506
(with-temp-buffer
507-
(insert-file-contents tramp-persistency-file-name)
507+
(insert-file-contents-literally tramp-persistency-file-name)
508508
(let ((list (read (current-buffer)))
509509
(tramp-verbose 0)
510510
element key item)

lisp/net/tramp-sh.el

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3004,13 +3004,15 @@ STDERR can also be a file name."
30043004
;; the process is deleted.
30053005
(when (bufferp stderr)
30063006
(with-current-buffer stderr
3007-
(insert-file-contents remote-tmpstderr 'visit))
3007+
(insert-file-contents-literally
3008+
remote-tmpstderr 'visit))
30083009
;; Delete tmpstderr file.
30093010
(add-function
30103011
:after (process-sentinel p)
30113012
(lambda (_proc _msg)
30123013
(with-current-buffer stderr
3013-
(insert-file-contents remote-tmpstderr 'visit))
3014+
(insert-file-contents-literally
3015+
remote-tmpstderr 'visit nil nil 'replace))
30143016
(delete-file remote-tmpstderr))))
30153017
;; Return process.
30163018
p)))

lisp/net/tramp.el

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2844,7 +2844,7 @@ User is always nil."
28442844
(let ((default-directory (tramp-compat-temporary-file-directory)))
28452845
(when (file-readable-p filename)
28462846
(with-temp-buffer
2847-
(insert-file-contents filename)
2847+
(insert-file-contents-literally filename)
28482848
(goto-char (point-min))
28492849
(cl-loop while (not (eobp)) collect (funcall function))))))
28502850

@@ -3699,32 +3699,37 @@ support symbolic links."
36993699
;; Run the process.
37003700
(setq p (start-file-process-shell-command
37013701
(buffer-name output-buffer) buffer command))
3702-
(if (process-live-p p)
3703-
;; Display output.
3704-
(with-current-buffer output-buffer
3705-
(display-buffer output-buffer '(nil (allow-no-window . t)))
3706-
(setq mode-line-process '(":%s"))
3707-
(shell-mode)
3708-
(set-process-filter p #'comint-output-filter)
3709-
(set-process-sentinel
3710-
p (if (listp buffer)
3711-
(lambda (_proc _string)
3712-
(with-current-buffer error-buffer
3713-
(insert-file-contents (cadr buffer)))
3714-
(delete-file (cadr buffer)))
3715-
#'shell-command-sentinel)))
3716-
;; Show stderr.
3702+
;; Insert error messages if they were separated.
3703+
(when (consp buffer)
37173704
(with-current-buffer error-buffer
3718-
(insert-file-contents (cadr buffer)))
3719-
(delete-file (cadr buffer)))))
3705+
(insert-file-contents-literally (cadr buffer))))
3706+
(if (process-live-p p)
3707+
;; Display output.
3708+
(with-current-buffer output-buffer
3709+
(display-buffer output-buffer '(nil (allow-no-window . t)))
3710+
(setq mode-line-process '(":%s"))
3711+
(shell-mode)
3712+
(set-process-filter p #'comint-output-filter)
3713+
(set-process-sentinel p #'shell-command-sentinel)
3714+
(when (consp buffer)
3715+
(add-function
3716+
:after (process-sentinel p)
3717+
(lambda (_proc _string)
3718+
(with-current-buffer error-buffer
3719+
(insert-file-contents-literally
3720+
(cadr buffer) nil nil nil 'replace))
3721+
(delete-file (cadr buffer))))))
3722+
3723+
(when (consp buffer)
3724+
(delete-file (cadr buffer))))))
37203725

37213726
(prog1
37223727
;; Run the process.
37233728
(process-file-shell-command command nil buffer nil)
37243729
;; Insert error messages if they were separated.
3725-
(when (listp buffer)
3730+
(when (consp buffer)
37263731
(with-current-buffer error-buffer
3727-
(insert-file-contents (cadr buffer)))
3732+
(insert-file-contents-literally (cadr buffer)))
37283733
(delete-file (cadr buffer)))
37293734
(if current-buffer-p
37303735
;; This is like exchange-point-and-mark, but doesn't
@@ -3745,10 +3750,10 @@ BUFFER might be a list, in this case STDERR is separated."
37453750
(tramp-file-name-handler
37463751
'make-process
37473752
:name name
3748-
:buffer (if (listp buffer) (car buffer) buffer)
3753+
:buffer (if (consp buffer) (car buffer) buffer)
37493754
:command (and program (cons program args))
37503755
;; `shell-command' adds an errfile to `buffer'.
3751-
:stderr (when (listp buffer) (cadr buffer))
3756+
:stderr (when (consp buffer) (cadr buffer))
37523757
:noquery nil
37533758
:file-handler t))
37543759

test/lisp/net/tramp-tests.el

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4410,6 +4410,7 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
44104410
;; order to establish the connection prior running an asynchronous
44114411
;; process.
44124412
(let ((default-directory (file-truename tramp-test-temporary-file-directory))
4413+
(delete-exited-processes t)
44134414
kill-buffer-query-functions proc)
44144415
(unwind-protect
44154416
(with-temp-buffer
@@ -4436,18 +4437,14 @@ This tests also `make-symbolic-link', `file-truename' and `add-name-to-file'."
44364437
(command output-buffer &optional error-buffer input)
44374438
"Like `async-shell-command', reading the output.
44384439
INPUT, if non-nil, is a string sent to the process."
4439-
(let ((proc (async-shell-command command output-buffer error-buffer)))
4440+
(let ((proc (async-shell-command command output-buffer error-buffer))
4441+
(delete-exited-processes t))
44404442
(when (stringp input)
44414443
(process-send-string proc input))
44424444
(with-timeout
44434445
((if (getenv "EMACS_EMBA_CI") 30 10) (tramp--test-timeout-handler))
4444-
(while (accept-process-output proc nil nil t))
4445-
(should-not (process-live-p proc)))
4446-
;; `ls' could produce colorized output.
4447-
(with-current-buffer output-buffer
4448-
(goto-char (point-min))
4449-
(while (re-search-forward tramp-display-escape-sequence-regexp nil t)
4450-
(replace-match "" nil nil)))))
4446+
(while (or (accept-process-output proc nil nil t) (process-live-p proc))))
4447+
(accept-process-output proc nil nil t)))
44514448

44524449
(defun tramp--test-shell-command-to-string-asynchronously (command)
44534450
"Like `shell-command-to-string', but for asynchronous processes."
@@ -4486,26 +4483,33 @@ INPUT, if non-nil, is a string sent to the process."
44864483
this-shell-command
44874484
(format "ls %s" (file-name-nondirectory tmp-name))
44884485
(current-buffer))
4486+
;; `ls' could produce colorized output.
4487+
(goto-char (point-min))
4488+
(while
4489+
(re-search-forward tramp-display-escape-sequence-regexp nil t)
4490+
(replace-match "" nil nil))
44894491
(should
44904492
(string-equal
44914493
(format "%s\n" (file-name-nondirectory tmp-name))
44924494
(buffer-string))))
44934495

44944496
;; Cleanup.
4495-
(ignore-errors (delete-file tmp-name))))
4497+
(ignore-errors (delete-file tmp-name)))
44964498

4497-
;; Test `shell-command' with error buffer.
4498-
(let ((stderr (generate-new-buffer "*stderr*")))
4499-
(unwind-protect
4500-
(with-temp-buffer
4501-
(shell-command "echo foo; echo bar >&2" (current-buffer) stderr)
4502-
(should (string-equal "foo\n" (buffer-string)))
4503-
;; Check stderr.
4504-
(with-current-buffer stderr
4505-
(should (string-equal "bar\n" (buffer-string)))))
4499+
;; Test `{async-}shell-command' with error buffer.
4500+
(let ((stderr (generate-new-buffer "*stderr*")))
4501+
(unwind-protect
4502+
(with-temp-buffer
4503+
(funcall
4504+
this-shell-command
4505+
"echo foo >&2; echo bar" (current-buffer) stderr)
4506+
(should (string-equal "bar\n" (buffer-string)))
4507+
;; Check stderr.
4508+
(with-current-buffer stderr
4509+
(should (string-equal "foo\n" (buffer-string)))))
45064510

4507-
;; Cleanup.
4508-
(ignore-errors (kill-buffer stderr))))
4511+
;; Cleanup.
4512+
(ignore-errors (kill-buffer stderr)))))
45094513

45104514
;; Test sending string to `async-shell-command'.
45114515
(unwind-protect
@@ -4514,6 +4518,7 @@ INPUT, if non-nil, is a string sent to the process."
45144518
(should (file-exists-p tmp-name))
45154519
(tramp--test-async-shell-command
45164520
"read line; ls $line" (current-buffer) nil
4521+
;; String to be sent.
45174522
(format "%s\n" (file-name-nondirectory tmp-name)))
45184523
(should
45194524
(string-equal

0 commit comments

Comments
 (0)