Skip to content
Open
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
22 changes: 13 additions & 9 deletions apps/rush-cli-client/src/test/launchClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import {
import type { DaemonClientOutcome } from '@rushstack/rush-client-core';
import { RushDaemonHost, WorkspaceSession } from '@rushstack/rush-daemon';
import {
captureTestProcessIdentity,
removeTestFolderAsync,
waitForTestProcessExitAsync
waitForTestProcessExitAsync,
type ITestProcessIdentity
} from '@rushstack/rush-daemon/lib/test/TestProcessExit';
import { captureTestDaemonListenerAsync } from '@rushstack/rush-daemon/lib/test/TestDaemonListener';
import {
Expand Down Expand Up @@ -67,11 +69,11 @@ describe('standalone rushx fallback', () => {
let logFilePath: string;
let host: RushDaemonHost | undefined;
let invocationClosures: Promise<unknown>[];
let daemonPids: Set<number>;
let daemonProcesses: Map<number, ITestProcessIdentity>;

beforeEach(() => {
invocationClosures = [];
daemonPids = new Set();
daemonProcesses = new Map();
folder = fs.mkdtempSync(path.join(os.tmpdir(), 'rush-cli-client-'));
project = path.join(folder, 'project');
fs.mkdirSync(project);
Expand Down Expand Up @@ -105,11 +107,13 @@ describe('standalone rushx fallback', () => {
const closingHost: RushDaemonHost | undefined = host;
const closingFolder: string = folder;
const closingLogFilePath: string = logFilePath;
const closingDaemonPids: Set<number> = daemonPids;
const closingDaemonProcesses: Map<number, ITestProcessIdentity> = daemonProcesses;
host = undefined;
await Promise.all(invocationClosures);
await closingHost?.closeAsync();
await Promise.all(Array.from(closingDaemonPids, (pid) => waitForTestProcessExitAsync(pid)));
await Promise.all(
Array.from(closingDaemonProcesses.values(), (identity) => waitForTestProcessExitAsync(identity))
);
await removeTestFolderAsync(closingLogFilePath, true);
await removeTestFolderAsync(closingFolder);
}, CLEANUP_TIMEOUT_MS);
Expand All @@ -122,7 +126,7 @@ describe('standalone rushx fallback', () => {
environmentOverrides: NodeJS.ProcessEnv = {},
onStderr?: (stderr: string) => void
): Promise<IInvocationResult> {
const invocationDaemonPids: Set<number> = daemonPids;
const invocationDaemonProcesses: Map<number, ITestProcessIdentity> = daemonProcesses;
const entry: string = client
? path.resolve(__dirname, managementArgs ? '../../bin/rush-client' : '../../bin/rushx-client')
: path.resolve(path.dirname(require.resolve('@microsoft/rush/package.json')), 'bin/rushx');
Expand Down Expand Up @@ -165,7 +169,7 @@ describe('standalone rushx fallback', () => {
if (typeof pid !== 'number' || !Number.isSafeInteger(pid) || pid <= 0) {
throw new Error('The started fixture daemon did not report a valid PID.');
}
if (pid !== process.pid) invocationDaemonPids.add(pid);
if (pid !== process.pid) invocationDaemonProcesses.set(pid, captureTestProcessIdentity(pid));
}
return { code: typeof code === 'number' ? code : undefined, stdout, stderr };
}
Expand Down Expand Up @@ -431,7 +435,7 @@ describe('standalone rushx fallback', () => {
} else {
expect((await invokeAsync(true, false, false, ['daemon', 'stop'])).code).toBe(0);
}
await waitForTestProcessExitAsync(pid);
await waitForTestProcessExitAsync(daemonProcesses.get(pid)!);
expect(fs.existsSync(paths.lockfilePath)).toBe(false);
expectNoDaemonRunning(
await invokeAsync(true, true, false, ['daemon', 'status'], { RUSH_DAEMON_AUTO_START: '1' }),
Expand Down Expand Up @@ -822,7 +826,7 @@ describe('standalone rushx fallback', () => {
expect(started.code).toBe(0);
const { pid }: { pid: number } = JSON.parse(started.stdout);
expect((await invokeAsync(true, false, false, ['daemon', 'stop'])).code).toBe(0);
await waitForTestProcessExitAsync(pid);
await waitForTestProcessExitAsync(daemonProcesses.get(pid)!);
expectNoDaemonRunning(
await invokeAsync(true, true, false, ['daemon', 'status'], { RUSH_DAEMON_AUTO_START: '1' }),
NEXT_COMMAND_STARTS
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@microsoft/rush",
"comment": "Add pure execution planning and opt-in version 1 graph export with executable commands, explicit environment overrides and per-operation metadata. Preserve the legacy rush-buildxl-graph-plugin contract by default.",
"type": "minor"
}
],
"packageName": "@microsoft/rush"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/rush-cli-client",
"comment": "Track fixture process identity during daemon test cleanup.",
"type": "none"
}
],
"packageName": "@rushstack/rush-cli-client"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/rush-client-core",
"comment": "Preserve completed daemon fixture joins during test cleanup.",
"type": "none"
}
],
"packageName": "@rushstack/rush-client-core"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@rushstack/rush-daemon",
"comment": "Update the operation graph test double for external execution planning, track fixture process identity during test cleanup, and assert stalled worker behavior without wall-clock timing.",
"type": "none"
}
],
"packageName": "@rushstack/rush-daemon"
}
5 changes: 5 additions & 0 deletions common/reviews/api/rush-buildxl-graph-plugin.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,27 @@ export interface IBuildXLRushGraph {
// (undocumented)
repoSettings: {
commonTempFolder: string;
executionContractVersion?: 1;
};
}

// @public (undocumented)
export interface IDropGraphPluginOptions {
buildXLCommandNames: string[];
executionContractVersion?: 1;
}

// @public (undocumented)
export interface IGraphNode {
cacheable?: false;
command: string;
dependencies: string[];
environmentVariables?: Record<string, string>;
id: string;
outputFolderNames?: string[];
package: string;
task: string;
warningsAreAllowed?: true;
workingDirectory: string;
}

Expand Down
8 changes: 8 additions & 0 deletions common/reviews/api/rush-lib.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ export function getWorkspaceRequestOperationEnvironment(hostEnvironment: Readonl

// @alpha (undocumented)
export interface IBaseOperationExecutionResult {
readonly environmentOverrides?: Readonly<Record<string, string>>;
getStateHash(): string;
getStateHashComponents(): IOperationStateHashComponents;
readonly metadataFolderPath: string;
Expand Down Expand Up @@ -475,6 +476,7 @@ export interface ICobuildLockProvider {
// @alpha
export interface IConfigurableOperation extends IBaseOperationExecutionResult {
enabled: boolean;
environmentOverrides?: Record<string, string>;
shouldRunnerPersist: boolean;
}

Expand Down Expand Up @@ -771,6 +773,7 @@ export interface IOperationGraph {
addTerminalDestination(destination: TerminalWritable): void;
allowOversubscription: boolean;
closeRunnersAsync(operations?: Iterable<Operation>): Promise<void>;
createExecutionPlanAsync?(options: IOperationGraphIterationOptions): Promise<ReadonlyMap<Operation, IOperationExecutionResult>>;
debugMode: boolean;
deleteResults?(operations: Iterable<Operation>): void;
discardScheduledIteration(): boolean;
Expand Down Expand Up @@ -891,6 +894,7 @@ export interface IOperationRunner {
cacheable: boolean;
closeAsync?(): Promise<void>;
executeAsync(context: IOperationRunnerContext, lastState?: IOperationLastState): Promise<OperationStatus>;
getCommandForExecution?(): string;
getConfigHash(): string;
readonly isActive?: boolean;
readonly isNoOp?: boolean;
Expand Down Expand Up @@ -1679,6 +1683,10 @@ export class PhasedCommandEngineUsageError extends Error {

// @alpha
export class PhasedCommandHooks {
readonly beforeExecuteGraphAsync: AsyncSeriesBailHook<[
IOperationGraph,
IOperationGraphContext
], true | undefined | void>;
readonly createOperationsAsync: AsyncSeriesWaterfallHook<[
Set<Operation>,
ICreateOperationsContext
Expand Down
77 changes: 77 additions & 0 deletions libraries/rush-client-core/src/test/TestProcessExit.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.

import { spawn } from 'node:child_process';
import { once } from 'node:events';

import { createTestProcessExitWaiter } from './TestProcessExit';

describe(createTestProcessExitWaiter.name, () => {
const pid: number = process.pid + 1;
const missing: NodeJS.ErrnoException = Object.assign(new Error('Process exited'), { code: 'ESRCH' });
let kill: jest.SpyInstance;

beforeEach(() => {
kill = jest.spyOn(process, 'kill').mockImplementation(() => {
throw missing;
});
});

afterEach(() => {
kill.mockRestore();
});

it('does not probe a completed lifetime when its PID is reused', async () => {
const waitForExit = createTestProcessExitWaiter(pid);
await waitForExit();
kill.mockImplementation(() => {
throw new Error('The PID now belongs to another process');
});
await expect(waitForExit()).resolves.toBeUndefined();
expect(kill).toHaveBeenCalledTimes(1);
});

it('still probes a newly registered lifetime with the same PID', async () => {
await createTestProcessExitWaiter(pid)();
const unavailable: Error = new Error('Cannot inspect new fixture');
kill.mockImplementation(() => {
throw unavailable;
});
await expect(createTestProcessExitWaiter(pid)()).rejects.toBe(unavailable);
expect(kill).toHaveBeenCalledTimes(2);
});

it('does not turn a failed join into successful cleanup', async () => {
const denied: NodeJS.ErrnoException = Object.assign(new Error('Cannot inspect fixture'), {
code: 'EPERM'
});
kill.mockImplementation(() => {
throw denied;
});
const waitForExit = createTestProcessExitWaiter(pid);
await expect(waitForExit()).rejects.toBe(denied);
await expect(waitForExit()).rejects.toBe(denied);
});
});

it('waits for fixture process exit rather than its work-finished marker', async () => {
const child = spawn(process.execPath, ['-e', "process.stdout.write('finished'); process.stdin.resume();"], {
stdio: 'pipe'
});
const closed: Promise<unknown[]> = once(child, 'close');
try {
await once(child.stdout, 'data');
let exited: boolean = false;
const waiting: Promise<void> = createTestProcessExitWaiter(child.pid!)().then(() => {
exited = true;
});
await new Promise<void>((resolve) => setImmediate(resolve));
expect(exited).toBe(false);
child.stdin.end();
await Promise.all([closed, waiting]);
expect(exited).toBe(true);
} finally {
if (child.exitCode === null && child.signalCode === null) child.kill('SIGKILL');
await closed;
}
});
6 changes: 6 additions & 0 deletions libraries/rush-client-core/src/test/TestProcessExit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export async function removeTestFolderAsync(folder: string, force: boolean = fal
}
}

/** Create once per fixture lifetime, not once per PID: a completed join must not probe a reused PID. */
export function createTestProcessExitWaiter(pid: number): () => Promise<void> {
let exited: Promise<void> | undefined;
return () => (exited ??= waitForTestProcessExitAsync(pid));
}

/** Only for PIDs captured from this test's own spawned fixtures, never for daemon ownership reclamation. */
export async function waitForTestProcessExitAsync(pid: number): Promise<void> {
if (!Number.isSafeInteger(pid) || pid <= 0 || pid === process.pid) {
Expand Down
41 changes: 15 additions & 26 deletions libraries/rush-client-core/src/test/connectOrStartDaemon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ import {
startOrphanedOperationAsync,
stopOperationIfRunning
} from './OrphanedOperation';
import { waitForTestProcessExitAsync } from './TestProcessExit';
import { createTestProcessExitWaiter, waitForTestProcessExitAsync } from './TestProcessExit';

/** How long after a launch a client may take over its reservation once the helper exited. */
const RELAUNCH_DELAY_MS: number = 15000;
Expand All @@ -74,9 +74,11 @@ describe('detached daemon startup', () => {
let paths: IDaemonPaths;
let options: IConnectOrStartDaemonOptions;
let starterProcesses: ChildProcess[];
let daemonExits: Map<number, () => Promise<void>>;

beforeEach(() => {
starterProcesses = [];
daemonExits = new Map();
FileSystem.ensureFolder(TEST_ROOT);
folder = FileSystem.getRealPath(fs.mkdtempSync(`${TEST_ROOT}/s`));
paths = {
Expand Down Expand Up @@ -119,7 +121,7 @@ describe('detached daemon startup', () => {
if (FileSystem.exists(path.join(folder, 'starts'))) {
FileSystem.writeFile(path.join(folder, 'stop'), '');
const pids: string[] = FileSystem.readFile(path.join(folder, 'starts')).trim().split('\n');
await Promise.all(pids.map((pid) => waitForTestProcessExitAsync(Number(pid))));
await Promise.all(pids.map((pid, index) => waitForDaemonExitAsync(Number(pid), index)));
expect(pids.every((pid) => FileSystem.exists(path.join(folder, `stopped-${pid}`)))).toBe(true);
}
// A test that expects a fixture daemon to fail checks and removes this record.
Expand All @@ -130,6 +132,16 @@ describe('detached daemon startup', () => {
}
});

function waitForDaemonExitAsync(pid: number, index: number): Promise<void> {
// Each append-only "starts" record owns a lifetime, even when a later record reuses its PID.
let waitForExit: (() => Promise<void>) | undefined = daemonExits.get(index);
if (!waitForExit) {
waitForExit = createTestProcessExitWaiter(pid);
daemonExits.set(index, waitForExit);
}
return waitForExit();
}

function startClient(startOptions: IConnectOrStartDaemonOptions = options): {
child: ChildProcess;
result: Promise<{ code: number | null; stderr: string }>;
Expand Down Expand Up @@ -743,7 +755,7 @@ describe('detached daemon startup', () => {
await running.closeAsync();
}
// With the reservation resolved, a later start launches a new daemon instead of being refused.
await waitForTestProcessExitAsync(daemonPid);
await waitForDaemonExitAsync(daemonPid, 0);
const successor: DaemonClient = await connectOrStartDaemonAsync(options);
try {
expect((await successor.status).pid).not.toBe(daemonPid);
Expand Down Expand Up @@ -848,29 +860,6 @@ describe('detached daemon startup', () => {
}
});

it('waits for fixture process exit rather than its work-finished marker', async () => {
const child = spawn(
process.execPath,
['-e', "process.stdout.write('finished'); process.stdin.resume();"],
{
cwd: folder,
stdio: 'pipe'
}
);
starterProcesses.push(child);
const closed: Promise<unknown[]> = once(child, 'close');
await once(child.stdout, 'data');
let exited: boolean = false;
const waiting: Promise<void> = waitForTestProcessExitAsync(child.pid!).then(() => {
exited = true;
});
await new Promise<void>((resolve) => setImmediate(resolve));
expect(exited).toBe(false);
child.stdin.end();
await Promise.all([closed, waiting]);
expect(exited).toBe(true);
});

it('releases a helper whose IPC handoff failed without reclaiming its reservation', async () => {
const childProcess = jest.requireActual<typeof import('node:child_process')>('node:child_process');
const originalSpawn: typeof spawn = childProcess.spawn;
Expand Down
16 changes: 12 additions & 4 deletions libraries/rush-daemon/src/test/OutputFolderDigestPool.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { spawnSync, type SpawnSyncReturns } from 'node:child_process';
import * as fs from 'node:fs';
import * as os from 'node:os';
import * as path from 'node:path';
import { Worker } from 'node:worker_threads';

import { digestOutputFolders, type IOutputFolderDigest, type IOutputFolderSet } from '../OutputFolderDigest';
import {
Expand Down Expand Up @@ -328,10 +329,17 @@ describe(OutputFolderDigestPool.name, () => {
expect(pool.stalled).toBe(true);
expect(dispose).toHaveBeenCalled();

const startTimeMs: number = performance.now();
expect([...pool.start(folderSets).finish()]).toEqual(withoutDigests(PROJECT_COUNT));
expect(pool.digest(folderSets)).toEqual(digestOnCallingThread(folderSets));
expect(performance.now() - startTimeMs).toBeLessThan(200);
const wait: jest.SpyInstance = jest.spyOn(Atomics, 'wait');
const postMessage: jest.SpyInstance = jest.spyOn(Worker.prototype, 'postMessage');
try {
expect([...pool.start(folderSets).finish()]).toEqual(withoutDigests(PROJECT_COUNT));
expect(pool.digest(folderSets)).toEqual(digestOnCallingThread(folderSets));
expect(wait).not.toHaveBeenCalled();
expect(postMessage).not.toHaveBeenCalled();
} finally {
wait.mockRestore();
postMessage.mockRestore();
}
} finally {
pool.dispose();
}
Expand Down
Loading
Loading