From 947dac48f28aa5b85d50ccc1883ecd01ff61c6cb Mon Sep 17 00:00:00 2001 From: Vincent Koc Date: Wed, 18 Mar 2026 08:58:29 -0700 Subject: [PATCH] Tests: cap shards for explicit file lanes --- scripts/test-parallel.mjs | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/scripts/test-parallel.mjs b/scripts/test-parallel.mjs index 4698209ad62..dc7158a4cb7 100644 --- a/scripts/test-parallel.mjs +++ b/scripts/test-parallel.mjs @@ -367,6 +367,10 @@ const parsePassthroughArgs = (args) => { }; const { fileFilters: passthroughFileFilters, optionArgs: passthroughOptionArgs } = parsePassthroughArgs(passthroughArgs); +const countExplicitEntryFilters = (entryArgs) => { + const { fileFilters } = parsePassthroughArgs(entryArgs.slice(2)); + return fileFilters.length > 0 ? fileFilters.length : null; +}; const passthroughRequiresSingleRun = passthroughOptionArgs.some((arg) => { if (!arg.startsWith("-")) { return false; @@ -757,15 +761,35 @@ const runOnce = (entry, extraArgs = []) => }); const run = async (entry, extraArgs = []) => { - if (shardCount <= 1) { + const explicitFilterCount = countExplicitEntryFilters(entry.args); + // Wrapper-generated singleton/small-file lanes should not ask Vitest to shard + // into more buckets than there are explicit test filters. + const effectiveShardCount = + explicitFilterCount === null ? shardCount : Math.min(shardCount, explicitFilterCount); + + if (effectiveShardCount <= 1) { + if (shardIndexOverride !== null && shardIndexOverride > effectiveShardCount) { + return 0; + } return runOnce(entry, extraArgs); } if (shardIndexOverride !== null) { - return runOnce(entry, ["--shard", `${shardIndexOverride}/${shardCount}`, ...extraArgs]); + if (shardIndexOverride > effectiveShardCount) { + return 0; + } + return runOnce(entry, [ + "--shard", + `${shardIndexOverride}/${effectiveShardCount}`, + ...extraArgs, + ]); } - for (let shardIndex = 1; shardIndex <= shardCount; shardIndex += 1) { + for (let shardIndex = 1; shardIndex <= effectiveShardCount; shardIndex += 1) { // eslint-disable-next-line no-await-in-loop - const code = await runOnce(entry, ["--shard", `${shardIndex}/${shardCount}`, ...extraArgs]); + const code = await runOnce(entry, [ + "--shard", + `${shardIndex}/${effectiveShardCount}`, + ...extraArgs, + ]); if (code !== 0) { return code; }