@@ -206,6 +206,15 @@ async function checkGitBranchExists(branchName: string): Promise<boolean> {
206206 }
207207}
208208
209+ async function getCurrentBranch ( ) : Promise < string > {
210+ const proc = Bun . spawn ( [ 'git' , 'rev-parse' , '--abbrev-ref' , 'HEAD' ] , {
211+ stdout : 'pipe' ,
212+ stderr : 'pipe' ,
213+ } )
214+ const output = await new Response ( proc . stdout ) . text ( )
215+ return output . trim ( ) || 'main'
216+ }
217+
209218function createEnvDevelopmentLocalFile (
210219 worktreePath : string ,
211220 args : WorktreeArgs ,
@@ -334,11 +343,32 @@ async function runDirenvAllow(worktreePath: string): Promise<void> {
334343 const envrcPath = join ( worktreePath , '.envrc' )
335344 if ( existsSync ( envrcPath ) ) {
336345 console . log ( 'Running direnv allow...' )
337- try {
338- await runCommand ( 'direnv' , [ 'allow' ] , worktreePath )
339- } catch ( error ) {
340- console . warn ( 'Failed to run direnv allow:' , error )
341- }
346+ return new Promise ( ( resolve ) => {
347+ // Use bash -c with explicit cd to ensure direnv sees the correct directory context
348+ // Just using cwd option doesn't work reliably with direnv
349+ const proc = spawn ( 'bash' , [ '-c' , `cd '${ worktreePath } ' && direnv allow` ] , {
350+ stdio : 'inherit' ,
351+ shell : false ,
352+ } )
353+
354+ proc . on ( 'close' , ( code ) => {
355+ if ( code === 0 ) {
356+ console . log ( 'direnv allow completed successfully' )
357+ } else {
358+ console . warn ( `direnv allow exited with code ${ code } ` )
359+ }
360+ resolve ( )
361+ } )
362+
363+ proc . on ( 'error' , ( error ) => {
364+ if ( ( error as NodeJS . ErrnoException ) . code === 'ENOENT' ) {
365+ console . warn ( 'bash not found, skipping direnv allow' )
366+ } else {
367+ console . warn ( 'Failed to run direnv allow:' , error . message )
368+ }
369+ resolve ( )
370+ } )
371+ } )
342372 } else {
343373 console . log ( 'No .envrc found, skipping direnv allow' )
344374 }
@@ -387,14 +417,33 @@ async function main(): Promise<void> {
387417 console . log ( `Location: ${ worktreePath } ` )
388418
389419 // Create the git worktree (with or without creating new branch)
420+ // Explicitly use HEAD to ensure worktree has latest tooling (.bin/bun, etc.)
421+ const baseBranch = await getCurrentBranch ( )
390422 const worktreeAddArgs = [ 'worktree' , 'add' , worktreePath ]
391423 if ( branchExists ) {
424+ // Branch exists - check it out
392425 worktreeAddArgs . push ( args . name )
393426 } else {
394- worktreeAddArgs . push ( '-b' , args . name )
427+ // New branch - explicitly create from HEAD to get latest tooling
428+ worktreeAddArgs . push ( '-b' , args . name , 'HEAD' )
395429 }
396430 await runCommand ( 'git' , worktreeAddArgs )
397431
432+ // If branch already existed, merge in the base branch to get latest tooling
433+ if ( branchExists ) {
434+ console . log ( `Merging ${ baseBranch } into ${ args . name } to get latest tooling...` )
435+ const mergeResult = await runCommand (
436+ 'git' ,
437+ [ 'merge' , baseBranch , '--no-edit' , '-m' , `Merge ${ baseBranch } to get latest tooling` ] ,
438+ worktreePath ,
439+ )
440+ if ( mergeResult . exitCode !== 0 ) {
441+ console . warn (
442+ `Warning: Merge had conflicts. Please resolve them manually in the worktree.` ,
443+ )
444+ }
445+ }
446+
398447 console . log ( 'Setting up worktree environment...' )
399448 console . log ( `Backend port: ${ args . backendPort } ` )
400449 console . log ( `Web port: ${ args . webPort } ` )
@@ -416,6 +465,7 @@ async function main(): Promise<void> {
416465
417466 console . log ( `✅ Worktree '${ args . name } ' created and set up successfully!` )
418467 console . log ( `📁 Location: ${ worktreePath } ` )
468+ console . log ( `🌿 Based on: ${ baseBranch } (HEAD)` )
419469 console . log ( `🚀 You can now cd into the worktree and start working:` )
420470 console . log ( ` cd ${ worktreePath } ` )
421471 console . log ( `` )
0 commit comments