11import { spawnSync } from 'node:child_process' ;
2- import { copyFileSync , existsSync , mkdirSync , readFileSync } from 'node:fs' ;
2+ import { copyFileSync , existsSync , mkdirSync , readFileSync , readdirSync } from 'node:fs' ;
33import { createRequire } from 'node:module' ;
44import path from 'node:path' ;
55import { fileURLToPath } from 'node:url' ;
@@ -9,8 +9,8 @@ const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../
99const cliDistDir = path . join ( repoRoot , 'packages' , 'cli' , 'dist' ) ;
1010const cliBinPath = path . join ( cliDistDir , 'bin.js' ) ;
1111const testCliPath = path . join ( repoRoot , 'packages' , 'test' , 'dist' , 'cli.js' ) ;
12- const localVpPath = path . join ( repoRoot , 'target' , 'debug' , isWindows ? 'vp.exe' : 'vp' ) ;
13- const localVpBinDir = path . dirname ( localVpPath ) ;
12+ const localVpBinaryName = isWindows ? 'vp.exe' : 'vp' ;
13+ const defaultLocalVpPath = path . join ( repoRoot , 'target' , 'debug' , localVpBinaryName ) ;
1414const viteRepoDir = path . join ( repoRoot , 'vite' ) ;
1515const legacyViteRepoDir = path . join ( repoRoot , 'rolldown-vite' ) ;
1616const rolldownRepoDir = path . join ( repoRoot , 'rolldown' ) ;
@@ -32,22 +32,71 @@ type CommandOptions = {
3232 hint ?: string ;
3333} ;
3434
35+ type LocalCliArtifacts = {
36+ vpPath : string ;
37+ vpBinDir : string ;
38+ } ;
39+
3540function failMissing ( pathname : string , description : string ) : never {
3641 console . error ( `Missing ${ description } : ${ pathname } ` ) ;
3742 console . error ( `Run "${ bootstrapHint } " from a fresh clone, or "${ buildHint } " after setup.` ) ;
3843 process . exit ( 1 ) ;
3944}
4045
41- function ensureLocalCliReady ( options ?: { needsTestCli ?: boolean } ) {
46+ function getTargetDirs ( ) : string [ ] {
47+ return [
48+ ...new Set (
49+ [ process . env . CARGO_TARGET_DIR , path . join ( repoRoot , 'target' ) ] . filter (
50+ ( targetDir ) : targetDir is string => Boolean ( targetDir ) ,
51+ ) ,
52+ ) ,
53+ ] ;
54+ }
55+
56+ function findLocalVpBinary ( ) : string | null {
57+ const profiles = [ 'debug' , 'release' ] ;
58+
59+ for ( const targetDir of getTargetDirs ( ) ) {
60+ for ( const profile of profiles ) {
61+ const directPath = path . join ( targetDir , profile , localVpBinaryName ) ;
62+ if ( existsSync ( directPath ) ) {
63+ return directPath ;
64+ }
65+ }
66+
67+ try {
68+ for ( const entry of readdirSync ( targetDir ) . toSorted ( ) ) {
69+ for ( const profile of profiles ) {
70+ const nestedPath = path . join ( targetDir , entry , profile , localVpBinaryName ) ;
71+ if ( existsSync ( nestedPath ) ) {
72+ return nestedPath ;
73+ }
74+ }
75+ }
76+ } catch {
77+ continue ;
78+ }
79+ }
80+
81+ return null ;
82+ }
83+
84+ function ensureLocalCliReady ( options ?: { needsTestCli ?: boolean } ) : LocalCliArtifacts {
4285 if ( ! existsSync ( cliBinPath ) ) {
4386 failMissing ( cliBinPath , 'local CLI bundle' ) ;
4487 }
45- if ( ! existsSync ( localVpPath ) ) {
46- failMissing ( localVpPath , 'local debug vp binary' ) ;
88+ const vpPath = findLocalVpBinary ( ) ;
89+ if ( ! vpPath ) {
90+ failMissing ( defaultLocalVpPath , 'local vp binary' ) ;
4791 }
4892 if ( options ?. needsTestCli && ! existsSync ( testCliPath ) ) {
4993 failMissing ( testCliPath , 'local test CLI bundle' ) ;
5094 }
95+
96+ return {
97+ vpPath,
98+ vpBinDir : path . dirname ( vpPath ) ,
99+ } ;
51100}
52101
53102function localCliEnv ( ) : NodeJS . ProcessEnv {
@@ -262,9 +311,9 @@ function exitWith(result: ReturnType<typeof spawnSync>): never {
262311}
263312
264313export function runLocalCli ( args : string [ ] ) {
265- ensureLocalCliReady ( { needsTestCli : args [ 0 ] === 'test' } ) ;
314+ const { vpPath } = ensureLocalCliReady ( { needsTestCli : args [ 0 ] === 'test' } ) ;
266315
267- const result = spawnSync ( localVpPath , args , {
316+ const result = spawnSync ( vpPath , args , {
268317 cwd : process . cwd ( ) ,
269318 env : localCliEnv ( ) ,
270319 stdio : 'inherit' ,
@@ -273,7 +322,7 @@ export function runLocalCli(args: string[]) {
273322}
274323
275324export function runLocalGlobalSnapTest ( args : string [ ] ) {
276- ensureLocalCliReady ( ) ;
325+ const { vpBinDir } = ensureLocalCliReady ( ) ;
277326
278327 const result = spawnSync (
279328 process . execPath ,
@@ -283,7 +332,7 @@ export function runLocalGlobalSnapTest(args: string[]) {
283332 '--dir' ,
284333 'snap-tests-global' ,
285334 '--local-vp-bin-dir' ,
286- localVpBinDir ,
335+ vpBinDir ,
287336 ...args ,
288337 ] ,
289338 {
0 commit comments