11// @effect -diagnostics nodeBuiltinImport:off - Effect has no incremental digest.
22import { ProviderDriverKind , type ProviderInstallState } from "@t3tools/contracts" ;
3+ import { cliArchiveTarCommand } from "@t3tools/shared/cliRelease" ;
34import * as HostProcess from "@t3tools/shared/HostProcess" ;
45import { resolveCommandPath , resolveSpawnCommand } from "@t3tools/shared/shell" ;
56import * as Clock from "effect/Clock" ;
@@ -13,6 +14,7 @@ import * as FileSystem from "effect/FileSystem";
1314import * as Layer from "effect/Layer" ;
1415import * as Option from "effect/Option" ;
1516import * as Path from "effect/Path" ;
17+ import * as Schedule from "effect/Schedule" ;
1618import * as Schema from "effect/Schema" ;
1719import * as Scope from "effect/Scope" ;
1820import * as Semaphore from "effect/Semaphore" ;
@@ -511,8 +513,10 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
511513 message : "Extracting Codex." ,
512514 } ) satisfies ProviderInstallState ,
513515 ) ;
514- const entries = ( yield * runCommand ( "tar" , [ "-tzf" , archivePath ] ) ) . trim ( ) . split ( "\n" ) ;
515- const types = ( yield * runCommand ( "tar" , [ "-tvzf" , archivePath ] ) ) . trim ( ) . split ( "\n" ) ;
516+ // Bare `tar` on Windows can resolve to Git's MSYS tar, which reads `C:\...` as a remote host.
517+ const tarCommand = cliArchiveTarCommand ( platform , environment ) ;
518+ const entries = ( yield * runCommand ( tarCommand , [ "-tzf" , archivePath ] ) ) . trim ( ) . split ( / \r ? \n / u) ;
519+ const types = ( yield * runCommand ( tarCommand , [ "-tvzf" , archivePath ] ) ) . trim ( ) . split ( / \r ? \n / u) ;
516520 if (
517521 entries . length > 10000 ||
518522 entries . length !== types . length ||
@@ -528,7 +532,7 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
528532 } )
529533 )
530534 return yield * installationError ( "extract" , "The Codex archive contains unsafe entries." ) ;
531- yield * runCommand ( "tar" , [ "-xzf" , archivePath , "-C" , runtime ] ) ;
535+ yield * runCommand ( tarCommand , [ "-xzf" , archivePath , "-C" , runtime ] ) ;
532536 yield * SubscriptionRef . update (
533537 state ,
534538 ( current ) =>
@@ -552,7 +556,12 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
552556 } ) ,
553557 { flag : "wx" , mode : 0o600 } ,
554558 ) ;
555- yield * fs . rename ( runtime , destination ) ;
559+ // On Windows, Defender and kernel process rundown can briefly hold the
560+ // verified codex.exe handle open right after `--version` exits.
561+ const renameRuntime = fs . rename ( runtime , destination ) ;
562+ yield * platform === "win32"
563+ ? renameRuntime . pipe ( Effect . retry ( { times : 10 , schedule : Schedule . spaced ( "100 millis" ) } ) )
564+ : renameRuntime ;
556565 yield * activate ( ) ;
557566 } ,
558567 Effect . scoped ,
@@ -691,6 +700,26 @@ export const makeCodexInstallation = Effect.fn("makeCodexInstallation")(function
691700 ) ;
692701
693702 yield * Effect . gen ( function * ( ) {
703+ // Clear staging directories left behind by an interrupted install so they
704+ // neither leak disk nor make an otherwise empty runtime look removable.
705+ if ( yield * fs . exists ( managedDirectory ) ) {
706+ const entries = yield * fs
707+ . readDirectory ( managedDirectory )
708+ . pipe ( Effect . orElseSucceed ( ( ) : ReadonlyArray < string > => [ ] ) ) ;
709+ for ( const entry of entries ) {
710+ if ( entry . startsWith ( ".install-" ) || entry . startsWith ( ".active-" ) ) {
711+ yield * fs
712+ . remove ( path . join ( managedDirectory , entry ) , { recursive : true , force : true } )
713+ . pipe ( Effect . ignore ) ;
714+ }
715+ }
716+ const remaining = yield * fs
717+ . readDirectory ( managedDirectory )
718+ . pipe ( Effect . orElseSucceed ( ( ) : ReadonlyArray < string > => [ ] ) ) ;
719+ if ( remaining . length === 0 ) {
720+ yield * fs . remove ( managedDirectory , { recursive : true , force : true } ) . pipe ( Effect . ignore ) ;
721+ }
722+ }
694723 const canRemove = yield * fs . exists ( managedDirectory ) ;
695724 yield * SubscriptionRef . update ( state , ( current ) => ( { ...current , canRemove } ) ) ;
696725 if ( yield * reuseLocal ( ) ) return ;
0 commit comments