Skip to content

Commit

Permalink
Fix an issue where 'heft clean' crashes.
Browse files Browse the repository at this point in the history
  • Loading branch information
iclanton committed Sep 26, 2023
1 parent 8df3e01 commit 4127d90
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 30 deletions.
58 changes: 30 additions & 28 deletions apps/heft/src/cli/HeftActionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,35 @@ export function initializeHeft(
terminal.writeVerboseLine('');
}

let _cliAbortSignal: AbortSignal | undefined;
export function ensureCliAbortSignal(terminal: ITerminal): AbortSignal {
if (!_cliAbortSignal) {
// Set up the ability to terminate the build via Ctrl+C and have it exit gracefully if pressed once,
// less gracefully if pressed a second time.
const cliAbortController: AbortController = new AbortController();
_cliAbortSignal = cliAbortController.signal;
const cli: ReadlineInterface = createInterface(process.stdin, undefined, undefined, true);
let forceTerminate: boolean = false;
cli.on('SIGINT', () => {
cli.close();

if (forceTerminate) {
terminal.writeErrorLine(`Forcibly terminating.`);
process.exit(1);
} else {
terminal.writeLine(
Colors.yellow(Colors.bold(`Canceling... Press Ctrl+C again to forcibly terminate.`))
);
}

forceTerminate = true;
cliAbortController.abort();
});
}

return _cliAbortSignal;
}

export async function runWithLoggingAsync(
fn: () => Promise<OperationStatus>,
action: IHeftAction,
Expand Down Expand Up @@ -272,7 +301,7 @@ export class HeftActionRunner {

const executionManager: OperationExecutionManager = new OperationExecutionManager(operations);

const cliAbortSignal: AbortSignal = this._createCliAbortSignal();
const cliAbortSignal: AbortSignal = ensureCliAbortSignal(this._terminal);

try {
await _startLifecycleAsync(this._internalHeftSession);
Expand Down Expand Up @@ -300,33 +329,6 @@ export class HeftActionRunner {
}
}

private _createCliAbortSignal(): AbortSignal {
// Set up the ability to terminate the build via Ctrl+C and have it exit gracefully if pressed once,
// less gracefully if pressed a second time.
const cliAbortController: AbortController = new AbortController();
const cliAbortSignal: AbortSignal = cliAbortController.signal;
const cli: ReadlineInterface = createInterface(process.stdin, undefined, undefined, true);
const terminal: ITerminal = this._terminal;
let forceTerminate: boolean = false;
cli.on('SIGINT', () => {
cli.close();

if (forceTerminate) {
terminal.writeErrorLine(`Forcibly terminating.`);
process.exit(1);
} else {
terminal.writeLine(
Colors.yellow(Colors.bold(`Canceling build... Press Ctrl+C again to forcibly terminate.`))
);
}

forceTerminate = true;
cliAbortController.abort();
});

return cliAbortSignal;
}

private _createWatchLoop(executionManager: OperationExecutionManager): WatchLoop {
const { _terminal: terminal } = this;
const watchLoop: WatchLoop = new WatchLoop({
Expand Down
4 changes: 2 additions & 2 deletions apps/heft/src/cli/actions/CleanAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { HeftTaskSession } from '../../pluginFramework/HeftTaskSession';
import { Constants } from '../../utilities/Constants';
import { definePhaseScopingParameters, expandPhases } from './RunAction';
import { deleteFilesAsync, type IDeleteOperation } from '../../plugins/DeleteFilesPlugin';
import { initializeHeft, runWithLoggingAsync } from '../HeftActionRunner';
import { ensureCliAbortSignal, initializeHeft, runWithLoggingAsync } from '../HeftActionRunner';

export class CleanAction extends CommandLineAction implements IHeftAction {
public readonly watch: boolean = false;
Expand Down Expand Up @@ -78,7 +78,7 @@ export class CleanAction extends CommandLineAction implements IHeftAction {

protected async onExecute(): Promise<void> {
const { heftConfiguration } = this._internalHeftSession;
const abortSignal: AbortSignal = new AbortSignal();
const abortSignal: AbortSignal = ensureCliAbortSignal(this._terminal);

initializeHeft(heftConfiguration, this._terminal, this._verboseFlag.value);
await runWithLoggingAsync(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/heft",
"comment": "Fix an issue where `heft clean` would crash with `ERR_ILLEGAL_CONSTRUCTOR`.",
"type": "patch"
}
],
"packageName": "@rushstack/heft"
}

0 comments on commit 4127d90

Please sign in to comment.