Skip to content

Commit 994d6b6

Browse files
committed
fix(core): retry failed ripgrep resolution and sanitize PowerShell 5.1 module path on Windows
Two Windows grep/glob failures from #40623: - the ripgrep resolution was wrapped in Effect.cached, which memoizes failures too, so one failed download/extraction poisoned every grep and glob call until the process was restarted. Replace it with a single-flight memo that only caches successful resolutions, so the next call retries. - zip extraction spawns powershell.exe, which inherits PSModulePath from the launching shell. When opencode is launched from the MSIX build of PowerShell 7, that path lists the Core-edition modules first and Expand-Archive autoload fails inside the 5.1 child. Pin the child's PSModulePath to the inbox 5.1 module directory.
1 parent 284214c commit 994d6b6

2 files changed

Lines changed: 136 additions & 41 deletions

File tree

‎packages/core/src/ripgrep/binary.ts‎

Lines changed: 75 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from "path"
2-
import { Context, Effect, Layer, Stream } from "effect"
2+
import { Context, Effect, Layer, Option, Ref, Semaphore, Stream } from "effect"
33
import { FetchHttpClient, HttpClient, HttpClientRequest } from "effect/unstable/http"
44
import { ChildProcess } from "effect/unstable/process"
55
import { 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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { describe, expect, test } from "bun:test"
2+
import path from "path"
3+
import { Effect, Exit } from "effect"
4+
import { RipgrepBinary } from "@opencode-ai/core/ripgrep/binary"
5+
6+
describe("RipgrepBinary.memoizeSuccess", () => {
7+
test("memoizes a successful resolution", async () => {
8+
let attempts = 0
9+
const program = Effect.gen(function* () {
10+
const memoized = yield* RipgrepBinary.memoizeSuccess(
11+
Effect.sync(() => {
12+
attempts++
13+
return "rg"
14+
}),
15+
)
16+
const first = yield* memoized
17+
const second = yield* memoized
18+
return [first, second]
19+
})
20+
21+
expect(await Effect.runPromise(program)).toEqual(["rg", "rg"])
22+
expect(attempts).toBe(1)
23+
})
24+
25+
test("retries a failed resolution instead of caching the failure", async () => {
26+
let attempts = 0
27+
const program = Effect.gen(function* () {
28+
const memoized = yield* RipgrepBinary.memoizeSuccess(
29+
Effect.sync(() => {
30+
attempts++
31+
if (attempts === 1) throw new Error("extraction failed")
32+
return "rg"
33+
}),
34+
)
35+
const first = yield* Effect.exit(memoized)
36+
const second = yield* memoized
37+
const third = yield* memoized
38+
return { first, second, third }
39+
})
40+
41+
const { first, second, third } = await Effect.runPromise(program)
42+
expect(Exit.isFailure(first)).toBe(true)
43+
expect(second).toBe("rg")
44+
expect(third).toBe("rg")
45+
expect(attempts).toBe(2)
46+
})
47+
})
48+
49+
describe("RipgrepBinary.windowsPowerShell5ModulePath", () => {
50+
test("points at the inbox Windows PowerShell 5.1 module directory", () => {
51+
expect(RipgrepBinary.windowsPowerShell5ModulePath("D:\\WinRoot")).toBe(
52+
path.join("D:\\WinRoot", "System32", "WindowsPowerShell", "v1.0", "Modules"),
53+
)
54+
})
55+
56+
test("falls back to C:\\Windows when SystemRoot is unset", () => {
57+
expect(RipgrepBinary.windowsPowerShell5ModulePath(undefined)).toBe(
58+
path.join("C:\\Windows", "System32", "WindowsPowerShell", "v1.0", "Modules"),
59+
)
60+
})
61+
})

0 commit comments

Comments
 (0)