33// Runs grouped Vitest plans for one or more bundled plugins.
44import path from "node:path" ;
55import pMap from "p-map" ;
6+ import { collectVitestExcludePatterns } from "../test/vitest/vitest.pattern-file.ts" ;
67import {
78 createExtensionTestProcessTargetChunks ,
89 listExtensionTestFilesForRoots ,
@@ -19,6 +20,7 @@ import {
1920import { parsePositiveInt } from "./lib/numeric-options.mjs" ;
2021import { isDirectScriptRun , runVitestBatch } from "./lib/vitest-batch-runner.mts" ;
2122import type { VitestBatchRunParams } from "./lib/vitest-batch-runner.mts" ;
23+ import { prepareVitestRuntime } from "./lib/vitest-build-prerequisites.mts" ;
2224
2325const FS_MODULE_CACHE_PATH_ENV_KEY = "OPENCLAW_VITEST_FS_MODULE_CACHE_PATH" ;
2426const PARALLEL_ENV_KEY = "OPENCLAW_EXTENSION_BATCH_PARALLEL" ;
@@ -141,25 +143,9 @@ function addExactExcludePath(excludePaths: Set<string>, value: string) {
141143 */
142144export function parseExactVitestExcludePaths ( vitestArgs : string [ ] ) {
143145 const excludePaths = new Set < string > ( ) ;
144- for ( let index = 0 ; index < vitestArgs . length ; index += 1 ) {
145- const arg = vitestArgs [ index ] ;
146- if ( arg === undefined ) {
147- break ;
148- }
149- if ( arg === "--exclude" ) {
150- const value = vitestArgs [ index + 1 ] ;
151- if ( value && isExactExcludePath ( value ) ) {
152- addExactExcludePath ( excludePaths , value ) ;
153- }
154- index += 1 ;
155- continue ;
156- }
157- const prefix = "--exclude=" ;
158- if ( arg . startsWith ( prefix ) ) {
159- const value = arg . slice ( prefix . length ) ;
160- if ( value && isExactExcludePath ( value ) ) {
161- addExactExcludePath ( excludePaths , value ) ;
162- }
146+ for ( const value of collectVitestExcludePatterns ( vitestArgs ) ) {
147+ if ( isExactExcludePath ( value ) ) {
148+ addExactExcludePath ( excludePaths , value ) ;
163149 }
164150 }
165151 return excludePaths ;
@@ -178,46 +164,49 @@ function resolveGroupTargets(group: ExtensionTestPlanGroup, exactExcludePaths: S
178164 return testFiles . filter ( ( file ) => ! exactExcludePaths . has ( file ) ) ;
179165}
180166
181- async function runPlanGroup (
167+ function preparePlanGroup (
182168 group : ExtensionTestPlanGroup ,
183- params : {
184- env : NodeJS . ProcessEnv ;
185- groupIndex : number ;
186- runGroup : ( params : VitestBatchRunParams ) => Promise < number > ;
187- exactExcludePaths : Set < string > ;
188- allowEmptyAfterExclude : boolean ;
189- useDedicatedCache : boolean ;
190- vitestArgs : string [ ] ;
191- } ,
169+ groupIndex : number ,
170+ env : NodeJS . ProcessEnv ,
171+ vitestArgs : string [ ] ,
172+ exactExcludePaths : Set < string > ,
173+ useDedicatedCache : boolean ,
174+ ) {
175+ const targets = resolveGroupTargets ( group , exactExcludePaths ) ;
176+ const targetChunks =
177+ targets . length === 0
178+ ? [ ]
179+ : exactExcludePaths . size > 0
180+ ? shouldSplitExtensionTestProcesses ( group . config , vitestArgs )
181+ ? splitExtensionTestProcessTargets ( group . config , targets )
182+ : [ targets ]
183+ : createExtensionTestProcessTargetChunks ( group . config , group . roots , vitestArgs ) ;
184+ return {
185+ group,
186+ invocations : targetChunks . map ( ( chunk ) => ( {
187+ args : relativizeExtensionVitestArgs ( vitestArgs ) ,
188+ config : group . config ,
189+ env : createGroupEnv ( { baseEnv : env , group, groupIndex, useDedicatedCache } ) ,
190+ targets : chunk . map ( ( target ) => relativizeExtensionVitestPath ( target ) ) ,
191+ } ) ) ,
192+ } ;
193+ }
194+
195+ async function runPlanGroup (
196+ { group, invocations } : ReturnType < typeof preparePlanGroup > ,
197+ runGroup : ( params : VitestBatchRunParams ) => Promise < number > ,
198+ allowEmptyAfterExclude : boolean ,
192199) {
193- const targets = resolveGroupTargets ( group , params . exactExcludePaths ) ;
194- if ( targets . length === 0 ) {
200+ if ( invocations . length === 0 ) {
195201 console . error ( `[test-extension-batch] ${ group . config } : no test files remain after excludes` ) ;
196- return params . allowEmptyAfterExclude ? 0 : 1 ;
202+ return allowEmptyAfterExclude ? 0 : 1 ;
197203 }
198-
199- const targetChunks =
200- params . exactExcludePaths . size > 0
201- ? shouldSplitExtensionTestProcesses ( group . config , params . vitestArgs )
202- ? splitExtensionTestProcessTargets ( group . config , targets )
203- : [ targets ]
204- : createExtensionTestProcessTargetChunks ( group . config , group . roots , params . vitestArgs ) ;
205204 let finalExitCode = 0 ;
206- for ( const [ index , chunk ] of targetChunks . entries ( ) ) {
205+ for ( const [ index , invocation ] of invocations . entries ( ) ) {
207206 console . log (
208- `[test-extension-batch] ${ group . config } : ${ group . extensionIds . join ( ", " ) } (${ chunk . length } targets${ targetChunks . length > 1 ? `, chunk ${ index + 1 } /${ targetChunks . length } ` : "" } )` ,
207+ `[test-extension-batch] ${ group . config } : ${ group . extensionIds . join ( ", " ) } (${ invocation . targets . length } targets${ invocations . length > 1 ? `, chunk ${ index + 1 } /${ invocations . length } ` : "" } )` ,
209208 ) ;
210- const exitCode = await params . runGroup ( {
211- args : relativizeExtensionVitestArgs ( params . vitestArgs ) ,
212- config : group . config ,
213- env : createGroupEnv ( {
214- baseEnv : params . env ,
215- group,
216- groupIndex : params . groupIndex ,
217- useDedicatedCache : params . useDedicatedCache ,
218- } ) ,
219- targets : chunk . map ( ( target ) => relativizeExtensionVitestPath ( target ) ) ,
220- } ) ;
209+ const exitCode = await runGroup ( invocation ) ;
221210 if ( exitCode !== 0 && finalExitCode === 0 ) {
222211 finalExitCode = exitCode ;
223212 }
@@ -232,14 +221,19 @@ export async function runExtensionBatchPlan(
232221 batchPlan : ExtensionBatchPlan ,
233222 params : {
234223 allowEmptyAfterExclude ?: boolean ;
224+ expandExactExcludes ?: boolean ;
235225 env ?: NodeJS . ProcessEnv ;
236226 runGroup ?: ( params : VitestBatchRunParams ) => Promise < number > ;
237227 vitestArgs ?: string [ ] ;
238228 } = { } ,
239229) {
240230 const env = params . env ?? process . env ;
241231 const vitestArgs = params . vitestArgs ?? [ ] ;
242- const exactExcludePaths = parseExactVitestExcludePaths ( vitestArgs ) ;
232+ // Single-plugin CLI historically leaves exact exclusions to Vitest.
233+ const exactExcludePaths =
234+ params . expandExactExcludes === false
235+ ? new Set < string > ( )
236+ : parseExactVitestExcludePaths ( vitestArgs ) ;
243237 const runGroup = params . runGroup ?? runVitestBatch ;
244238 const parallelism = resolveExtensionBatchParallelism ( batchPlan . planGroups . length , env ) ;
245239 const orderedGroups = orderPlanGroups ( batchPlan . planGroups , parallelism ) ;
@@ -250,22 +244,31 @@ export async function runExtensionBatchPlan(
250244 console . log ( `[test-extension-batch] Running up to ${ parallelism } config groups in parallel` ) ;
251245 }
252246
247+ const preparedGroups = orderedGroups . map ( ( group , index ) =>
248+ preparePlanGroup ( group , index , env , vitestArgs , exactExcludePaths , useDedicatedCache ) ,
249+ ) ;
250+ // No reader may start while a shared generation is being replaced. Select
251+ // from the exact emitted chunks, including existing exact-exclude expansion.
252+ const preparationCode = await prepareVitestRuntime (
253+ preparedGroups . flatMap ( ( { invocations } ) =>
254+ invocations . map ( ( { config, args, targets } ) => ( {
255+ configs : [ config ] ,
256+ cli : { args : [ ...args , ...targets ] , dir : "extensions" , env } ,
257+ } ) ) ,
258+ ) ,
259+ env ,
260+ ) ;
261+ if ( preparationCode !== 0 ) {
262+ return preparationCode ;
263+ }
253264 let exitCode = 0 ;
254265 await pMap (
255- orderedGroups ,
256- async ( group , groupIndex ) => {
266+ preparedGroups ,
267+ async ( group ) => {
257268 if ( exitCode !== 0 ) {
258269 return ;
259270 }
260- const groupExitCode = await runPlanGroup ( group , {
261- env,
262- groupIndex,
263- runGroup,
264- exactExcludePaths,
265- allowEmptyAfterExclude,
266- useDedicatedCache,
267- vitestArgs,
268- } ) ;
271+ const groupExitCode = await runPlanGroup ( group , runGroup , allowEmptyAfterExclude ) ;
269272 if ( groupExitCode !== 0 && exitCode === 0 ) {
270273 exitCode = groupExitCode ;
271274 }
0 commit comments