Skip to content
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

feat(core): forward stdin to commands started via rust #21195

Merged
merged 7 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 47 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/nx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ swc_common = "0.31.16"
swc_ecma_parser = { version = "0.137.1", features = ["typescript"] }
swc_ecma_visit = "0.93.0"
swc_ecma_ast = "0.107.0"
term_size = "0.3.2"
crossterm = "0.27.0"

[lib]
crate-type = ['cdylib']
Expand Down
2 changes: 0 additions & 2 deletions packages/nx/src/command-line/run/run-one.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,6 @@ export async function runOne(
extraTargetDependencies,
extraOptions
);
// fix for https://github.com/nrwl/nx/issues/1666
if (process.stdin['unref']) (process.stdin as any).unref();
process.exit(status);
}
}
Expand Down
25 changes: 22 additions & 3 deletions packages/nx/src/native/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::{

use anyhow::anyhow;
use crossbeam_channel::{bounded, unbounded, Receiver};
use crossterm::terminal::{self, disable_raw_mode, enable_raw_mode};
use crossterm::tty::IsTty;
use napi::threadsafe_function::ErrorStrategy::Fatal;
use napi::threadsafe_function::ThreadsafeFunction;
use napi::threadsafe_function::ThreadsafeFunctionCallMode::NonBlocking;
Expand Down Expand Up @@ -126,10 +128,10 @@ pub fn run_command(

let pty_system = NativePtySystem::default();

let (w, h) = term_size::dimensions().unwrap_or((80, 24));
let (w, h) = terminal::size().unwrap_or((80, 24));
let pair = pty_system.openpty(PtySize {
rows: h as u16,
cols: w as u16,
rows: h,
cols: w,
pixel_width: 0,
pixel_height: 0,
})?;
Expand All @@ -148,6 +150,8 @@ pub fn run_command(

let reader = pair.master.try_clone_reader()?;
let mut stdout = std::io::stdout();

// Output -> stdout handling
std::thread::spawn(move || {
let mut reader = BufReader::new(reader);
let mut buffer = [0; 8 * 1024];
Expand Down Expand Up @@ -182,10 +186,25 @@ pub fn run_command(

let process_killer = child.clone_killer();
let (exit_tx, exit_rx) = bounded(1);

let mut writer = pair.master.take_writer()?;

// Stdin -> pty stdin
if std::io::stdout().is_tty() {
std::thread::spawn(move || {
enable_raw_mode().expect("Failed to enter raw terminal mode");
let mut stdin = std::io::stdin();
#[allow(clippy::redundant_pattern_matching)]
// ignore errors that come from copying the stream
if let Ok(_) = std::io::copy(&mut stdin, &mut writer) {}
});
}

std::thread::spawn(move || {
let exit = child.wait().unwrap();
// make sure that master is only dropped after we wait on the child. Otherwise windows does not like it
drop(pair.master);
disable_raw_mode().expect("Failed to restore non-raw terminal");
exit_tx.send(exit.exit_code()).ok();
});

Expand Down
14 changes: 9 additions & 5 deletions packages/nx/src/native/tests/command.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { checkUnknownRules } from 'ajv/dist/compile/util';
Cammisuli marked this conversation as resolved.
Show resolved Hide resolved
import { PseudoTtyProcess } from '../../utils/child-process';
import { runCommand } from '../index';

Expand Down Expand Up @@ -26,23 +27,26 @@ describe('runCommand', () => {
it('should subscribe to output', (done) => {
const childProcess = runCommand('echo "hello world"', process.cwd());

childProcess.onOutput((output) => {
expect(output.trim()).toEqual('hello world');
let output = '';
childProcess.onOutput((chunk) => {
output += chunk;
});

Cammisuli marked this conversation as resolved.
Show resolved Hide resolved
childProcess.onExit(() => {
expect(output.trim()).toEqual('hello world');
done();
});
});

it('should be tty', (done) => {
const childProcess = runCommand('node -p "process.stdout.isTTY"');
let output = '';
childProcess.onOutput((out) => {
let output = JSON.stringify(out.trim());
output += out.trim();
// check to make sure that we have ansi sequence characters only available in tty terminals
expect(output).toMatchInlineSnapshot(`""\\u001b[33mtrue\\u001b[39m""`);
});
childProcess.onExit((_) => {
expect(JSON.stringify(output)).toMatchInlineSnapshot(`""\\u001b[33mtrue\\u001b[39m""`);
done();
});
});
Expand Down