@@ -35,8 +35,9 @@ export async function sendToDaemon(req: Omit<DaemonRequest, 'token'>): Promise<D
3535async function ensureDaemon ( ) : Promise < DaemonInfo > {
3636 const existing = readDaemonInfo ( ) ;
3737 const localVersion = readVersion ( ) ;
38- if ( existing && existing . version === localVersion && ( await canConnect ( existing ) ) ) return existing ;
39- if ( existing && ( existing . version !== localVersion || ! ( await canConnect ( existing ) ) ) ) {
38+ const existingReachable = existing ? await canConnect ( existing ) : false ;
39+ if ( existing && existing . version === localVersion && existingReachable ) return existing ;
40+ if ( existing && ( existing . version !== localVersion || ! existingReachable ) ) {
4041 removeDaemonInfo ( ) ;
4142 }
4243
@@ -67,7 +68,11 @@ function readDaemonInfo(): DaemonInfo | null {
6768}
6869
6970function removeDaemonInfo ( ) : void {
70- if ( fs . existsSync ( infoPath ) ) fs . unlinkSync ( infoPath ) ;
71+ try {
72+ if ( fs . existsSync ( infoPath ) ) fs . unlinkSync ( infoPath ) ;
73+ } catch {
74+ // Best-effort cleanup only; daemon can still overwrite this file on startup.
75+ }
7176}
7277
7378async function canConnect ( info : DaemonInfo ) : Promise < boolean > {
@@ -87,11 +92,14 @@ async function startDaemon(): Promise<void> {
8792 const distPath = path . join ( root , 'dist' , 'src' , 'daemon.js' ) ;
8893 const srcPath = path . join ( root , 'src' , 'daemon.ts' ) ;
8994
90- const useDist = fs . existsSync ( distPath ) ;
91- if ( ! useDist && ! fs . existsSync ( srcPath ) ) {
95+ const hasDist = fs . existsSync ( distPath ) ;
96+ const hasSrc = fs . existsSync ( srcPath ) ;
97+ if ( ! hasDist && ! hasSrc ) {
9298 throw new AppError ( 'COMMAND_FAILED' , 'Daemon entry not found' , { distPath, srcPath } ) ;
9399 }
94- const args = useDist ? [ distPath ] : [ '--experimental-strip-types' , srcPath ] ;
100+ const runningFromSource = process . execArgv . includes ( '--experimental-strip-types' ) ;
101+ const useSrc = runningFromSource ? hasSrc : ! hasDist && hasSrc ;
102+ const args = useSrc ? [ '--experimental-strip-types' , srcPath ] : [ distPath ] ;
95103
96104 runCmdDetached ( process . execPath , args ) ;
97105}
0 commit comments