@@ -490,22 +490,14 @@ async function checkForUpdates(runningProcess, exitListener) {
490490
491491 await downloadBinary ( latestVersion )
492492
493- const newChild = spawn ( CONFIG . binaryPath , process . argv . slice ( 2 ) , {
494- stdio : 'inherit' ,
495- detached : false ,
496- } )
493+ const newChild = spawnInstalledBinary ( { detached : false } )
497494
498495 newChild . on ( 'exit' , ( code , signal ) => {
499496 resetTerminal ( )
500497 printCrashDiagnostics ( code , signal )
501498 process . exit ( signal ? 1 : ( code || 0 ) )
502499 } )
503500
504- newChild . on ( 'error' , ( err ) => {
505- console . error ( 'Failed to start codebuff:' , err . message )
506- process . exit ( 1 )
507- } )
508-
509501 return new Promise ( ( ) => { } )
510502 }
511503 } catch ( error ) {
@@ -561,13 +553,77 @@ function printCrashDiagnostics(code, signal) {
561553 console . error ( '' )
562554}
563555
564- async function main ( ) {
565- await ensureBinaryExists ( )
556+ function getInstalledBinaryStatus ( ) {
557+ try {
558+ const stats = fs . statSync ( CONFIG . binaryPath )
559+ return stats . isFile ( ) ? `yes (${ formatBytes ( stats . size ) } )` : 'no'
560+ } catch {
561+ return 'no'
562+ }
563+ }
564+
565+ function printSpawnFailure ( err ) {
566+ resetTerminal ( )
567+ const code = err && err . code ? ` (${ err . code } )` : ''
568+
569+ console . error ( `Failed to start ${ packageName } : ${ err . message } ${ code } ` )
570+ console . error ( '' )
571+ console . error ( 'System info:' )
572+ console . error ( ` Platform: ${ process . platform } ${ process . arch } ` )
573+ console . error ( ` Node: ${ process . version } ` )
574+ console . error ( ` Binary: ${ CONFIG . binaryPath } ` )
575+ console . error ( ` Exists: ${ getInstalledBinaryStatus ( ) } ` )
576+
577+ if ( process . platform === 'win32' ) {
578+ console . error ( '' )
579+ console . error (
580+ 'On Windows, this can happen when Windows Security or antivirus blocks' ,
581+ )
582+ console . error (
583+ 'or quarantines the downloaded executable, or when the binary requires' ,
584+ )
585+ console . error ( 'CPU instructions that are not available on this machine.' )
586+ }
587+
588+ console . error ( '' )
589+ console . error ( 'Try deleting the downloaded files and running again:' )
590+ console . error ( ` ${ CONFIG . configDir } ` )
591+ console . error ( '' )
592+ }
593+
594+ function spawnInstalledBinary ( options = { } ) {
595+ if ( ! fs . existsSync ( CONFIG . binaryPath ) ) {
596+ try {
597+ if ( fs . existsSync ( CONFIG . metadataPath ) ) fs . unlinkSync ( CONFIG . metadataPath )
598+ } catch {
599+ // best effort
600+ }
601+ const error = new Error (
602+ `downloaded binary is missing at ${ CONFIG . binaryPath } ` ,
603+ )
604+ error . code = 'BINARY_MISSING'
605+ printSpawnFailure ( error )
606+ process . exit ( 1 )
607+ }
566608
567609 const child = spawn ( CONFIG . binaryPath , process . argv . slice ( 2 ) , {
568610 stdio : 'inherit' ,
611+ ...options ,
612+ } )
613+
614+ child . on ( 'error' , ( err ) => {
615+ printSpawnFailure ( err )
616+ process . exit ( 1 )
569617 } )
570618
619+ return child
620+ }
621+
622+ async function main ( ) {
623+ await ensureBinaryExists ( )
624+
625+ const child = spawnInstalledBinary ( )
626+
571627 const exitListener = ( code , signal ) => {
572628 resetTerminal ( )
573629 printCrashDiagnostics ( code , signal )
@@ -576,11 +632,6 @@ async function main() {
576632
577633 child . on ( 'exit' , exitListener )
578634
579- child . on ( 'error' , ( err ) => {
580- console . error ( 'Failed to start codebuff:' , err . message )
581- process . exit ( 1 )
582- } )
583-
584635 setTimeout ( ( ) => {
585636 checkForUpdates ( child , exitListener )
586637 } , 100 )
0 commit comments