11import path from "path"
2- import { Context , Effect , Layer , Stream } from "effect"
2+ import { Context , Effect , Layer , Option , Ref , Semaphore , Stream } from "effect"
33import { FetchHttpClient , HttpClient , HttpClientRequest } from "effect/unstable/http"
44import { ChildProcess } from "effect/unstable/process"
55import { ChildProcessSpawner } from "effect/unstable/process/ChildProcessSpawner"
@@ -28,15 +28,41 @@ export namespace RipgrepBinary {
2828
2929 export class Service extends Context . Service < Service , Interface > ( ) ( "@opencode/RipgrepBinary" ) { }
3030
31+ // Unlike Effect.cached, only successful results are memoized: a failed resolution
32+ // must be retried on the next call in the same process.
33+ export const memoizeSuccess = < A , E , R > ( effect : Effect . Effect < A , E , R > ) =>
34+ Effect . gen ( function * ( ) {
35+ const resolved = yield * Ref . make ( Option . none < A > ( ) )
36+ const singleFlight = Semaphore . makeUnsafe ( 1 ) . withPermit
37+ return singleFlight (
38+ Effect . gen ( function * ( ) {
39+ const hit = yield * Ref . get ( resolved )
40+ if ( Option . isSome ( hit ) ) return hit . value
41+ const value = yield * effect
42+ yield * Ref . set ( resolved , Option . some ( value ) )
43+ return value
44+ } ) ,
45+ )
46+ } )
47+
48+ // Windows PowerShell 5.1 inherits PSModulePath from the launching process. When opencode
49+ // is started from the MSIX (Microsoft Store) build of PowerShell 7, that variable points at
50+ // the Core-edition modules first, so Expand-Archive autoload fails inside the 5.1 child.
51+ // Pinning the child to the inbox 5.1 module directory avoids the polluted lookup.
52+ export const windowsPowerShell5ModulePath = ( systemRoot : string | undefined = process . env . SystemRoot ) =>
53+ path . join ( systemRoot ?? "C:\\Windows" , "System32" , "WindowsPowerShell" , "v1.0" , "Modules" )
54+
3155 const layer = Layer . effect (
3256 Service ,
3357 Effect . gen ( function * ( ) {
3458 const fs = yield * FSUtil . Service
3559 const http = HttpClient . filterStatusOk ( yield * HttpClient . HttpClient )
3660 const spawner = yield * ChildProcessSpawner
3761
38- const run = Effect . fnUntraced ( function * ( command : string , args : string [ ] ) {
39- const handle = yield * spawner . spawn ( ChildProcess . make ( command , args , { extendEnv : true , stdin : "ignore" } ) )
62+ const run = Effect . fnUntraced ( function * ( command : string , args : string [ ] , env ?: Record < string , string > ) {
63+ const handle = yield * spawner . spawn (
64+ ChildProcess . make ( command , args , { extendEnv : true , stdin : "ignore" , env } ) ,
65+ )
4066 const [ stdout , stderr , code ] = yield * Effect . all (
4167 [
4268 Stream . mkString ( Stream . decodeText ( handle . stdout ) ) ,
@@ -57,12 +83,20 @@ export namespace RipgrepBinary {
5783
5884 if ( config . extension === "zip" ) {
5985 const shell = ( yield * Effect . sync ( ( ) => which ( "powershell.exe" ) ?? which ( "pwsh.exe" ) ) ) ?? "powershell.exe"
60- const result = yield * run ( shell , [
61- "-NoProfile" ,
62- "-NonInteractive" ,
63- "-Command" ,
64- `$global:ProgressPreference = 'SilentlyContinue'; Expand-Archive -LiteralPath '${ archive . replaceAll ( "'" , "''" ) } ' -DestinationPath '${ dir . replaceAll ( "'" , "''" ) } ' -Force` ,
65- ] )
86+ const env =
87+ path . basename ( shell ) . toLowerCase ( ) === "powershell.exe"
88+ ? { PSModulePath : windowsPowerShell5ModulePath ( ) }
89+ : undefined
90+ const result = yield * run (
91+ shell ,
92+ [
93+ "-NoProfile" ,
94+ "-NonInteractive" ,
95+ "-Command" ,
96+ `$global:ProgressPreference = 'SilentlyContinue'; Expand-Archive -LiteralPath '${ archive . replaceAll ( "'" , "''" ) } ' -DestinationPath '${ dir . replaceAll ( "'" , "''" ) } ' -Force` ,
97+ ] ,
98+ env ,
99+ )
66100 if ( result . code !== 0 )
67101 throw new Error (
68102 result . stderr . trim ( ) || result . stdout . trim ( ) || `ripgrep extraction failed with code ${ result . code } ` ,
@@ -88,39 +122,39 @@ export namespace RipgrepBinary {
88122 if ( process . platform !== "win32" ) yield * fs . chmod ( target , 0o755 )
89123 } , Effect . scoped )
90124
91- return Service . of ( {
92- filepath : yield * Effect . cached (
93- Effect . gen ( function * ( ) {
94- const system = yield * Effect . sync ( ( ) => which ( process . platform === "win32" ? "rg.exe" : "rg" ) )
95- if ( system && ( yield * fs . isFile ( system ) . pipe ( Effect . orDie ) ) ) return system
96-
97- const target = path . join ( Global . Path . bin , `rg${ process . platform === "win32" ? ".exe" : "" } ` )
98- if ( yield * fs . isFile ( target ) . pipe ( Effect . orDie ) ) return target
99-
100- const platformKey = `${ process . arch } -${ process . platform } ` as keyof typeof PLATFORM
101- const config = PLATFORM [ platformKey ]
102- if ( ! config ) throw new Error ( `unsupported platform for ripgrep: ${ platformKey } ` )
103-
104- const filename = `ripgrep-${ VERSION } -${ config . platform } .${ config . extension } `
105- const url = `https://github.com/BurntSushi/ripgrep/releases/download/${ VERSION } /${ filename } `
106- const archive = path . join ( Global . Path . bin , filename )
107-
108- yield * Effect . logInfo ( "downloading ripgrep" , { url } )
109- yield * fs . ensureDir ( Global . Path . bin ) . pipe ( Effect . orDie )
110- const bytes = yield * HttpClientRequest . get ( url ) . pipe (
111- http . execute ,
112- Effect . flatMap ( ( response ) => response . arrayBuffer ) ,
113- Effect . mapError ( ( cause ) => ( cause instanceof Error ? cause : new Error ( String ( cause ) ) ) ) ,
114- )
115- if ( bytes . byteLength === 0 ) throw new Error ( `failed to download ripgrep from ${ url } ` )
116-
117- yield * fs . writeWithDirs ( archive , new Uint8Array ( bytes ) )
118- yield * extract ( archive , config , target )
119- yield * fs . remove ( archive , { force : true } ) . pipe ( Effect . ignore )
120- return target
121- } ) ,
122- ) ,
125+ const resolve = Effect . gen ( function * ( ) {
126+ const system = yield * Effect . sync ( ( ) => which ( process . platform === "win32" ? "rg.exe" : "rg" ) )
127+ if ( system && ( yield * fs . isFile ( system ) . pipe ( Effect . orDie ) ) ) return system
128+
129+ const target = path . join ( Global . Path . bin , `rg${ process . platform === "win32" ? ".exe" : "" } ` )
130+ if ( yield * fs . isFile ( target ) . pipe ( Effect . orDie ) ) return target
131+
132+ const platformKey = `${ process . arch } -${ process . platform } ` as keyof typeof PLATFORM
133+ const config = PLATFORM [ platformKey ]
134+ if ( ! config ) throw new Error ( `unsupported platform for ripgrep: ${ platformKey } ` )
135+
136+ const filename = `ripgrep-${ VERSION } -${ config . platform } .${ config . extension } `
137+ const url = `https://github.com/BurntSushi/ripgrep/releases/download/${ VERSION } /${ filename } `
138+ const archive = path . join ( Global . Path . bin , filename )
139+
140+ yield * Effect . logInfo ( "downloading ripgrep" , { url } )
141+ yield * fs . ensureDir ( Global . Path . bin ) . pipe ( Effect . orDie )
142+ const bytes = yield * HttpClientRequest . get ( url ) . pipe (
143+ http . execute ,
144+ Effect . flatMap ( ( response ) => response . arrayBuffer ) ,
145+ Effect . mapError ( ( cause ) => ( cause instanceof Error ? cause : new Error ( String ( cause ) ) ) ) ,
146+ )
147+ if ( bytes . byteLength === 0 ) throw new Error ( `failed to download ripgrep from ${ url } ` )
148+
149+ yield * fs . writeWithDirs ( archive , new Uint8Array ( bytes ) )
150+ yield * extract ( archive , config , target )
151+ yield * fs . remove ( archive , { force : true } ) . pipe ( Effect . ignore )
152+ return target
123153 } )
154+
155+ const filepath = yield * memoizeSuccess ( resolve )
156+
157+ return Service . of ( { filepath } )
124158 } ) ,
125159 )
126160
0 commit comments