unofficial mirror of bug-guix@gnu.org 
 help / color / mirror / code / Atom feed
From: Chris Marusich <cmmarusich@gmail.com>
To: "Ludovic Courtès" <ludo@gnu.org>
Cc: 53355@debbugs.gnu.org, 51466@debbugs.gnu.org
Subject: bug#53355: guix shell --check: confusing error message
Date: Sat, 25 Jun 2022 02:07:50 -0700	[thread overview]
Message-ID: <875ykpdsbd.fsf_-_@gmail.com> (raw)
In-Reply-To: <87sfozzglf.fsf_-_@gmail.com> (Chris Marusich's message of "Mon,  23 May 2022 21:42:36 -0700")


[-- Attachment #1.1: Type: text/plain, Size: 1486 bytes --]

Hi Ludo & Everyone,

Chris Marusich <cmmarusich@gmail.com> writes:

> Is it OK to rely on shell redirection?

It turns out that it is probably not OK to rely on shell redirection in
this case, after all.  For example, "dash does not support multi-digit
file descriptors":

https://bugs.launchpad.net/ubuntu/+source/dash/+bug/249620

Indeed, the patch I proposed earlier to rely on shell redirection caused
a command like

./pre-inst-env env SHELL=/gnu/store/nm0hccsphymxi8c24xmg6ixm9vcf25xb-dash-0.5.11.5/bin/dash guix shell --check --container -D guix

to hang.  It hangs because the FD Guile chooses to create and embed in
the script is 19 (on my machine, at least).  A redirection like
"env >&19" causes dash to error out, so no environment information gets
sent back to the parent process.  The same issue seemed to occur for the
ksh from our oksh package.

To resolve this, I changed the code so that it just writes to a
temporary file.  This is simple and more robust.  With the attached
patch, I was able to use a command like the one above to verify that
"guix environment --check" works correctly for Guix-built bash, dash,
ksh, fish, zsh, and ash.  I also verified that it works for Fedora's
/bin/sh and /bin/bash.

What do you think of this file-based approach?  Supporting many
different shells is pretty tricky, but I think this patch does a good
enough job.

-- 
Chris

PGP: https://savannah.gnu.org/people/viewgpg.php?user_id=106836

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-environment-Prevent-PS1-from-clobbering-output-in-ch.patch --]
[-- Type: text/x-patch, Size: 5014 bytes --]

From ef8d12a1d44903eafca7153c9344263b1d5d7d56 Mon Sep 17 00:00:00 2001
From: Chris Marusich <cmmarusich@gmail.com>
Date: Fri, 11 Mar 2022 00:20:12 -0800
Subject: [PATCH] environment: Prevent PS1 from clobbering output in 'check'.

Fixes: <https://issues.guix.gnu.org/51466>.

* guix/scripts/environment.scm (child-shell-environment) [temporary-file-port]
[temporary-file]: New local variables.
[script]: Redirect stdout to the temporary file.
[lines]: In the parent, send the script to the shell, wait for the shell to
exit, and then read lines from the temporary file.  Use a dynamic-wind
expression to ensure we always close port, close temporary-file-port, and
delete temporary-file.
---
 guix/scripts/environment.scm | 63 ++++++++++++++++++++++++------------
 1 file changed, 43 insertions(+), 20 deletions(-)

diff --git a/guix/scripts/environment.scm b/guix/scripts/environment.scm
index 3216235937..b02b0771e3 100644
--- a/guix/scripts/environment.scm
+++ b/guix/scripts/environment.scm
@@ -48,6 +48,7 @@ (define-module (guix scripts environment)
   #:autoload   (gnu packages bash) (bash)
   #:autoload   (gnu packages bootstrap) (bootstrap-executable %bootstrap-guile)
   #:use-module (ice-9 match)
+  #:use-module (ice-9 format)
   #:autoload   (ice-9 rdelim) (read-line)
   #:use-module (ice-9 vlist)
   #:use-module (srfi srfi-1)
@@ -418,14 +419,27 @@ (define (child-shell-environment shell profile manifest)
   (define-values (controller inferior)
     (openpty))
 
+  (define temporary-file-port (mkstemp "/tmp/guix-env.XXXXXX"))
+
+  (define temporary-file (port-filename temporary-file-port))
+
   (define script
-    ;; Script to obtain the list of environment variable values.  On a POSIX
-    ;; shell we can rely on 'set', but on fish we have to use 'env' (fish's
-    ;; 'set' truncates values and prints them in a different format.)
-    "env || /usr/bin/env || set; echo GUIX-CHECK-DONE; read x; exit\n")
+    ;; Script to obtain the list of environment variable values.
+    ;;
+    ;; On a POSIX shell we can rely on 'set', but on fish we have to use 'env'
+    ;; (fish's 'set' truncates values and prints them in a different format).
+    ;;
+    ;; Unless we redirect output to a file, there is a risk that the shell's
+    ;; PS1 prompt might clobber the output.  See:
+    ;; https://issues.guix.gnu.org/53355
+    (format
+     #f "env >~a || /usr/bin/env >~a || set >~a; \
+echo GUIX-CHECK-DONE >>~a; exit\n"
+     temporary-file temporary-file temporary-file temporary-file))
 
   (define lines
     (match (primitive-fork)
+      ;; Child
       (0
        (catch #t
          (lambda ()
@@ -436,24 +450,33 @@ (define lines
            (execl shell shell))
          (lambda _
            (primitive-exit 127))))
+      ;; Parent
       (pid
        (close-fdes inferior)
-       (let* ((port   (fdopen controller "r+l"))
-              (result (begin
-                        (display script port)
-                        (let loop ((lines '()))
-                          (match (read-line port)
-                            ((? eof-object?) (reverse lines))
-                            ("GUIX-CHECK-DONE\r"
-                             (display "done\n" port)
-                             (reverse lines))
-                            (line
-                             ;; Drop the '\r' from LINE.
-                             (loop (cons (string-drop-right line 1)
-                                         lines))))))))
-         (close-port port)
-         (waitpid pid)
-         result))))
+       (let* ((port   (fdopen controller "r+l")))
+         (dynamic-wind
+           (const #t)
+           (lambda ()
+             (display script port)
+             ;; Wait until the shell is done writing to the temporary file.
+             (waitpid pid)
+             (let loop ((lines '()))
+               ;; Read from the temporary file, not from the controller port, to
+               ;; prevent the shell's PS1 prompt from getting mixed into what we
+               ;; read.  See: https://issues.guix.gnu.org/51466
+               (match (read-line temporary-file-port)
+                 ((? eof-object?) (reverse lines))
+                 ("GUIX-CHECK-DONE"
+                  (reverse lines))
+                 (line
+                  (loop (cons line lines))))))
+           (lambda ()
+             (close-port temporary-file-port)
+             ;; The temporary file might not exist if something weird happens
+             ;; in the child shell that prevents it from even writing to the
+             ;; file (e.g. the shell fails to start for some reason).
+             (false-if-exception (delete-file temporary-file))
+             (close-port port)))))))
 
   (fold (lambda (line table)
           ;; Note: 'set' in fish outputs "NAME VALUE" instead of "NAME=VALUE"

base-commit: 319b8331b2357e12ec9edb9665513c32bef56622
-- 
2.34.0


[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 861 bytes --]

  parent reply	other threads:[~2022-06-25  9:09 UTC|newest]

Thread overview: 36+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-19  3:29 bug#53355: guix shell --check: confusing error message Chris Marusich
2022-01-24 14:35 ` Ludovic Courtès
2022-01-25  0:55   ` Chris Marusich
2022-01-25 13:39     ` Ludovic Courtès
2022-02-02  7:49       ` bug#51466: " Chris Marusich
2022-02-08  9:26         ` Ludovic Courtès
2022-02-13 23:17           ` Chris Marusich
2022-02-14  9:47             ` Ludovic Courtès
2022-03-08 19:07               ` Ludovic Courtès
2022-05-20 21:37                 ` Ludovic Courtès
2022-05-24  4:42               ` Chris Marusich
2022-06-13 10:03                 ` Ludovic Courtès
2022-06-19 20:40                   ` Chris Marusich
2022-06-20  7:34                     ` bug#51466: " Ludovic Courtès
2022-06-20 10:12                     ` bug#53355: " bokr
2022-06-20 17:56                       ` Bengt Richter
2022-06-20 23:27                         ` bug#51466: " Bengt Richter
2022-06-21  4:00                           ` Thiago Jung Bauermann via Bug reports for GNU Guix
2022-06-25  9:07                 ` Chris Marusich [this message]
2022-06-25  9:37                   ` Maxime Devos
2022-06-25 16:52                     ` Chris Marusich
2022-06-25 17:40                       ` Maxime Devos
2022-06-25 20:06                         ` bug#51466: " bokr
2022-06-25 21:04                           ` Maxime Devos
2022-06-26 10:33                         ` Josselin Poiret via Bug reports for GNU Guix
2022-06-26 13:07                           ` Maxime Devos
2022-06-26 19:45                             ` Tobias Geerinckx-Rice via Bug reports for GNU Guix
2022-06-27 10:17                   ` bug#51466: " Ludovic Courtès
2022-06-27 10:34                     ` bug#53355: " Maxime Devos
2022-06-28  7:45                       ` Ludovic Courtès
2022-06-28 10:38                         ` Maxime Devos
2022-06-28 16:57                           ` bug#53355: " paren--- via Bug reports for GNU Guix
2022-06-28 17:31                             ` bug#51466: " Maxime Devos
2022-07-04  8:11                             ` Ludovic Courtès
2022-06-27 11:23                     ` bokr
2022-06-27 14:22                       ` bug#51466: bug#53355: " Bengt Richter

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://guix.gnu.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=875ykpdsbd.fsf_-_@gmail.com \
    --to=cmmarusich@gmail.com \
    --cc=51466@debbugs.gnu.org \
    --cc=53355@debbugs.gnu.org \
    --cc=ludo@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/guix.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).