From 312a5d9f30ff95bfacd70ff69b27128173d14aa0 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Sat, 28 Jan 2023 15:47:10 +0100 Subject: [PATCH] cli: make sure max workers workers are set to at least 2 by default Signed-off-by: Patrik Oldsberg --- .changeset/chatty-owls-care.md | 2 +- packages/cli/src/commands/repo/test.ts | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.changeset/chatty-owls-care.md b/.changeset/chatty-owls-care.md index 84b6fc76f6..9f39f51bb5 100644 --- a/.changeset/chatty-owls-care.md +++ b/.changeset/chatty-owls-care.md @@ -2,4 +2,4 @@ '@backstage/cli': patch --- -The `backstage-cli repo test` command now sets a default Jest `--workerIdleMemoryLimit` of 1GB. This is the recommended workaround for dealing with Jest workers leaking memory and eventually hitting the heap limit. +The `backstage-cli repo test` command now sets a default Jest `--workerIdleMemoryLimit` of 1GB. If needed to ensure that tests are not run in band, `--maxWorkers=2` is set as well. This is the recommended workaround for dealing with Jest workers leaking memory and eventually hitting the heap limit. diff --git a/packages/cli/src/commands/repo/test.ts b/packages/cli/src/commands/repo/test.ts index 69b3d914ed..22f1a68745 100644 --- a/packages/cli/src/commands/repo/test.ts +++ b/packages/cli/src/commands/repo/test.ts @@ -14,6 +14,7 @@ * limitations under the License. */ +import os from 'os'; import { Command, OptionValues } from 'commander'; import { PackageGraph } from '../../lib/monorepo'; import { paths } from '../../lib/paths'; @@ -91,6 +92,19 @@ export async function command(opts: OptionValues, cmd: Command): Promise { args.push('--workerIdleMemoryLimit=1000M'); } + // In order for the above worker memory limit to work we need to make sure the worker + // count is set to at least 2, as the tests will otherwise run in-band. + // Depending on the mode tests are run with the default count is either cpus-1, or cpus/2. + // This means that if we've got at 4 or more cores we'll always get at least 2 workers, but + // otherwise we need to set the worker count explicitly unless already done. + if ( + os.cpus().length <= 3 && + !includesAnyOf(args, '-i', '--runInBand') && + !args.some(arg => arg.match(/^(--maxWorkers|-w)/)) + ) { + args.push('--maxWorkers=2'); + } + if (opts.since) { removeOptionArg(args, '--since'); }