File size: 5,238 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
#!/usr/bin/env node
import { basename, dirname } from 'node:path';
import util from 'node:util';
import glob from 'glob';
import runTask from './teamcity-task-runner.mjs';
// TEMPORARY: These apps *should* be type-checked, but there are existing issues that need to be
// resolved.
const APPS_EXCLUDED_FROM_TYPE_CHECK = [
'happy-blocks',
'o2-blocks',
'odyssey-stats',
'notifications',
'wpcom-block-editor',
];
const globPromise = util.promisify( glob );
// Promise.allSettled with some extra code to throw an error.
async function completeTasks( promises ) {
const results = await Promise.allSettled( promises );
const exitCodes = results
.filter( ( { status } ) => status === 'rejected' )
.map( ( { reason } ) => reason );
if ( exitCodes.length ) {
throw exitCodes[ 0 ];
}
}
function withTscInfo( { cmd, id } ) {
return {
testId: id,
name: 'yarn',
args: cmd,
env: { NODE_ENV: 'test' },
};
}
function withUnitTestInfo( cmd ) {
return {
testId: cmd.split( ' ' )[ 0 ],
name: 'yarn',
args: `${ cmd } --ci --reporters=default --reporters=jest-teamcity --silent`,
};
}
const [ packagesTsconfigs, appsTsconfigs ] = await Promise.all(
[ 'packages', 'apps' ].map( ( path ) => globPromise( `${ path }/*/tsconfig.json` ) )
);
const isTypeCheckedApp = ( path ) =>
! APPS_EXCLUDED_FROM_TYPE_CHECK.includes( basename( dirname( path ) ) );
const tscPackages = withTscInfo( {
cmd: `tsc --build ${ packagesTsconfigs.join( ' ' ) }`,
id: 'type_check_packages',
} );
const tscCommands = [
{ cmd: 'tsc --noEmit --project build-tools/tsconfig.json', id: 'type_check_build_tools' },
{ cmd: 'tsc --noEmit --project client/tsconfig.json', id: 'type_check_client' },
{ cmd: 'tsc --noEmit --project test/e2e/tsconfig.json', id: 'type_check_tests' },
...appsTsconfigs.filter( isTypeCheckedApp ).map( ( path ) => ( {
cmd: `tsc --noEmit --project ${ path }`,
id: `type_check_apps_${ basename( dirname( path ) ) }`,
} ) ),
].map( withTscInfo );
// When Jest runs without --maxWorkers, each instance of Jest will try to use all
// cores available. (Which is a lot in our CI.) This isn't a problem per se, because
// everything ends up completing pretty quickly. However, with 100% CPU usage for
// most of the test, it's possible for some tests which rely on i/o to time out.
// This causes flakey tests. As a result, we need to manage the number of workers
// manually so that there is some amount of margin.
//
// After some testing, I've found that 8+4 is a good setup. The largest task
// runs by itself with 8 cores, and the other tasks run one by one with 4 other
// cores. This leaves a final 4 cores free for tsc + any other tasks. This seems
// to result in the fastest overall completion time.
//
// --workerIdleMemoryLimit=512MB is added because of https://github.com/jestjs/jest/issues/11956
const testClient = withUnitTestInfo( 'test-client --maxWorkers=8 --workerIdleMemoryLimit=1GB' );
const testPackages = withUnitTestInfo( 'test-packages --maxWorkers=4 --workerIdleMemoryLimit=1GB' );
const testServer = withUnitTestInfo( 'test-server --maxWorkers=4 --workerIdleMemoryLimit=1GB' );
const testBuildTools = withUnitTestInfo(
'test-build-tools --maxWorkers=4 --workerIdleMemoryLimit=1GB'
);
// Includes ETK and Odyssey Stats, migrated here from their individual builds.
const testApps = withUnitTestInfo( 'test-apps --maxWorkers=1 --workerIdleMemoryLimit=1GB' );
const testWorkspaces = {
name: 'yarn',
args: 'workspaces foreach -A --verbose --parallel run storybook:start --ci --smoke-test',
testId: 'check_storybook',
};
try {
// Since this task is so much larger than the others, we give it a large amount
// of CPU and run it by itself. We let other tasks complete in parallel with
// less CPU since they'll still finish much more quickly.
const testClientTask = runTask( testClient );
// The async () wrapper is needed so that the Promise settles only after
// all tasks finish. If we instead use Promise.all with a chain of Promises,
// Promise.all would complete when the first Promise in the chain settles.
//
// One note about the tsc tasks is that tsc doesn't parallelize well. This means
// it doesn't expand to take advantage of more cores. As a result, it's the
// limiting factor for overall build speed. We need to give it just enough cores
// so that it runs as fast as possible, but leave enough to other tasks so that
// they can finish by the time tsc finishes. I found that using 12 cores for
// jest and the remaining for tsc and anything else accomplished this.
const tscTasks = ( async () => {
// This task is a prerequisite for the other tsc tasks, so it must run separately.
await runTask( tscPackages );
await completeTasks( tscCommands.map( runTask ) );
} )();
// Run these smaller tasks in serial to keep a healthy amount of CPU available for the other tasks.
const otherTestTasks = ( async () => {
await runTask( testPackages );
await runTask( testServer );
await runTask( testBuildTools );
await runTask( testWorkspaces );
await runTask( testApps );
} )();
await completeTasks( [ testClientTask, tscTasks, otherTestTasks ] );
} catch ( exitCode ) {
console.error( 'One or more tasks failed.' );
process.exit( exitCode );
}
|