-
Notifications
You must be signed in to change notification settings - Fork 708
Add Rush reporter frontend controls #5989
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
Merged
Sean Larkin (TheLarkInn)
merged 3 commits into
main
from
copilot/reporter-r2b-frontend-host-controls
Sep 11, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import type { ILaunchOptions } from '@microsoft/rush-lib'; | ||
| import type { IReporterEventSink } from '@rushstack/rush-reporter'; | ||
|
|
||
| /** | ||
| * The cross-version launch contract owned by the Rush frontend. | ||
| * | ||
| * @remarks | ||
| * Reporter selection remains in `@microsoft/rush`. The selected `rush-lib` | ||
| * receives only the typed producer sink in addition to its existing launch | ||
| * options, so an older engine can safely ignore the new property. | ||
| */ | ||
| export interface IRushFrontendLaunchOptions extends ILaunchOptions { | ||
| readonly reporterEventSink: IReporterEventSink; | ||
| readonly reporterCloseAsync: () => Promise<void>; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. | ||
| // See LICENSE in the project root for license information. | ||
|
|
||
| import type { ILaunchOptions } from '@microsoft/rush-lib'; | ||
| import { DEFAULT_SIGNAL_FLUSH_TIMEOUT_MS } from '@rushstack/rush-reporter'; | ||
|
|
||
| import { | ||
| initializeRushReporterHostAsync, | ||
| stripReporterValueControls, | ||
| type IRushReporterHostOptions, | ||
| type IInitializedRushReporterHost | ||
| } from './RushReporterHost'; | ||
| import { RushCommandSelector } from './RushCommandSelector'; | ||
| import { RushVersionSelector } from './RushVersionSelector'; | ||
| import type { MinimalRushConfiguration } from './MinimalRushConfiguration'; | ||
| import type { IRushFrontendLaunchOptions } from './IRushFrontendLaunchOptions'; | ||
|
|
||
| export interface IRushFrontendOptions { | ||
| readonly currentPackageVersion: string; | ||
| readonly rushVersionToLoad: string | undefined; | ||
| readonly configuration: MinimalRushConfiguration | undefined; | ||
| readonly launchOptions: ILaunchOptions; | ||
| readonly currentRushLib: typeof import('@microsoft/rush-lib'); | ||
| readonly initializeReporterHostAsync?: ( | ||
| options: IRushReporterHostOptions | ||
| ) => Promise<IInitializedRushReporterHost>; | ||
| readonly createVersionSelector?: (currentPackageVersion: string) => RushVersionSelector; | ||
| readonly executeCurrentRush?: ( | ||
| currentPackageVersion: string, | ||
| currentRushLib: typeof import('@microsoft/rush-lib'), | ||
| launchOptions: IRushFrontendLaunchOptions | ||
| ) => void | Promise<void>; | ||
| readonly processLifecycle?: IRushFrontendProcessLifecycle; | ||
| } | ||
|
|
||
| type RushTerminationSignal = 'SIGINT' | 'SIGTERM'; | ||
|
|
||
| export interface IRushFrontendProcessLifecycle { | ||
| registerBeforeExit(listener: () => void): () => void; | ||
| registerSignal(signal: RushTerminationSignal, listener: () => void): () => void; | ||
| terminate(signal: RushTerminationSignal): void; | ||
| setExitCode(exitCode: number): void; | ||
| reportCloseError(error: Error): void; | ||
| } | ||
|
|
||
| class RushFrontendReporterLifecycle { | ||
| private readonly _reporterHost: IInitializedRushReporterHost; | ||
| private readonly _processLifecycle: IRushFrontendProcessLifecycle; | ||
| private _disposeBeforeExit: (() => void) | undefined; | ||
| private readonly _disposeSignalHandlers: Array<() => void> = []; | ||
| private _closePromise: Promise<void> | undefined; | ||
|
|
||
| public constructor( | ||
| reporterHost: IInitializedRushReporterHost, | ||
| processLifecycle: IRushFrontendProcessLifecycle | ||
| ) { | ||
| this._reporterHost = reporterHost; | ||
| this._processLifecycle = processLifecycle; | ||
| } | ||
|
|
||
| public start(): void { | ||
| this._disposeBeforeExit = this._processLifecycle.registerBeforeExit(() => { | ||
| void this.closeAsync().catch((error: Error) => { | ||
| this._processLifecycle.reportCloseError(error); | ||
| this._processLifecycle.setExitCode(1); | ||
| }); | ||
| }); | ||
| for (const signal of ['SIGINT', 'SIGTERM'] as const) { | ||
| this._disposeSignalHandlers.push( | ||
| this._processLifecycle.registerSignal(signal, () => { | ||
| this._disposeSignals(); | ||
| void this._closeForSignalAsync(signal); | ||
| }) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| public closeAsync(timeoutMs?: number): Promise<void> { | ||
| if (!this._closePromise) { | ||
| this._closePromise = Promise.resolve() | ||
| .then(() => this._reporterHost.closeAsync(timeoutMs)) | ||
| .finally(() => this._dispose()); | ||
| } | ||
| return this._closePromise; | ||
| } | ||
|
|
||
| private _dispose(): void { | ||
| this._disposeBeforeExit?.(); | ||
| this._disposeBeforeExit = undefined; | ||
| this._disposeSignals(); | ||
| } | ||
|
|
||
| private _disposeSignals(): void { | ||
| for (const dispose of this._disposeSignalHandlers.splice(0)) { | ||
| dispose(); | ||
| } | ||
| } | ||
|
|
||
| private async _closeForSignalAsync(signal: RushTerminationSignal): Promise<void> { | ||
| const closeResult: Promise<Error | undefined> = this.closeAsync(DEFAULT_SIGNAL_FLUSH_TIMEOUT_MS).then( | ||
| () => undefined, | ||
| (error: Error) => error | ||
| ); | ||
| let timeout: ReturnType<typeof setTimeout> | undefined; | ||
| const deadline: Promise<'deadline'> = new Promise((resolve: (value: 'deadline') => void) => { | ||
| timeout = setTimeout(() => resolve('deadline'), DEFAULT_SIGNAL_FLUSH_TIMEOUT_MS); | ||
| }); | ||
|
|
||
| const result: Error | 'deadline' | undefined = await Promise.race([closeResult, deadline]); | ||
| if (timeout !== undefined) { | ||
| clearTimeout(timeout); | ||
| } | ||
| if (result === 'deadline') { | ||
| this._processLifecycle.reportCloseError( | ||
| new Error(`Reporter close exceeded the ${DEFAULT_SIGNAL_FLUSH_TIMEOUT_MS}ms signal deadline.`) | ||
| ); | ||
| } else if (result) { | ||
| this._processLifecycle.reportCloseError(result); | ||
| } | ||
| this._dispose(); | ||
| this._processLifecycle.terminate(signal); | ||
| } | ||
| } | ||
|
|
||
| export async function launchRushFrontendAsync(options: IRushFrontendOptions): Promise<void> { | ||
| const { | ||
| currentPackageVersion, | ||
| rushVersionToLoad, | ||
| configuration, | ||
| launchOptions, | ||
| currentRushLib, | ||
| initializeReporterHostAsync = initializeRushReporterHostAsync, | ||
| createVersionSelector = (version: string) => new RushVersionSelector(version), | ||
| executeCurrentRush = RushCommandSelector.execute, | ||
| processLifecycle = createProcessLifecycle() | ||
| } = options; | ||
|
|
||
| const reporterHost: IInitializedRushReporterHost = await initializeReporterHostAsync({ | ||
| repositoryOptIn: configuration?.useRushReporter, | ||
| forceLegacy: rushVersionToLoad !== undefined && rushVersionToLoad !== currentPackageVersion, | ||
| selectedRushVersion: rushVersionToLoad | ||
| }); | ||
| const reporterLifecycle: RushFrontendReporterLifecycle | undefined = reporterHost.selection.enabled | ||
| ? new RushFrontendReporterLifecycle(reporterHost, processLifecycle) | ||
| : undefined; | ||
| reporterLifecycle?.start(); | ||
| if (reporterHost.selection.reporterControlsOwnedByFrontend) { | ||
| process.argv = stripReporterValueControls( | ||
| process.argv, | ||
| new Set(reporterHost.selection.reporterValueFlagsToStrip), | ||
| new Set(reporterHost.selection.reporterFlagsToStrip) | ||
| ); | ||
| } | ||
| const reporterCloseAsync: () => Promise<void> = () => | ||
| reporterLifecycle?.closeAsync() ?? reporterHost.closeAsync(); | ||
| const reporterLaunchOptions: IRushFrontendLaunchOptions = { | ||
| ...launchOptions, | ||
| reporterEventSink: reporterHost.sink, | ||
| reporterCloseAsync | ||
| }; | ||
|
|
||
| try { | ||
| if (rushVersionToLoad && rushVersionToLoad !== currentPackageVersion) { | ||
| const versionSelector: RushVersionSelector = createVersionSelector(currentPackageVersion); | ||
| await versionSelector.ensureRushVersionInstalledAsync( | ||
| rushVersionToLoad, | ||
| configuration, | ||
| reporterLaunchOptions | ||
| ); | ||
| } else { | ||
| await executeCurrentRush(currentPackageVersion, currentRushLib, reporterLaunchOptions); | ||
| } | ||
| } catch (error) { | ||
| try { | ||
| await reporterCloseAsync(); | ||
| } catch (closeError) { | ||
| processLifecycle.reportCloseError(closeError as Error); | ||
| processLifecycle.setExitCode(1); | ||
| } | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| function createProcessLifecycle(): IRushFrontendProcessLifecycle { | ||
| return { | ||
| registerBeforeExit: (listener: () => void) => { | ||
| process.once('beforeExit', listener); | ||
| return () => process.off('beforeExit', listener); | ||
| }, | ||
| registerSignal: (signal: RushTerminationSignal, listener: () => void) => { | ||
| process.once(signal, listener); | ||
| return () => process.off(signal, listener); | ||
| }, | ||
| terminate: (signal: RushTerminationSignal) => { | ||
| process.kill(process.pid, signal); | ||
| }, | ||
| setExitCode: (exitCode: number) => { | ||
| process.exitCode = exitCode; | ||
| }, | ||
| reportCloseError: (error: Error) => { | ||
| process.stderr.write(`[reporter] Unable to finalize reporters: ${error.message}\n`); | ||
| } | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.