fix(core): properly print errors coming from js when tui is enabled (#30885)

<!-- Please make sure you have read the submission guidelines before
posting an PR -->
<!--
https://github.com/nrwl/nx/blob/master/CONTRIBUTING.md#-submitting-a-pr
-->

<!-- Please make sure that your commit message follows our format -->
<!-- Example: `fix(nx): must begin with lowercase` -->

<!-- If this is a particularly complex change or feature addition, you
can request a dedicated Nx release for this pull request branch. Mention
someone from the Nx team or the `@nrwl/nx-pipelines-reviewers` and they
will confirm if the PR warrants its own release for testing purposes,
and generate it for you if appropriate. -->

## Current Behavior
<!-- This is the behavior we have today -->

When the TUI is enabled, JS errors during task running cause the process
to exit without printing out information about the error and also leave
the terminal in a broken state.

## Expected Behavior
<!-- This is the behavior we should expect with the changes in this PR
-->

JS errors are printed before the process exits and the terminal is
restored to a good state.

## Related Issue(s)
<!-- Please link the issue being fixed so it gets closed when this is
merged. -->

Fixes #
This commit is contained in:
Jason Jean 2025-04-28 11:58:59 -04:00 committed by GitHub
parent 2c8aba2fc2
commit 87e5e6bef8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 20 deletions

View File

@ -278,6 +278,12 @@ impl AppLifeCycle {
#[napi]
pub fn restore_terminal() -> Result<()> {
// Restore the terminal to a clean state
if let Ok(mut t) = Tui::new() {
if let Err(r) = t.exit() {
debug!("Unable to exit Terminal: {:?}", r);
}
}
// TODO: Maybe need some additional cleanup here in addition to the tui cleanup performed at the end of the render loop?
Ok(())
}

View File

@ -17,7 +17,6 @@ import {
getTaskDetails,
hashTasksThatDoNotDependOnOutputsOfOtherTasks,
} from '../hasher/hash-task';
import { IS_WASM } from '../native';
import {
runPostTasksExecution,
runPreTasksExecution,
@ -84,6 +83,7 @@ async function getTerminalOutputLifeCycle(
): Promise<{
lifeCycle: LifeCycle;
printSummary?: () => void;
restoreTerminal?: () => void;
renderIsDone: Promise<void>;
}> {
const overridesWithoutHidden = { ...overrides };
@ -277,6 +277,13 @@ async function getTerminalOutputLifeCycle(
return {
lifeCycle: new CompositeLifeCycle(lifeCycles),
restoreTerminal: () => {
process.stdout.write = originalStdoutWrite;
process.stderr.write = originalStderrWrite;
console.log = originalConsoleLog;
console.error = originalConsoleError;
restoreTerminal();
},
printSummary,
renderIsDone,
};
@ -464,7 +471,7 @@ export async function runCommandForTasks(
nxArgs.targets.includes(t.target.target)
);
const { lifeCycle, renderIsDone, printSummary } =
const { lifeCycle, renderIsDone, printSummary, restoreTerminal } =
await getTerminalOutputLifeCycle(
initiatingProject,
initiatingTasks,
@ -476,6 +483,7 @@ export async function runCommandForTasks(
overrides
);
try {
const taskResults = await invokeTasksRunner({
tasks,
projectGraph,
@ -497,6 +505,12 @@ export async function runCommandForTasks(
await printNxKey();
return taskResults;
} catch (e) {
if (restoreTerminal) {
restoreTerminal();
}
throw e;
}
}
async function ensureWorkspaceIsInSyncAndGetGraphs(