Skip to content

Error message printed if text is piped to a script STDIN #22443

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
philwalk opened this issue Jan 23, 2025 · 2 comments · May be fixed by #22444
Open

Error message printed if text is piped to a script STDIN #22443

philwalk opened this issue Jan 23, 2025 · 2 comments · May be fixed by #22444
Labels
area:runner Issues tied to the scala runner command. compat:windows itype:bug

Comments

@philwalk
Copy link
Contributor

philwalk commented Jan 23, 2025

Compiler version

This is common to all versions of scala3 that I've tried.
It can be demonstrated with scala3-3.6.3-x86_64-pc-win32.
No such error occurs in scala 2.13.

Minimized code

#!/usr/bin/env -S scala
val lines = for {
  line <- scala.collection.Iterator.Iterator.continually(scala.io.StdIn.readLine()).takeWhile( _ != null )
  if line != null
} yield line
printf("%d lines\n", lines.size)

Output

# ls -l | jsrc/readStdin.sc
stty: 'standard input': Inappropriate ioctl for device
Compiling project (Scala 3.6.3, JVM (17))
Compiled project (Scala 3.6.3, JVM (17))
837 lines

Expectation

No error message

A fix is to not call stty unless there's a tty, as in this modification of scala3-3.6.3-x86_64-pc-win32/libexec/common-shared

#!/usr/bin/env bash

#/*--------------------------------------------------------------------------
# *  Credits: This script is based on the script generated by sbt-pack.
# *--------------------------------------------------------------------------*/

if [ -e /usr/bin/tty -a "`tty`" != "not a tty" -a ! -p /dev/stdin ]; then
  isterminal=1
  # save terminal settings
  saved_stty=$(stty -g 2>/dev/null)
  # clear on error so we don't later try to restore them
  if [[ ! $? ]]; then
    saved_stty=""
  fi
else
  isterminal=0
fi

# restore stty settings (echo in particular)
function restoreSttySettings() {
  [ $isterminal -eq 1 ] && stty $saved_stty
  saved_stty=""
}

It's also necessary to condition another call to stty later in the file:

      [ $isterminal -eq 1 ] && stty -icanon min 1 -echo

Making stty calls conditional fixes the problem in all my manually tested environments, including cygwin, msys64, git-bash, WSL ubuntu, Linux Ubuntu and OSX.

I can submit a PR.

@philwalk philwalk added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 23, 2025
philwalk added a commit to philwalk/scala3 that referenced this issue Jan 23, 2025
@Gedochao Gedochao added area:runner Issues tied to the scala runner command. compat:windows and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Jan 24, 2025
@hamzaremmal
Copy link
Member

I can't reproduce this with my MacBook, I'm undusting my old Windows machine to try to reproduce the issue; since you're using scala3-3.6.3-x86_64-pc-win32

@philwalk
Copy link
Contributor Author

philwalk commented Mar 9, 2025

I can't reproduce this with my MacBook

I believe the fix does the right thing on a MacBook, and I assume it doesn't matter whether the problem it fixes is reproducible there.

This is a subtle test due to a variety of behaviors across different supported systems. My goal was to find an expression that does the same thing on all tested systems in both the tty context, and when running on the receiving end of a pipe.

I tested it against Linux, MaxBook, and several varieties of Window shell and console environments. Please let me know if it fails anywhere in either context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:runner Issues tied to the scala runner command. compat:windows itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants