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

[heft] Fix an issue where 'heft clean' crashes. #4358

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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: 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 {
iclanton marked this conversation as resolved.
Show resolved Hide resolved
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"
}