diff --git a/Cargo.lock b/Cargo.lock index 44ca715769..520f0667f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2630,6 +2630,7 @@ dependencies = [ name = "nu-cli" version = "0.67.1" dependencies = [ + "atty", "chrono", "crossterm 0.24.0", "fancy-regex", diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index 58c5642670..71e1234e4a 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -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" diff --git a/crates/nu-cli/src/repl.rs b/crates/nu-cli/src/repl.rs index c1168ce9d5..1275cd427e 100644 --- a/crates/nu-cli/src/repl.rs +++ b/crates/nu-cli/src/repl.rs @@ -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))?;