nushell/crates/nu-system/src/lib.rs

32 lines
829 B
Rust
Raw Normal View History

#![doc = include_str!("../README.md")]
Fix handling of stopped TUI applications on unix (#13741) # Description Instead of handling a foreground process being stopped in any way, we were simply ignoring SIGTSTP (which the terminal usually sends to the foreground process group when Ctrl-Z is pressed), and propagating this to our foreground children. This works for most processes, but it generally fails for applications which put the terminal in raw mode[1] and implement their own suspension mechanism (typically TUI apps like helix[2], neovim[3], top[4] or amp[5]). In these cases, triggering suspension within the app results in the terminal getting blocked, since the application is waiting for a SIGCONT, while nushell is waiting for it to exit. Fix this by unblocking SIGTSTP for our foreground children (neovim, helix and probably others send this to themselves while trying to suspend), and displaying the following message whenever one of them gets stopped: nushell currently does not support background jobs press any key to continue Pressing any key will then send SIGCONT to the child's process group and resume waiting. This fix is probably going to be superseded by a proper background job implementation (#247) at some point, but for now it's better than completely blocking the terminal. [1] https://docs.rs/crossterm/latest/crossterm/terminal/index.html#raw-mode [2] https://helix-editor.com/ [3] https://neovim.io/ [4] https://man7.org/linux/man-pages/man1/top.1.html [5] https://amp.rs/ - fixes #1038 - fixes #7335 - fixes #10335 # User-Facing Changes While any foreground process is running, Ctrl-Z is no longer ignored. Instead, the message described above is displayed, and nushell waits for a key to be pressed. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2024-09-24 11:44:58 +00:00
mod exit_status;
mod foreground;
2024-05-22 15:13:45 +00:00
#[cfg(target_os = "freebsd")]
mod freebsd;
#[cfg(any(target_os = "android", target_os = "linux"))]
mod linux;
#[cfg(target_os = "macos")]
mod macos;
2024-05-22 15:13:45 +00:00
#[cfg(any(target_os = "netbsd", target_os = "openbsd"))]
mod netbsd;
2023-09-01 06:18:55 +00:00
pub mod os_info;
#[cfg(target_os = "windows")]
mod windows;
Fix handling of stopped TUI applications on unix (#13741) # Description Instead of handling a foreground process being stopped in any way, we were simply ignoring SIGTSTP (which the terminal usually sends to the foreground process group when Ctrl-Z is pressed), and propagating this to our foreground children. This works for most processes, but it generally fails for applications which put the terminal in raw mode[1] and implement their own suspension mechanism (typically TUI apps like helix[2], neovim[3], top[4] or amp[5]). In these cases, triggering suspension within the app results in the terminal getting blocked, since the application is waiting for a SIGCONT, while nushell is waiting for it to exit. Fix this by unblocking SIGTSTP for our foreground children (neovim, helix and probably others send this to themselves while trying to suspend), and displaying the following message whenever one of them gets stopped: nushell currently does not support background jobs press any key to continue Pressing any key will then send SIGCONT to the child's process group and resume waiting. This fix is probably going to be superseded by a proper background job implementation (#247) at some point, but for now it's better than completely blocking the terminal. [1] https://docs.rs/crossterm/latest/crossterm/terminal/index.html#raw-mode [2] https://helix-editor.com/ [3] https://neovim.io/ [4] https://man7.org/linux/man-pages/man1/top.1.html [5] https://amp.rs/ - fixes #1038 - fixes #7335 - fixes #10335 # User-Facing Changes While any foreground process is running, Ctrl-Z is no longer ignored. Instead, the message described above is displayed, and nushell waits for a key to be pressed. # Tests + Formatting <!-- Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass (on Windows make sure to [enable developer mode](https://learn.microsoft.com/en-us/windows/apps/get-started/developer-mode-features-and-debugging)) - `cargo run -- -c "use toolkit.nu; toolkit test stdlib"` to run the tests for the standard library > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` --> # After Submitting <!-- If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. -->
2024-09-24 11:44:58 +00:00
pub use self::exit_status::ExitStatus;
#[cfg(unix)]
pub use self::foreground::stdin_fd;
Local socket mode and foreground terminal control for plugins (#12448) # Description Adds support for running plugins using local socket communication instead of stdio. This will be an optional thing that not all plugins have to support. This frees up stdio for use to make plugins that use stdio to create terminal UIs, cc @amtoine, @fdncred. This uses the [`interprocess`](https://crates.io/crates/interprocess) crate (298 stars, MIT license, actively maintained), which seems to be the best option for cross-platform local socket support in Rust. On Windows, a local socket name is provided. On Unixes, it's a path. The socket name is kept to a relatively small size because some operating systems have pretty strict limits on the whole path (~100 chars), so on macOS for example we prefer `/tmp/nu.{pid}.{hash64}.sock` where the hash includes the plugin filename and timestamp to be unique enough. This also adds an API for moving plugins in and out of the foreground group, which is relevant for Unixes where direct terminal control depends on that. TODO: - [x] Generate local socket path according to OS conventions - [x] Add support for passing `--local-socket` to the plugin executable instead of `--stdio`, and communicating over that instead - [x] Test plugins that were broken, including [amtoine/nu_plugin_explore](https://github.com/amtoine/nu_plugin_explore) - [x] Automatically upgrade to using local sockets when supported, falling back if it doesn't work, transparently to the user without any visible error messages - Added protocol feature: `LocalSocket` - [x] Reset preferred mode to `None` on `register` - [x] Allow plugins to detect whether they're running on a local socket and can use stdio freely, so that TUI plugins can just produce an error message otherwise - Implemented via `EngineInterface::is_using_stdio()` - [x] Clean up foreground state when plugin command exits on the engine side too, not just whole plugin - [x] Make sure tests for failure cases work as intended - `nu_plugin_stress_internals` added # User-Facing Changes - TUI plugins work - Non-Rust plugins could optionally choose to use this - This might behave differently, so will need to test it carefully across different operating systems # Tests + Formatting - :green_circle: `toolkit fmt` - :green_circle: `toolkit clippy` - :green_circle: `toolkit test` - :green_circle: `toolkit test stdlib` # After Submitting - [ ] Document local socket option in plugin contrib docs - [ ] Document how to do a terminal UI plugin in plugin contrib docs - [ ] Document: `EnterForeground` engine call - [ ] Document: `LeaveForeground` engine call - [ ] Document: `LocalSocket` protocol feature
2024-04-15 18:28:18 +00:00
pub use self::foreground::{ForegroundChild, ForegroundGuard};
2024-05-22 15:13:45 +00:00
#[cfg(target_os = "freebsd")]
pub use self::freebsd::*;
#[cfg(any(target_os = "android", target_os = "linux"))]
pub use self::linux::*;
#[cfg(target_os = "macos")]
pub use self::macos::*;
2024-05-22 15:13:45 +00:00
#[cfg(any(target_os = "netbsd", target_os = "openbsd"))]
pub use self::netbsd::*;
#[cfg(target_os = "windows")]
pub use self::windows::*;