fix(core): exit the command with sigint if it is interrupted (#31028)

<!-- 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 tasks are still running but the user exits the TUI, Nx returns exit
code 0 (success)

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

When tasks are still running but the user exits the TUI, Nx returns exit
code 130 (SIGINT)

## 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-05-05 03:45:22 -04:00 committed by GitHub
parent 2cb0fa2b55
commit 02ba546ad7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 37 additions and 11 deletions

View File

@ -271,7 +271,7 @@ async function runPublishOnProjects(
* NOTE: Force TUI to be disabled for now.
*/
process.env.NX_TUI = 'false';
const commandResults = await runCommandForTasks(
const { taskResults } = await runCommandForTasks(
projectsWithTarget,
projectGraph,
{ nxJson },
@ -289,13 +289,13 @@ async function runPublishOnProjects(
);
const publishProjectsResult: PublishProjectsResult = {};
for (const taskData of Object.values(commandResults)) {
for (const taskData of Object.values(taskResults)) {
publishProjectsResult[taskData.task.target.project] = {
code: taskData.code,
};
}
await runPostTasksExecution({
taskResults: commandResults,
taskResults,
workspaceRoot,
nxJsonConfiguration: nxJson,
});

View File

@ -66,6 +66,7 @@ import {
import { TasksRunner, TaskStatus } from './tasks-runner';
import { shouldStreamOutput } from './utils';
import chalk = require('chalk');
import { signalToCode } from '../utils/exit-codes';
const originalStdoutWrite = process.stdout.write.bind(process.stdout);
const originalStderrWrite = process.stderr.write.bind(process.stderr);
@ -417,7 +418,7 @@ export async function runCommand(
nxJsonConfiguration: nxJson,
});
const taskResults = await runCommandForTasks(
const { taskResults, completed } = await runCommandForTasks(
projectsToRun,
currentProjectGraph,
{ nxJson },
@ -434,10 +435,12 @@ export async function runCommand(
extraOptions
);
const result = Object.values(taskResults).some(
(taskResult) =>
taskResult.status === 'failure' || taskResult.status === 'skipped'
)
const exitCode = !completed
? signalToCode('SIGINT')
: Object.values(taskResults).some(
(taskResult) =>
taskResult.status === 'failure' || taskResult.status === 'skipped'
)
? 1
: 0;
@ -447,7 +450,7 @@ export async function runCommand(
nxJsonConfiguration: nxJson,
});
return result;
return exitCode;
}
);
@ -463,7 +466,7 @@ export async function runCommandForTasks(
initiatingProject: string | null,
extraTargetDependencies: Record<string, (TargetDependencyConfig | string)[]>,
extraOptions: { excludeTaskDependencies: boolean; loadDotEnvFiles: boolean }
): Promise<TaskResults> {
): Promise<{ taskResults: TaskResults; completed: boolean }> {
const projectNames = projectsToRun.map((t) => t.name);
const projectNameSet = new Set(projectNames);
@ -517,7 +520,7 @@ export async function runCommandForTasks(
await printNxKey();
return taskResults;
return { taskResults, completed: didCommandComplete(tasks, taskResults) };
} catch (e) {
if (restoreTerminal) {
restoreTerminal();
@ -526,6 +529,29 @@ export async function runCommandForTasks(
}
}
function didCommandComplete(tasks: Task[], taskResults: TaskResults): boolean {
// If no tasks, then we can consider it complete
if (tasks.length === 0) {
return true;
}
let everyTaskIsContinuous = true;
for (const task of tasks) {
if (!task.continuous) {
everyTaskIsContinuous = false;
// If any discrete task does not have a result then it did not run
if (!taskResults[task.id]) {
return false;
}
}
}
// If every task is continuous, command cannot complete by definition
// Otherwise, we've looped through all the discrete tasks and they have results
return !everyTaskIsContinuous;
}
async function ensureWorkspaceIsInSyncAndGetGraphs(
projectGraph: ProjectGraph,
nxJson: NxJsonConfiguration,