Terminate REPL if not connected to tty input (#6480)

* Terminate REPL if not connected to tty input

If the standard input stream is not a TTY abort the REPL execution.

Solves a problem as the current REPL tries to be IO fault tolerant and
would indefinetely fail when crossterm tries to handle the STDIN.

Fixes nushell/nushell#6452

* Improve the error message
This commit is contained in:
Stefan Holderbach 2022-09-05 13:33:54 +02:00 committed by GitHub
parent 3278d290be
commit 33e1120add
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 0 deletions

1
Cargo.lock generated
View File

@ -2630,6 +2630,7 @@ dependencies = [
name = "nu-cli"
version = "0.67.1"
dependencies = [
"atty",
"chrono",
"crossterm 0.24.0",
"fancy-regex",

View File

@ -22,6 +22,7 @@ nu-ansi-term = "0.46.0"
nu-color-config = { path = "../nu-color-config", version = "0.67.1" }
reedline = { version = "0.10.0", features = ["bashisms", "sqlite"]}
atty = "0.2.14"
chrono = "0.4.21"
crossterm = "0.24.0"
fancy-regex = "0.10.0"

View File

@ -43,6 +43,16 @@ pub fn evaluate_repl(
) -> Result<()> {
use reedline::{FileBackedHistory, Reedline, Signal};
// Guard against invocation without a connected terminal.
// reedline / crossterm event polling will fail without a connected tty
if !atty::is(atty::Stream::Stdin) {
return Err(std::io::Error::new(
std::io::ErrorKind::NotFound,
"Nushell launched as interactive REPL but STDIN is not a TTY, either launch in a valid terminal or provide arguments to invoke a script!",
))
.into_diagnostic();
}
let mut entry_num = 0;
let mut nu_prompt = NushellPrompt::new();
@ -498,6 +508,10 @@ pub fn evaluate_repl(
let message = err.to_string();
if !message.contains("duration") {
println!("Error: {:?}", err);
// TODO: Identify possible error cases where a hard failure is preferable
// Ignoring and reporting could hide bigger problems
// e.g. https://github.com/nushell/nushell/issues/6452
// Alternatively only allow that expected failures let the REPL loop
}
if shell_integration {
run_ansi_sequence(&get_command_finished_marker(stack, engine_state))?;