// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license. // See LICENSE in the project root for license information. import * as fs from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; import * as rushLib from '@microsoft/rush-lib'; import type { ILaunchOptions } from '@microsoft/rush-lib'; import { EnvironmentConfiguration } from '@microsoft/rush-lib/lib/api/EnvironmentConfiguration'; import { RushCommandLineParser } from '@microsoft/rush-lib/lib/cli/RushCommandLineParser'; import { Import, LockFile } from '@rushstack/node-core-library'; import { Utilities } from '@microsoft/rush-lib/lib/utilities/Utilities'; import { ReporterHost, ReporterManager, type IReporter, type IReporterContext, type IReporterEventEnvelope, type IReporterEventSink } from '@rushstack/rush-reporter'; import { launchRushFrontendAsync, type IRushFrontendProcessLifecycle } from '../RushFrontend'; import type { IRushFrontendLaunchOptions } from '../IRushFrontendLaunchOptions'; import { initializeRushReporterHostAsync, resolveRushReporterSelection, type IInitializedRushReporterHost, type IRushReporterSelection } from '../RushReporterHost'; import { RushVersionSelector } from '../RushVersionSelector'; import type { MinimalRushConfiguration } from '../MinimalRushConfiguration'; async function createInitializedHostAsync( order: string[], reason: IInitializedRushReporterHost['selection']['reason'] = 'pre-major legacy default' ): Promise { order.push('host'); const host: ReporterHost = new ReporterHost({ env: {} }); await host.manager.initializeAsync(); let closePromise: Promise | undefined; const hasExplicitReporter: boolean = reason === 'explicit --reporter'; return { host, sink: host.getSink(), logArtifact: undefined, bootstrapReplay: { direct: true, replayed: false, eventCount: 0 }, abandonedHandoffFilesDeleted: [], selection: { reporter: 'legacy', logLevel: 'normal', outputs: [], commandJson: false, enabled: false, reporterControlsOwnedByFrontend: hasExplicitReporter, reporterValueFlagsToStrip: hasExplicitReporter ? ['--reporter'] : [], reason }, closeAsync: (timeoutMs?: number) => { if (!closePromise) { order.push('close'); closePromise = host.manager.closeAsync(timeoutMs); } return closePromise; } }; } async function createEnabledHostAsync( closeAsync?: (timeoutMs?: number) => Promise ): Promise { const host: ReporterHost = new ReporterHost({ env: {} }); await host.manager.initializeAsync(); return { host, sink: host.getSink(), logArtifact: undefined, bootstrapReplay: { direct: true, replayed: false, eventCount: 0 }, abandonedHandoffFilesDeleted: [], selection: { reporter: 'json', logLevel: 'normal', outputs: [], commandJson: false, enabled: true, reporterControlsOwnedByFrontend: true, reporterValueFlagsToStrip: ['--reporter', '--output', '--log-level'], reason: 'explicit --reporter' }, closeAsync: closeAsync ?? ((timeoutMs?: number) => host.manager.closeAsync(timeoutMs)) }; } async function createPhaseHangingHostAsync( hangingPhase: 'flush' | 'close' ): Promise { const never: Promise = new Promise(() => undefined); const reporter: IReporter = { name: `hang-${hangingPhase}`, initializeAsync: async (context: IReporterContext) => { void context; }, report: (event: IReporterEventEnvelope) => { void event; }, flushAsync: () => (hangingPhase === 'flush' ? never : Promise.resolve()), closeAsync: () => (hangingPhase === 'close' ? never : Promise.resolve()) }; const manager: ReporterManager = new ReporterManager(); manager.addReporter(reporter); const host: ReporterHost = new ReporterHost({ env: {}, manager }); await manager.initializeAsync(); let closePromise: Promise | undefined; return { host, sink: host.getSink(), logArtifact: undefined, bootstrapReplay: { direct: true, replayed: false, eventCount: 0 }, abandonedHandoffFilesDeleted: [], selection: { reporter: 'json', logLevel: 'normal', outputs: [], commandJson: false, enabled: true, reporterControlsOwnedByFrontend: true, reporterValueFlagsToStrip: ['--reporter', '--output', '--log-level'], reason: 'explicit --reporter' }, closeAsync: (timeoutMs?: number) => { closePromise ??= manager.closeAsync(timeoutMs); return closePromise; } }; } interface ITestProcessLifecycle extends IRushFrontendProcessLifecycle { beforeExitListener: (() => void) | undefined; readonly signalListeners: Map<'SIGINT' | 'SIGTERM', () => void>; readonly terminatedSignals: Array<'SIGINT' | 'SIGTERM'>; readonly exitCodes: number[]; readonly closeErrors: Error[]; } function createTestProcessLifecycle(): ITestProcessLifecycle { const lifecycle: ITestProcessLifecycle = { beforeExitListener: undefined, signalListeners: new Map(), terminatedSignals: [], exitCodes: [], closeErrors: [], registerBeforeExit: (listener: () => void) => { lifecycle.beforeExitListener = listener; return () => { if (lifecycle.beforeExitListener === listener) { lifecycle.beforeExitListener = undefined; } }; }, registerSignal: (signal: 'SIGINT' | 'SIGTERM', listener: () => void) => { lifecycle.signalListeners.set(signal, listener); return () => { if (lifecycle.signalListeners.get(signal) === listener) { lifecycle.signalListeners.delete(signal); } }; }, terminate: (signal: 'SIGINT' | 'SIGTERM') => { lifecycle.terminatedSignals.push(signal); }, setExitCode: (exitCode: number) => { lifecycle.exitCodes.push(exitCode); }, reportCloseError: (error: Error) => { lifecycle.closeErrors.push(error); } }; return lifecycle; } function emitCommandStarted(sink: IReporterEventSink): void { sink.emit({ protocolVersion: { major: 1, minor: 0 }, sessionId: 'session', source: { packageName: '@microsoft/rush-lib', packageVersion: '5.178.1' }, privacy: 'public', type: 'commandStarted', payload: { commandName: 'build' } }); } function releaseParserTestLocks(spy: jest.SpiedFunction): void { try { // Native parser locks live until process exit; these fixtures run the parser inside Jest instead. for (const result of spy.mock.results) { if (result.type === 'return' && result.value && !result.value.isReleased) result.value.release(); } } finally { spy.mockRestore(); } } describe(launchRushFrontendAsync.name, () => { it.each([ { reporter: 'file', output: undefined, commandJson: false, machineStdout: false }, { reporter: 'json', output: undefined, commandJson: false, machineStdout: true }, { reporter: 'file', output: 'json://stdout', commandJson: false, machineStdout: true }, { reporter: 'file', output: 'file://stdout', commandJson: false, machineStdout: true }, { reporter: 'file', output: 'json://stderr', commandJson: false, machineStdout: false }, { reporter: 'file', output: 'json://./stdout', commandJson: false, machineStdout: false }, { reporter: 'file', output: 'json://stdout', commandJson: true, machineStdout: true } ])('classifies $reporter / $output stdout ownership with command JSON $commandJson', async (testCase) => { const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'list', `--reporter=${testCase.reporter}`]; if (testCase.output) { process.argv.push(`--output=${testCase.output}`); } if (testCase.commandJson) { process.argv.push('--json'); } let receivedOptions: IRushFrontendLaunchOptions | undefined; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async (options) => ({ ...(await createEnabledHostAsync()), selection: resolveRushReporterSelection({ ...options, env: {}, stdout: { isTTY: false, write: () => undefined } }) }), executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; receivedOptions = launchOptions; return launchOptions.reporterCloseAsync(); }, processLifecycle: createTestProcessLifecycle() }); expect(receivedOptions?.reporterEnabled).toBe(true); expect(receivedOptions?.reporterSelectionReason).toBe('explicit --reporter'); expect(receivedOptions?.reporterStdoutIsMachineReadable).toBe(testCase.machineStdout); expect(receivedOptions?.reporterStdoutIsReserved).toBe(true); } finally { process.argv = originalArgv; } }); it.each([ ['file', false, true], ['file', true, false], ['json', false, true], ['ai', false, true], ['plaintext', false, false] ] as const)('reserves stdout for %s with command JSON %s: %s', async (reporter, commandJson, reserved) => { const initialized: IInitializedRushReporterHost = await createEnabledHostAsync(); const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'list']; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async () => ({ ...initialized, selection: { ...initialized.selection, reporter, commandJson } }), executeCurrentRush: (version, selectedRushLib, options) => { void version; void selectedRushLib; expect(options.reporterStdoutIsReserved).toBe(reserved); return options.reporterCloseAsync(); }, processLifecycle: createTestProcessLifecycle() }); } finally { await initialized.closeAsync(); process.argv = originalArgv; } }); it.each([true, false])( 'keeps installed-path activity local-sensitive without changing plain status (reporter: %s)', async (reporterEnabled) => { const directory: string = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'rush-version-privacy-')); const originalGlobalFolder: string | undefined = process.env.RUSH_GLOBAL_FOLDER; process.env.RUSH_GLOBAL_FOLDER = directory; const host: ReporterHost = new ReporterHost({ env: {} }); await host.manager.initializeAsync(); const emitSpy = jest.spyOn(host.manager, 'emit'); const consoleSpy = jest.spyOn(console, 'log').mockImplementation(() => undefined); const markerSpy = jest.spyOn(rushLib._FlagFile.prototype, 'isValidAsync').mockResolvedValue(false); const createSpy = jest.spyOn(rushLib._FlagFile.prototype, 'createAsync').mockResolvedValue(undefined); const installSpy = jest.spyOn(Utilities, 'installPackageInDirectoryAsync').mockResolvedValue(undefined); const stopBeforeLaunch: Error = new Error('stop before loading installed engine'); const resolveSpy = jest.spyOn(Import, 'resolveModuleAsync').mockRejectedValue(stopBeforeLaunch); const expectedPath: string = path.join( new rushLib._RushGlobalFolder().nodeSpecificPath, 'rush-5.177.0' ); const messages: string[] = [ 'Rush version 5.177.0 is not currently installed. Installing...', 'Trying to acquire lock for rush-5.177.0', `Successfully installed Rush version 5.177.0 in ${expectedPath}.` ]; try { await expect( new RushVersionSelector('5.178.1').ensureRushVersionInstalledAsync('5.177.0', undefined, { isManaged: false, reporter: { eventSink: host.getSink(), sessionId: 'startup-session' }, reporterCloseAsync: () => host.manager.closeAsync(), reporterEnabled, reporterSelectionReason: reporterEnabled ? 'explicit --reporter' : 'pre-major legacy default' }) ).rejects.toBe(stopBeforeLaunch); expect(installSpy).toHaveBeenCalledWith(expect.objectContaining({ directory: expectedPath })); if (reporterEnabled) { expect(emitSpy.mock.calls.map(([event]) => event.privacy)).toEqual([ 'public', 'public', 'local-sensitive' ]); expect(emitSpy.mock.calls.map(([event]) => event.payload)).toEqual( messages.map((text) => ({ kind: 'version-selection', text })) ); expect(consoleSpy).not.toHaveBeenCalled(); } else { expect(emitSpy).not.toHaveBeenCalled(); expect(consoleSpy.mock.calls).toEqual(messages.map((text) => [text])); } } finally { emitSpy.mockRestore(); consoleSpy.mockRestore(); markerSpy.mockRestore(); createSpy.mockRestore(); installSpy.mockRestore(); resolveSpy.mockRestore(); await host.manager.closeAsync(); if (originalGlobalFolder === undefined) delete process.env.RUSH_GLOBAL_FOLDER; else process.env.RUSH_GLOBAL_FOLDER = originalGlobalFolder; await fs.promises.rm(directory, { recursive: true, force: true }); } } ); it('retains the frontend version in startup envelopes after native-private parent alignment', async () => { const host: ReporterHost = new ReporterHost({ env: {} }); await host.manager.initializeAsync(); const emitSpy: jest.SpiedFunction = jest.spyOn(host.manager, 'emit'); const markerSpy: jest.SpiedFunction = jest .spyOn(rushLib._FlagFile.prototype, 'isValidAsync') .mockResolvedValue(false); const stopBeforeInstall: Error = new Error('stop before package installation'); const lockSpy: jest.SpiedFunction = jest .spyOn(LockFile, 'acquireAsync') .mockRejectedValue(stopBeforeInstall); try { await expect( new RushVersionSelector('5.178.1-native').ensureRushVersionInstalledAsync('5.177.0', undefined, { isManaged: false, reporter: { eventSink: host.getSink(), sessionId: 'startup-session' }, reporterCloseAsync: () => host.manager.closeAsync(), reporterEnabled: true, reporterSelectionReason: 'explicit --reporter' }) ).rejects.toBe(stopBeforeInstall); expect(emitSpy.mock.calls.map(([event]) => event.source)).toEqual([ { packageName: '@microsoft/rush', packageVersion: '5.178.1-native' }, { packageName: '@microsoft/rush', packageVersion: '5.178.1-native' } ]); } finally { emitSpy.mockRestore(); markerSpy.mockRestore(); lockSpy.mockRestore(); await host.manager.closeAsync(); } }); it('creates the authoritative host before invoking the bundled rush-lib and passes only its channel', async () => { const order: string[] = []; let receivedOptions: IRushFrontendLaunchOptions | undefined; const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'build', '--reporter=legacy', '--json']; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: () => createInitializedHostAsync(order, 'explicit --reporter'), executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; order.push('engine'); receivedOptions = launchOptions; return launchOptions.reporterCloseAsync(); }, processLifecycle }); expect(order).toEqual(['host', 'engine', 'close']); expect(process.argv).toEqual(['node', 'rush', 'build', '--json']); expect(receivedOptions?.reporter).toEqual({ eventSink: expect.objectContaining({ emit: expect.any(Function) }), sessionId: expect.any(String), operationStreamEnabled: false }); expect(receivedOptions).not.toHaveProperty('selection'); expect(receivedOptions).not.toHaveProperty('host'); expect(receivedOptions).not.toHaveProperty('manager'); expect(processLifecycle.beforeExitListener).toBeUndefined(); expect(processLifecycle.signalListeners.size).toBe(0); } finally { process.argv = originalArgv; } }); it('passes one typed reporter session through the real Rush launch boundary', async () => { const order: string[] = []; const initialized: IInitializedRushReporterHost = await createInitializedHostAsync(order); const createSessionId: jest.Mock = jest.fn(() => 'session-from-frontend'); let receivedOptions: ILaunchOptions | undefined; const launchSpy: jest.SpyInstance = jest .spyOn(rushLib.Rush, 'launch') .mockImplementation((version, launchOptions) => { void version; receivedOptions = launchOptions; }); const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'build']; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async () => initialized, createSessionId, processLifecycle: createTestProcessLifecycle() }); expect(launchSpy).toHaveBeenCalledTimes(1); expect(createSessionId).toHaveBeenCalledTimes(1); expect(receivedOptions?.reporter).toEqual({ eventSink: initialized.sink, sessionId: 'session-from-frontend', operationStreamEnabled: false }); await initialized.closeAsync(); expect(order).toEqual(['host', 'close']); } finally { launchSpy.mockRestore(); process.argv = originalArgv; } }); it('keeps an active purge reporter log outside the temp folder being purged', async () => { const order: string[] = []; let commonTempFolder: string | undefined = 'not-captured'; let actionName: string | undefined; const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'purge', '--reporter=file']; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: { commonTempFolder: '/repo/common/temp', useRushReporter: false } as MinimalRushConfiguration, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async (options) => { commonTempFolder = options.commonTempFolder; actionName = options.actionName; return createInitializedHostAsync(order, 'explicit --reporter'); }, executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; return launchOptions.reporterCloseAsync(); }, processLifecycle: createTestProcessLifecycle() }); expect(actionName).toBe('purge'); expect(commonTempFolder).toBeUndefined(); } finally { process.argv = originalArgv; } }); it('rejects an explicit reporter before initializing an incompatible selected engine', async () => { const directory: string = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'rush-old-engine-')); const outputPath: string = path.join(directory, 'events.jsonl'); const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'build', '--reporter=json', `--output=json://${outputPath}`]; const createVersionSelector: jest.Mock = jest.fn(); try { await expect( launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: '5.177.0', configuration: undefined, launchOptions: { isManaged: true }, currentRushLib: rushLib, initializeReporterHostAsync: (options) => initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), cwd: directory, env: {}, stdout: { isTTY: false, write: () => undefined } }), createVersionSelector, processLifecycle }) ).rejects.toThrow(/selected Rush engine 5\.177\.0 cannot safely use --reporter=json/); expect(createVersionSelector).not.toHaveBeenCalled(); expect(processLifecycle.beforeExitListener).toBeUndefined(); expect(processLifecycle.signalListeners.size).toBe(0); await expect(fs.promises.stat(outputPath)).rejects.toMatchObject({ code: 'ENOENT' }); } finally { process.argv = originalArgv; await fs.promises.rm(directory, { recursive: true, force: true }); } }); it('keeps an implicit repository opt-in on the legacy path for an incompatible engine', async () => { const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); const versionSelector: RushVersionSelector = Object.create(RushVersionSelector.prototype); let receivedArgv: string[] | undefined; versionSelector.ensureRushVersionInstalledAsync = async (version, configuration, launchOptions) => { void version; void configuration; receivedArgv = [...process.argv]; await launchOptions.reporterCloseAsync(); }; const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'custom', '--output', 'custom.zip', '--log-level', 'custom', '--verbose']; let selection: IRushReporterSelection | undefined; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: '5.177.0', configuration: { useRushReporter: true } as MinimalRushConfiguration, launchOptions: { isManaged: true }, currentRushLib: rushLib, initializeReporterHostAsync: async (options) => { const initialized: IInitializedRushReporterHost = await initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), env: {}, stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }); selection = initialized.selection; return initialized; }, createVersionSelector: () => versionSelector, processLifecycle }); expect(selection).toMatchObject({ reporter: 'legacy', enabled: false, reporterControlsOwnedByFrontend: false, reporterValueFlagsToStrip: [] }); expect(receivedArgv).toEqual(process.argv); expect(processLifecycle.beforeExitListener).toBeUndefined(); expect(processLifecycle.signalListeners.size).toBe(0); } finally { process.argv = originalArgv; } }); it.each([ { name: 'unsupported custom reporter', reporter: 'junit', expectedArgv: [ 'node', 'rush', 'custom', '--reporter', 'junit', '--output', 'custom.zip', '--log-level', 'custom', '--verbose' ] }, { name: 'explicit legacy reporter', reporter: 'legacy', expectedArgv: ['node', 'rush', 'custom', '--output', 'custom.zip', '--log-level', 'custom', '--verbose'] } ])('preserves the old-engine $name escape path', async ({ reporter, expectedArgv }) => { const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); const versionSelector: RushVersionSelector = Object.create(RushVersionSelector.prototype); let receivedArgv: string[] | undefined; versionSelector.ensureRushVersionInstalledAsync = async (version, configuration, launchOptions) => { void version; void configuration; receivedArgv = [...process.argv]; await launchOptions.reporterCloseAsync(); }; const originalArgv: string[] = process.argv; process.argv = [ 'node', 'rush', 'custom', '--reporter', reporter, '--output', 'custom.zip', '--log-level', 'custom', '--verbose' ]; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: '5.177.0', configuration: undefined, launchOptions: { isManaged: true }, currentRushLib: rushLib, initializeReporterHostAsync: (options) => initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), env: {}, stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }), createVersionSelector: () => versionSelector, processLifecycle }); expect(receivedArgv).toEqual(expectedArgv); expect(processLifecycle.beforeExitListener).toBeUndefined(); expect(processLifecycle.signalListeners.size).toBe(0); } finally { process.argv = originalArgv; } }); it.each([ { name: 'unsupported reporter as a custom value', reporter: 'junit', env: {}, repositoryOptIn: false, expectedArguments: [ '--reporter', 'junit', '--output', 'custom.zip', '--log-level', 'custom', '--verbose' ], expectedEnabled: false }, { name: 'supported reporter as frontend ownership', reporter: 'json', env: {}, repositoryOptIn: false, expectedArguments: ['--verbose'], expectedEnabled: true }, { name: 'explicit legacy under the emergency override', reporter: 'legacy', env: { RUSH_REPORTER: 'legacy' }, repositoryOptIn: false, expectedArguments: ['--output', 'custom.zip', '--log-level', 'custom', '--verbose'], expectedEnabled: false }, { name: 'custom reporter under repository emergency rollback', reporter: 'junit', env: { RUSH_REPORTER: 'legacy' }, repositoryOptIn: true, expectedArguments: [ '--reporter', 'junit', '--output', 'custom.zip', '--log-level', 'custom', '--verbose' ], expectedEnabled: false } ])('runs the real custom command fixture with $name', async (testCase) => { const directory: string = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'rush-custom-command-')); const repoPath: string = path.join(directory, 'repo'); const fixturePath: string = path.resolve( __dirname, '../../../../libraries/rush-lib/src/cli/test/basicAndRunBuildActionRepo' ); await fs.promises.cp(fixturePath, repoPath, { recursive: true }); const reporterOutputPath: string = path.join(directory, 'reporter.jsonl'); const outputValue: string = testCase.reporter === 'json' ? `json://${reporterOutputPath}` : 'custom.zip'; const logLevelValue: string = testCase.reporter === 'json' ? 'debug' : 'custom'; const originalArgv: string[] = process.argv; const originalExitCode: string | number | null | undefined = process.exitCode; process.argv = [ 'node', 'rush', 'custom-output', '--reporter', testCase.reporter, '--output', outputValue ]; if (testCase.reporter !== 'json') { process.argv.push('--log-level', logLevelValue); } process.argv.push('--verbose'); let selection: IRushReporterSelection | undefined; const lockSpy = jest.spyOn(LockFile, 'tryAcquire'); try { EnvironmentConfiguration.reset(); await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: { useRushReporter: testCase.repositoryOptIn } as MinimalRushConfiguration, launchOptions: { isManaged: true }, currentRushLib: rushLib, initializeReporterHostAsync: async (options) => { const initialized: IInitializedRushReporterHost = await initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), cwd: repoPath, env: testCase.env, stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }); selection = initialized.selection; return initialized; }, executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: repoPath, reporterCloseAsync: launchOptions.reporterCloseAsync }); return parser.executeAsync().then(() => undefined); }, processLifecycle: createTestProcessLifecycle() }); expect(selection?.enabled).toBe(testCase.expectedEnabled); expect( JSON.parse(await fs.promises.readFile(path.join(repoPath, 'custom-output-args.json'), 'utf8')) ).toEqual(testCase.expectedArguments); expect( lockSpy.mock.results.some( (result) => result.type === 'return' && result.value && !result.value.isReleased ) ).toBe(true); } finally { EnvironmentConfiguration.reset(); process.argv = originalArgv; process.exitCode = originalExitCode; releaseParserTestLocks(lockSpy); await fs.promises.rm(directory, { recursive: true, force: true }); } }); it('rejects an unsupported reporter typo when repository opt-in establishes ownership', async () => { const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'custom-output', '--reporter=junit']; try { await expect( launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: { useRushReporter: true } as MinimalRushConfiguration, launchOptions: { isManaged: true }, currentRushLib: rushLib, initializeReporterHostAsync: (options) => initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), env: {}, stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }), processLifecycle: createTestProcessLifecycle() }) ).rejects.toThrow('Unsupported reporter "junit"'); } finally { process.argv = originalArgv; } }); it.each([false, true])( 'runs a value-less custom reporter flag with repository rollback %s', async (rollback) => { const directory: string = await fs.promises.mkdtemp( path.join(os.tmpdir(), 'rush-custom-reporter-flag-') ); const repoPath: string = path.join(directory, 'repo'); const fixturePath: string = path.resolve( __dirname, '../../../../libraries/rush-lib/src/cli/test/basicAndRunRebuildActionRepo' ); await fs.promises.cp(fixturePath, repoPath, { recursive: true }); const originalArgv: string[] = process.argv; const originalExitCode: string | number | null | undefined = process.exitCode; process.argv = ['node', 'rush', 'custom-reporter-flag', '--reporter']; const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); let selection: IRushReporterSelection | undefined; const lockSpy = jest.spyOn(LockFile, 'tryAcquire'); try { EnvironmentConfiguration.reset(); await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: { useRushReporter: rollback } as MinimalRushConfiguration, launchOptions: { isManaged: true }, currentRushLib: rushLib, initializeReporterHostAsync: async (options) => { const initialized: IInitializedRushReporterHost = await initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), cwd: repoPath, env: rollback ? { RUSH_REPORTER: 'legacy' } : {}, stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }); selection = initialized.selection; return initialized; }, executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: repoPath, reporterCloseAsync: launchOptions.reporterCloseAsync }); return parser.executeAsync().then(() => undefined); }, processLifecycle }); expect(selection).toMatchObject({ reporter: 'legacy', enabled: false, reporterControlsOwnedByFrontend: rollback }); expect( JSON.parse( await fs.promises.readFile(path.join(repoPath, 'custom-reporter-flag-args.json'), 'utf8') ) ).toEqual(['--reporter']); expect(processLifecycle.beforeExitListener).toBeUndefined(); expect(processLifecycle.signalListeners.size).toBe(0); expect( lockSpy.mock.results.some( (result) => result.type === 'return' && result.value && !result.value.isReleased ) ).toBe(true); } finally { EnvironmentConfiguration.reset(); process.argv = originalArgv; process.exitCode = originalExitCode; releaseParserTestLocks(lockSpy); await fs.promises.rm(directory, { recursive: true, force: true }); } } ); it('flushes and closes an explicit output through the real frontend boundary on success', async () => { const directory: string = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'rush-frontend-')); const outputPath: string = path.join(directory, 'events.jsonl'); const originalArgv: string[] = process.argv; let stdoutText: string = ''; process.argv = ['node', 'rush', 'build', '--reporter=json', `--output=json://${outputPath}`]; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: (options) => initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), cwd: directory, env: {}, stdout: { isTTY: false, write: (text: string) => { stdoutText += text; } }, includeDefaultFileReporter: false }), executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; emitCommandStarted(launchOptions.reporter.eventSink); return launchOptions.reporterCloseAsync(); }, processLifecycle: createTestProcessLifecycle() }); expect(JSON.parse(stdoutText).type).toBe('commandStarted'); expect(JSON.parse(await fs.promises.readFile(outputPath, 'utf8')).type).toBe('commandStarted'); } finally { process.argv = originalArgv; await fs.promises.rm(directory, { recursive: true, force: true }); } }); it('flushes an explicit output before the parser process.exit backstop', async () => { const directory: string = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'rush-parser-exit-')); const outputPath: string = path.join(directory, 'events.jsonl'); const originalArgv: string[] = process.argv; const originalExitCode: string | number | null | undefined = process.exitCode; process.argv = ['node', 'rush', 'build', '--reporter=json', `--output=json://${outputPath}`]; let outputAtExit: string | undefined; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: (options) => initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), cwd: directory, env: {}, stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }), executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; emitCommandStarted(launchOptions.reporter.eventSink); const parser: RushCommandLineParser = new RushCommandLineParser({ cwd: directory, reporterCloseAsync: launchOptions.reporterCloseAsync }); jest .spyOn(parser.pluginManager, 'tryInitializeUnassociatedPluginsAsync') .mockRejectedValue(new Error('parser failed')); process.exitCode = 1; const exited: Promise = new Promise((resolve: () => void) => { jest.spyOn(process, 'exit').mockImplementation(() => { outputAtExit = fs.readFileSync(outputPath, 'utf8'); resolve(); return undefined as never; }); jest.spyOn(console, 'error').mockImplementation(() => undefined); }); return Promise.all([exited, parser.executeAsync(['build'])]).then(([, succeeded]) => { expect(succeeded).toBe(false); }); }, processLifecycle: createTestProcessLifecycle() }); expect(JSON.parse(outputAtExit!).type).toBe('commandStarted'); } finally { jest.restoreAllMocks(); process.argv = originalArgv; process.exitCode = originalExitCode; await fs.promises.rm(directory, { recursive: true, force: true }); } }); it('preserves pass-through arguments byte-for-byte through the real frontend boundary', async () => { const originalArgv: string[] = process.argv; const passThroughArguments: string[] = [ '--', '--reporter=unknown', '--reporter', 'tool-reporter', '--output=not-a-url', '--output', 'tool-output', '--log-level=loud', '--log-level', 'tool-level', '--quiet', '-q', '--verbose', '--debug', '-d', '--json', 'ordinary', 'value with spaces' ]; process.argv = ['node', 'rush', 'build', '--reporter=json', ...passThroughArguments]; let receivedArgv: string[] | undefined; let selection: IRushReporterSelection | undefined; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async (options) => { const initialized: IInitializedRushReporterHost = await initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), env: {}, stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }); selection = initialized.selection; return initialized; }, executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; receivedArgv = [...process.argv]; return launchOptions.reporterCloseAsync(); }, processLifecycle: createTestProcessLifecycle() }); expect(selection).toMatchObject({ reporter: 'json', logLevel: 'normal', commandJson: false, enabled: true }); expect(receivedArgv).toEqual(['node', 'rush', 'build', ...passThroughArguments]); } finally { process.argv = originalArgv; } }); it('preserves custom value parameters when repository opt-in enables reporting', async () => { const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'custom', '--output', 'custom.zip', '--log-level', 'custom', '--verbose']; let receivedArgv: string[] | undefined; let selection: IRushReporterSelection | undefined; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: { useRushReporter: true } as MinimalRushConfiguration, launchOptions: { isManaged: true }, currentRushLib: rushLib, initializeReporterHostAsync: async (options) => { const initialized: IInitializedRushReporterHost = await initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), env: {}, stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }); selection = initialized.selection; return initialized; }, executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; receivedArgv = [...process.argv]; return launchOptions.reporterCloseAsync(); }, processLifecycle: createTestProcessLifecycle() }); expect(selection).toMatchObject({ reporter: 'plaintext', logLevel: 'verbose', outputs: [], enabled: true, reporterControlsOwnedByFrontend: false, reporterValueFlagsToStrip: [] }); expect(receivedArgv).toEqual(process.argv); } finally { process.argv = originalArgv; } }); it('closes exactly once when the engine rejects', async () => { const closeAsync: jest.Mock, [number?]> = jest.fn(async () => undefined); const initialized: IInitializedRushReporterHost = await createEnabledHostAsync(closeAsync); await expect( launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async () => initialized, executeCurrentRush: () => Promise.reject(new Error('engine rejected')), processLifecycle: createTestProcessLifecycle() }) ).rejects.toThrow('engine rejected'); expect(closeAsync).toHaveBeenCalledTimes(1); }); it('closes exactly once when command selection fails', async () => { const order: string[] = []; const initialized: IInitializedRushReporterHost = await createInitializedHostAsync(order); const originalArgv: string[] = process.argv; process.argv = ['node', 'rush', 'build']; try { await expect( launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: {} as typeof import('@microsoft/rush-lib'), initializeReporterHostAsync: async () => initialized, processLifecycle: createTestProcessLifecycle() }) ).rejects.toThrow('Unable to find the "Rush" entry point'); expect(order).toEqual(['host', 'close']); } finally { process.argv = originalArgv; } }); it('preserves the command failure when reporter close also fails', async () => { const initialized: IInitializedRushReporterHost = await createEnabledHostAsync(async () => { throw new Error('close failed'); }); const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); await expect( launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async () => initialized, executeCurrentRush: () => Promise.reject(new Error('command failed')), processLifecycle }) ).rejects.toThrow('command failed'); expect(processLifecycle.exitCodes).toEqual([1]); expect(processLifecycle.closeErrors).toEqual([expect.objectContaining({ message: 'close failed' })]); }); it.each(['rush', 'rushx', 'rush-pnpm'])( 'does not install lifecycle listeners for the disabled %s path', async (commandName) => { const originalArgv: string[] = process.argv; process.argv = [ 'node', commandName, 'custom', '--reporter', 'junit', '--output', 'custom.zip', '--log-level', 'custom', '--verbose' ]; const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); let receivedArgv: string[] | undefined; try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: (options) => initializeRushReporterHostAsync({ ...options, argv: process.argv.slice(2), env: {}, commandName: commandName as 'rush' | 'rushx' | 'rush-pnpm', stdout: { isTTY: false, write: () => undefined }, includeDefaultFileReporter: false }), executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; receivedArgv = [...process.argv]; return launchOptions.reporterCloseAsync(); }, processLifecycle }); expect(receivedArgv).toEqual(process.argv); expect(processLifecycle.beforeExitListener).toBeUndefined(); expect(processLifecycle.signalListeners.size).toBe(0); } finally { process.argv = originalArgv; } } ); it('uses a bounded close before preserving signal termination', async () => { let resolveClose: (() => void) | undefined; const closePromise: Promise = new Promise((resolve: () => void) => { resolveClose = resolve; }); const closeAsync: jest.Mock, [number?]> = jest.fn(() => closePromise); const initialized: IInitializedRushReporterHost = await createEnabledHostAsync(closeAsync); const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async () => initialized, executeCurrentRush: () => undefined, processLifecycle }); processLifecycle.signalListeners.get('SIGTERM')!(); await Promise.resolve(); expect(closeAsync).toHaveBeenCalledTimes(1); expect(closeAsync).toHaveBeenCalledWith(2000); expect(processLifecycle.terminatedSignals).toEqual([]); resolveClose!(); await closePromise; await new Promise((resolve: () => void) => setImmediate(resolve)); expect(processLifecycle.terminatedSignals).toEqual(['SIGTERM']); expect(processLifecycle.signalListeners.size).toBe(0); expect(processLifecycle.beforeExitListener).toBeUndefined(); }); it('enforces the signal deadline when a longer close is already in flight', async () => { jest.useFakeTimers(); const closeAsync: jest.Mock, [number?]> = jest.fn(() => new Promise(() => undefined)); const initialized: IInitializedRushReporterHost = await createEnabledHostAsync(closeAsync); const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async () => initialized, executeCurrentRush: (version, selectedRushLib, launchOptions) => { void version; void selectedRushLib; void launchOptions.reporterCloseAsync(); }, processLifecycle }); await Promise.resolve(); expect(closeAsync).toHaveBeenCalledWith(undefined); processLifecycle.signalListeners.get('SIGTERM')!(); await jest.advanceTimersByTimeAsync(1999); expect(processLifecycle.terminatedSignals).toEqual([]); await jest.advanceTimersByTimeAsync(1); expect(processLifecycle.terminatedSignals).toEqual(['SIGTERM']); expect(processLifecycle.closeErrors).toEqual([ expect.objectContaining({ message: 'Reporter close exceeded the 2000ms signal deadline.' }) ]); expect(closeAsync).toHaveBeenCalledTimes(1); } finally { jest.useRealTimers(); } }); it.each(['flush', 'close'] as const)( 'uses one signal deadline when the reporter %s phase hangs', async (hangingPhase) => { jest.useFakeTimers(); const initialized: IInitializedRushReporterHost = await createPhaseHangingHostAsync(hangingPhase); const processLifecycle: ITestProcessLifecycle = createTestProcessLifecycle(); try { await launchRushFrontendAsync({ currentPackageVersion: '5.178.1', rushVersionToLoad: undefined, configuration: undefined, launchOptions: { isManaged: false }, currentRushLib: rushLib, initializeReporterHostAsync: async () => initialized, executeCurrentRush: () => undefined, processLifecycle }); processLifecycle.signalListeners.get('SIGINT')!(); await jest.advanceTimersByTimeAsync(1999); expect(processLifecycle.terminatedSignals).toEqual([]); await jest.advanceTimersByTimeAsync(1); expect(processLifecycle.terminatedSignals).toEqual(['SIGINT']); expect(processLifecycle.closeErrors).toEqual([ expect.objectContaining({ message: 'Reporter close exceeded the 2000ms signal deadline.' }) ]); } finally { jest.useRealTimers(); } } ); });