Skip to content

Commit 47e5e71

Browse files
authored
fix(cli): resolve symlinked home path when detecting curl installs (#54609)
1 parent 1c63fe9 commit 47e5e71

2 files changed

Lines changed: 50 additions & 18 deletions

File tree

‎packages/cli/src/services/updater.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,13 @@ const make = Effect.gen(function* () {
173173
)
174174
})
175175

176-
const curlBinary = path.resolve(
176+
const curlPath = path.resolve(
177177
global.home,
178178
".opencode",
179179
"bin",
180180
process.platform === "win32" ? "opencode.exe" : "opencode",
181181
)
182+
const curlBinary = yield* fs.realPath(curlPath).pipe(Effect.orElseSucceed(() => curlPath))
182183

183184
// On Windows a bare `bash` may be the WSL launcher, which cannot run the installer against Windows paths.
184185
const installerBash = Effect.fnUntraced(function* () {
@@ -197,7 +198,7 @@ const make = Effect.gen(function* () {
197198
})
198199

199200
const method = Effect.fnUntraced(function* () {
200-
if (path.resolve(process.execPath) === curlBinary) return "curl"
201+
if (executable === curlBinary) return "curl"
201202
if (installedFormula) return "brew"
202203
if (!installedPackage) return
203204

@@ -323,7 +324,7 @@ const make = Effect.gen(function* () {
323324
const retaining = <A, E, R>(method: Method, effect: Effect.Effect<A, E, R>, directory = global.cache) => {
324325
if (process.platform !== "win32" || method === "brew") return effect
325326
// Only the installed binary is at stake; source checkouts run inside bun or node.
326-
const owned = method === "curl" ? path.resolve(process.execPath) === curlBinary : installedPackage !== undefined
327+
const owned = method === "curl" ? executable === curlBinary : installedPackage !== undefined
327328
if (!owned) return effect
328329
return Effect.scoped(RetainedImage.retain(directory, "upgrade").pipe(Effect.andThen(effect))).pipe(
329330
Effect.provideService(FileSystem.FileSystem, fs),

‎packages/cli/test/updater-install.test.ts‎

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,22 @@ function fixture(
2323
failCleanup = false,
2424
releasePackage = name,
2525
formula?: string,
26+
executableOverride?: (root: string, home: string) => Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem>,
2627
) {
2728
return Effect.gen(function* () {
2829
const fs = yield* FileSystem.FileSystem
2930
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner
3031
const root = yield* fs.makeTempDirectoryScoped({ prefix: "opencode-updater-" })
3132
const execPath = process.execPath
3233
const modules = path.join(root, "node_modules")
33-
const executable = formula
34-
? path.join(root, "Cellar", formula, "2.0.20", "bin", "opencode")
35-
: path.join(modules, "@opencode", "cli", "bin", "opencode")
34+
const executable = executableOverride
35+
? yield* executableOverride(root, path.join(root, "home"))
36+
: formula
37+
? path.join(root, "Cellar", formula, "2.0.20", "bin", "opencode")
38+
: path.join(modules, "@opencode", "cli", "bin", "opencode")
3639
yield* fs.makeDirectory(path.dirname(executable), { recursive: true })
3740
yield* fs.writeFileString(executable, "binary")
38-
if (!formula)
41+
if (!formula && !executableOverride)
3942
yield* fs.writeFileString(
4043
path.join(modules, "@opencode", "cli", "package.json"),
4144
JSON.stringify({ name, bin: { opencode: "bin/opencode" } }),
@@ -98,7 +101,7 @@ function fixture(
98101
}),
99102
)
100103
: fs.remove(target, options),
101-
realPath: (input) => (input === execPath ? Effect.succeed(executable) : fs.realPath(input)),
104+
realPath: (input) => (input === execPath ? fs.realPath(executable) : fs.realPath(input)),
102105
}),
103106
Effect.provideService(
104107
AppProcess.Service,
@@ -403,17 +406,22 @@ windows("windows keeps the uninstall link in the temporary directory, not the re
403406
windows("windows links the curl binary before the installer replaces it", () =>
404407
Effect.gen(function* () {
405408
const layout = { executable: "", cache: "" }
406-
const test = yield* fixture((command) => {
407-
if (path.basename(command.command, ".exe") === "bash") {
408-
expect(readFileSync(layout.executable, "utf8")).toBe("binary")
409-
expect(upgradeLinks(layout.cache)).toHaveLength(1)
410-
}
411-
return {}
412-
})
413-
layout.executable = path.join(test.global.home, ".opencode", "bin", "opencode.exe")
409+
const test = yield* fixture(
410+
(command) => {
411+
if (path.basename(command.command, ".exe") === "bash") {
412+
expect(readFileSync(layout.executable, "utf8")).toBe("binary")
413+
expect(upgradeLinks(layout.cache)).toHaveLength(1)
414+
}
415+
return {}
416+
},
417+
"@opencode/cli",
418+
false,
419+
"@opencode/cli",
420+
undefined,
421+
(_root, home) => Effect.succeed(path.join(home, ".opencode", "bin", "opencode.exe")),
422+
)
423+
layout.executable = test.executable
414424
layout.cache = test.global.cache
415-
yield* test.fs.makeDirectory(path.dirname(layout.executable), { recursive: true })
416-
yield* test.fs.writeFileString(layout.executable, "binary")
417425
const original = process.execPath
418426
process.execPath = layout.executable
419427
yield* Effect.addFinalizer(() => Effect.sync(() => (process.execPath = original)))
@@ -425,6 +433,29 @@ windows("windows links the curl binary before the installer replaces it", () =>
425433
}),
426434
)
427435

436+
unix("curl method detection resolves symlinked home directories", () =>
437+
Effect.gen(function* () {
438+
const test = yield* fixture(
439+
() => ({}),
440+
"@opencode/cli",
441+
false,
442+
"@opencode/cli",
443+
undefined,
444+
(root, home) =>
445+
Effect.gen(function* () {
446+
const fs = yield* FileSystem.FileSystem
447+
const realHome = path.join(root, "real-home")
448+
const bin = path.join(realHome, ".opencode", "bin", "opencode")
449+
yield* fs.makeDirectory(path.dirname(bin), { recursive: true })
450+
yield* fs.symlink(realHome, home)
451+
return bin
452+
}),
453+
)
454+
expect(yield* test.updater.method()).toBe("curl")
455+
expect(test.commands).toEqual([])
456+
}),
457+
)
458+
428459
windows("windows leaves a source checkout's runtime alone", () =>
429460
Effect.gen(function* () {
430461
const layout = { cache: "" }

0 commit comments

Comments
 (0)