Subject: Re: bug#48940: 27.2; regression: "emacs --script /dev/stdin" parses the script incorrectly when /dev/stdin is a pipe

From: Eli Zaretskii <[email-masked]>
Date: Sun, 13 Jun 2021 13:26:23 +0300

> Date: Wed, 9 Jun 2021 16:31:36 -0400
> From:  "Bryan C. Mills" via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
> `emacs --script /dev/stdin` worked reliably for me on Emacs 24 through 26.
> 
> As of Emacs 27, it no longer works reliably — depending on the script contents,
> it either fails with spurious errors or silently exits without
> finishing the script.
> 
> 
> In the following bash interaction, all three emacs invocations
> *should* run the same script.
> The third invocation demonstrates the bug: when /dev/stdin is a pipe from `cat`
> (instead of the script.el file directly), the script is no longer interpreted.
> With other scripts, I get various errors, seemingly due to `;` or `"`
> characters being
> dropped or ignored from the input.
> 
> ```
> $ cat script.el | cat /dev/stdin
> (print "Hello emacs!")
> 
> $ emacs --no-init-file --no-site-file --script script.el
> 
> "Hello emacs!"
> 
> $ emacs --no-init-file --no-site-file --script /dev/stdin <script.el
> 
> "Hello emacs!"
> 
> $ cat script.el | emacs --no-init-file --no-site-file --script /dev/stdin
> 
> $ echo $?
> 0
> 
> $
> ```
> 
> This reproduces with the GNU Emacs 27.1 currently packaged on Debian Testing,
> as well as the Google build of GNU Emacs 27.2 from which I produced
> the report below.

I see the problem, but it happens for me in Emacs 26 and Emacs 25 as
well, so I'm not sure this is new, or why it works for you in older
versions.  Maybe Debian included some local patches in those older
versions?  (My old Emacs versions were built from the unmodified
upstream sources.)

AFAICS, the problem seems to be that load-with-code-conversion calls
insert-file-contents, and the latter comes up with an empty buffer in
the problematic case.

It is notoriously hard to debug a program whose standard input was
redirected from a pipe of another program, so I couldn't see why the
above happens.  If someone could step in a debugger through
insert-file-contents in this case and describe what's going on there,
or tell how to do that when Emacs is invoked like this, maybe we
could make some progress.

Thanks.