From 2521a3f26bfb9f2696b74dbc445e0ed9e897ab05 Mon Sep 17 00:00:00 2001 From: FrozenPandaz Date: Thu, 25 Apr 2024 13:57:02 -0400 Subject: [PATCH] fix(react): disable tty for next build --- .../packages/nx/executors/run-commands.json | 5 +++++ .../plugins/__snapshots__/plugin.spec.ts.snap | 2 ++ packages/next/src/plugins/plugin.ts | 5 +++++ .../executors/run-commands/run-commands.impl.ts | 12 +++++++++--- .../nx/src/executors/run-commands/schema.json | 16 ++++++++++++++-- packages/nx/src/native/index.d.ts | 2 +- packages/nx/src/native/pseudo_terminal/mac.rs | 5 +++-- .../nx/src/native/pseudo_terminal/non_mac.rs | 12 ++++++++++-- .../native/pseudo_terminal/pseudo_terminal.rs | 15 ++++++++++----- packages/nx/src/tasks-runner/pseudo-terminal.ts | 4 +++- 10 files changed, 62 insertions(+), 16 deletions(-) diff --git a/docs/generated/packages/nx/executors/run-commands.json b/docs/generated/packages/nx/executors/run-commands.json index 9440a545996d9..b522b32c842d4 100644 --- a/docs/generated/packages/nx/executors/run-commands.json +++ b/docs/generated/packages/nx/executors/run-commands.json @@ -126,6 +126,11 @@ "type": "boolean", "description": "Whether arguments should be forwarded when interpolation is not present.", "default": true + }, + "tty": { + "type": "boolean", + "description": "Whether commands should be run with a tty terminal", + "hidden": true } }, "additionalProperties": true, diff --git a/packages/next/src/plugins/__snapshots__/plugin.spec.ts.snap b/packages/next/src/plugins/__snapshots__/plugin.spec.ts.snap index 55dcc3a45b9df..38052c0234116 100644 --- a/packages/next/src/plugins/__snapshots__/plugin.spec.ts.snap +++ b/packages/next/src/plugins/__snapshots__/plugin.spec.ts.snap @@ -23,6 +23,7 @@ exports[`@nx/next/plugin integrated projects should create nodes 1`] = ` ], "options": { "cwd": "my-app", + "tty": false, }, "outputs": [ "{workspaceRoot}/my-app/.next", @@ -82,6 +83,7 @@ exports[`@nx/next/plugin root projects should create nodes 1`] = ` ], "options": { "cwd": ".", + "tty": false, }, "outputs": [ "{projectRoot}/.next", diff --git a/packages/next/src/plugins/plugin.ts b/packages/next/src/plugins/plugin.ts index 61f18394498c9..033fd47721c5e 100644 --- a/packages/next/src/plugins/plugin.ts +++ b/packages/next/src/plugins/plugin.ts @@ -130,6 +130,11 @@ async function getBuildTargetConfig( inputs: getInputs(namedInputs), outputs: [nextOutputPath, `${nextOutputPath}/!(cache)`], }; + + // TODO(ndcunningham): Update this to be consider different versions of next.js which is running + // This doesn't actually need to be tty, but next.js has a bug, https://github.com/vercel/next.js/issues/62906, where it exits 0 when SIGINT is sent. + targetConfig.options.tty = false; + return targetConfig; } diff --git a/packages/nx/src/executors/run-commands/run-commands.impl.ts b/packages/nx/src/executors/run-commands/run-commands.impl.ts index 6d28de1f6436e..b6f71b2060262 100644 --- a/packages/nx/src/executors/run-commands/run-commands.impl.ts +++ b/packages/nx/src/executors/run-commands/run-commands.impl.ts @@ -60,6 +60,7 @@ export interface RunCommandsOptions extends Json { __unparsed__: string[]; usePty?: boolean; streamOutput?: boolean; + tty?: boolean; } const propKeys = [ @@ -77,6 +78,7 @@ const propKeys = [ 'streamOutput', 'verbose', 'forwardAllArgs', + 'tty', ]; export interface NormalizedRunCommandsOptions extends RunCommandsOptions { @@ -151,7 +153,8 @@ async function runInParallel( options.env ?? {}, true, options.usePty, - options.streamOutput + options.streamOutput, + options.tty ).then((result: { success: boolean; terminalOutput: string }) => ({ result, command: c.command, @@ -269,7 +272,8 @@ async function runSerially( options.env ?? {}, false, options.usePty, - options.streamOutput + options.streamOutput, + options.tty ); terminalOutput += result.terminalOutput; if (!result.success) { @@ -298,7 +302,8 @@ async function createProcess( env: Record, isParallel: boolean, usePty: boolean = true, - streamOutput: boolean = true + streamOutput: boolean = true, + tty: boolean ): Promise<{ success: boolean; terminalOutput: string }> { env = processEnv(color, cwd, env); // The rust runCommand is always a tty, so it will not look nice in parallel and if we need prefixes @@ -319,6 +324,7 @@ async function createProcess( cwd, jsEnv: env, quiet: !streamOutput, + tty, }); childProcesses.add(cp); diff --git a/packages/nx/src/executors/run-commands/schema.json b/packages/nx/src/executors/run-commands/schema.json index 1a314e07c5122..a1ada1bc3c77e 100644 --- a/packages/nx/src/executors/run-commands/schema.json +++ b/packages/nx/src/executors/run-commands/schema.json @@ -100,8 +100,15 @@ }, "args": { "oneOf": [ - { "type": "array", "items": { "type": "string" } }, - { "type": "string" } + { + "type": "array", + "items": { + "type": "string" + } + }, + { + "type": "string" + } ], "description": "Extra arguments. You can pass them as follows: nx run project:target --args='--wait=100'. You can then use {args.wait} syntax to interpolate them in the workspace config file. See example [above](#chaining-commands-interpolating-args-and-setting-the-cwd)" }, @@ -140,6 +147,11 @@ "type": "boolean", "description": "Whether arguments should be forwarded when interpolation is not present.", "default": true + }, + "tty": { + "type": "boolean", + "description": "Whether commands should be run with a tty terminal", + "hidden": true } }, "additionalProperties": true, diff --git a/packages/nx/src/native/index.d.ts b/packages/nx/src/native/index.d.ts index d05d51b4a0606..560bad8b5e956 100644 --- a/packages/nx/src/native/index.d.ts +++ b/packages/nx/src/native/index.d.ts @@ -150,7 +150,7 @@ export class ChildProcess { } export class RustPseudoTerminal { constructor() - runCommand(command: string, commandDir?: string | undefined | null, jsEnv?: Record | undefined | null, quiet?: boolean | undefined | null): ChildProcess + runCommand(command: string, commandDir?: string | undefined | null, jsEnv?: Record | undefined | null, quiet?: boolean | undefined | null, tty?: boolean | undefined | null): ChildProcess /** * This allows us to run a pseudoterminal with a fake node ipc channel * this makes it possible to be backwards compatible with the old implementation diff --git a/packages/nx/src/native/pseudo_terminal/mac.rs b/packages/nx/src/native/pseudo_terminal/mac.rs index 325b83806eb5c..20a5389c55dd8 100644 --- a/packages/nx/src/native/pseudo_terminal/mac.rs +++ b/packages/nx/src/native/pseudo_terminal/mac.rs @@ -25,9 +25,10 @@ impl RustPseudoTerminal { command_dir: Option, js_env: Option>, quiet: Option, + tty: Option, ) -> napi::Result { let pseudo_terminal = create_pseudo_terminal()?; - run_command(&pseudo_terminal, command, command_dir, js_env, quiet) + run_command(&pseudo_terminal, command, command_dir, js_env, quiet, tty) } /// This allows us to run a pseudoterminal with a fake node ipc channel @@ -50,6 +51,6 @@ impl RustPseudoTerminal { ); trace!("nx_fork command: {}", &command); - self.run_command(command, command_dir, js_env, Some(quiet)) + self.run_command(command, command_dir, js_env, Some(quiet), Some(true)) } } diff --git a/packages/nx/src/native/pseudo_terminal/non_mac.rs b/packages/nx/src/native/pseudo_terminal/non_mac.rs index 8bfd73cb5d46b..514e5b3a23216 100644 --- a/packages/nx/src/native/pseudo_terminal/non_mac.rs +++ b/packages/nx/src/native/pseudo_terminal/non_mac.rs @@ -30,8 +30,16 @@ impl RustPseudoTerminal { command_dir: Option, js_env: Option>, quiet: Option, + tty: Option, ) -> napi::Result { - run_command(&self.pseudo_terminal, command, command_dir, js_env, quiet) + run_command( + &self.pseudo_terminal, + command, + command_dir, + js_env, + quiet, + tty, + ) } /// This allows us to run a pseudoterminal with a fake node ipc channel @@ -54,6 +62,6 @@ impl RustPseudoTerminal { ); trace!("nx_fork command: {}", &command); - self.run_command(command, command_dir, js_env, Some(quiet)) + self.run_command(command, command_dir, js_env, Some(quiet), Some(true)) } } diff --git a/packages/nx/src/native/pseudo_terminal/pseudo_terminal.rs b/packages/nx/src/native/pseudo_terminal/pseudo_terminal.rs index d30ff76a04a9f..4a998865cdad5 100644 --- a/packages/nx/src/native/pseudo_terminal/pseudo_terminal.rs +++ b/packages/nx/src/native/pseudo_terminal/pseudo_terminal.rs @@ -55,6 +55,7 @@ pub fn create_pseudo_terminal() -> napi::Result { } }); } + // Why do we do this here when it's already done when running a command? if std::io::stdout().is_tty() { trace!("Enabling raw mode"); enable_raw_mode().expect("Failed to enter raw terminal mode"); @@ -95,7 +96,8 @@ pub fn create_pseudo_terminal() -> napi::Result { printing_tx.send(()).ok(); }); if std::io::stdout().is_tty() { - disable_raw_mode().expect("Failed to enter raw terminal mode"); + trace!("Disabling raw mode"); + disable_raw_mode().expect("Failed to exit raw terminal mode"); } Ok(PseudoTerminal { quiet, @@ -111,6 +113,7 @@ pub fn run_command( command_dir: Option, js_env: Option>, quiet: Option, + tty: Option, ) -> napi::Result { let command_dir = get_directory(command_dir)?; @@ -134,7 +137,7 @@ pub fn run_command( let mut child = pair.slave.spawn_command(cmd)?; pseudo_terminal.running.store(true, Ordering::SeqCst); trace!("Running {}", command); - let is_tty = std::io::stdout().is_tty(); + let is_tty = tty.unwrap_or_else(|| std::io::stdout().is_tty()); if is_tty { trace!("Enabling raw mode"); enable_raw_mode().expect("Failed to enter raw terminal mode"); @@ -151,10 +154,10 @@ pub fn run_command( trace!("{} Exited", command); // This mitigates the issues with ConPTY on windows and makes it work. running_clone.store(false, Ordering::SeqCst); - trace!("Waiting for printing to finish"); - let timeout = 500; - let a = Instant::now(); if cfg!(windows) { + trace!("Waiting for printing to finish"); + let timeout = 500; + let a = Instant::now(); loop { if let Ok(_) = printing_rx.try_recv() { break; @@ -163,8 +166,10 @@ pub fn run_command( break; } } + trace!("Printing finished"); } if is_tty { + trace!("Disabling raw mode"); disable_raw_mode().expect("Failed to restore non-raw terminal"); } exit_to_process_tx.send(exit.to_string()).ok(); diff --git a/packages/nx/src/tasks-runner/pseudo-terminal.ts b/packages/nx/src/tasks-runner/pseudo-terminal.ts index db46e46a27110..016911fd6a418 100644 --- a/packages/nx/src/tasks-runner/pseudo-terminal.ts +++ b/packages/nx/src/tasks-runner/pseudo-terminal.ts @@ -43,14 +43,16 @@ export class PseudoTerminal { cwd, jsEnv, quiet, + tty, }: { cwd?: string; jsEnv?: Record; quiet?: boolean; + tty?: boolean; } = {} ) { return new PseudoTtyProcess( - this.rustPseudoTerminal.runCommand(command, cwd, jsEnv, quiet) + this.rustPseudoTerminal.runCommand(command, cwd, jsEnv, quiet, tty) ); }