import { confirm, intro, log, outro, spinner } from "@clack/prompts" import { Service } from "@opencode/client/effect/service" import { Global } from "@opencode/util/global" import { Effect, FileSystem, Schedule } from "effect" import { randomBytes } from "node:crypto" import path from "node:path" import { Commands } from "../commands" import { Runtime } from "../../framework/runtime" import { ServerConnection } from "../../services/server-connection" import { RetainedImage } from "../../services/retained-image" import { Updater } from "../../services/updater" import { handlePromptErrors, prompt, requireInteractive } from "../../ui/prompt" import { errorMessage } from "../../util/error" export default Runtime.handler( Commands.commands.uninstall, Effect.fn("cli.uninstall")(function* (input) { intro("Uninstall OpenCode") const fs = yield* FileSystem.FileSystem const global = yield* Global.Service const updater = yield* Updater.Service const method = yield* updater.method() const removal = method ? updater.removal(method) : undefined // Data, config, and state hold sessions, credentials, settings, and prompt history; uninstall only removes the cache. // All channels share the cache. Stop background services before deleting it. // Read registrations directly: ServiceConfig.options() can migrate files even during a dry run. const services = (yield* fs.exists(global.state)) ? (yield* fs.readDirectory(global.state)).filter((name) => /^service(?:-.*)?\.json$/.test(name)) : [] const shell = method === "curl" ? yield* shellConfigs(global.home) : [] log.info(`Installation method: ${method ?? "unknown"}`) log.message("The following global files will be removed (shared by OpenCode versions and channels):") if (yield* fs.exists(global.cache)) log.info(` Cache: ${global.cache}`) services.forEach((name) => log.info(` Stop background service and persistent terminals: ${path.join(global.state, name)}`), ) shell.forEach((file) => log.info(` Shell PATH: ${file}`)) if (removal) log.info(` Package: ${removal.command.join(" ")}`) if (method === "curl") log.info(` Binary (manual removal): ${process.execPath}`) if (!method) log.warn("Could not detect the installation method. Remove the installation manually after cleanup.") if (input.dryRun) { log.warn("Dry run - no changes made") outro("Done") return } if (!input.force) { yield* requireInteractive("Use --force to uninstall without an interactive terminal, or --dry-run to preview.") const accepted = yield* prompt(() => confirm({ message: "Are you sure you want to uninstall?", initialValue: false }), ) if (!accepted) { outro("Cancelled") return } } const progress = spinner() if (services.length) { progress.start("Stopping background services...") yield* Effect.forEach(services, (name) => Effect.gen(function* () { const options = { file: path.join(global.state, name) } yield* ServerConnection.shutdownPersistentPty(options).pipe(Effect.ignore) yield* Service.stop(options) }), ).pipe( Effect.tap(() => Effect.sync(() => progress.stop("Background services stopped"))), Effect.tapCause(() => Effect.sync(() => progress.stop("Failed to stop background services", 1))), ) } // Links that keep an older OpenCode replaceable may still run; move them so the cache can go. if (process.platform === "win32") yield* RetainedImage.relocate(global.cache, global.tmp) const errors: string[] = [] progress.start("Removing Cache...") yield* fs.remove(global.cache, { recursive: true, force: true }).pipe( // Windows reports a terminated service as gone before it releases its file handles, // so the first removal can race that teardown. Effect.retry({ while: (error) => process.platform === "win32" && error.reason._tag === "Busy", schedule: Schedule.max([Schedule.spaced("250 millis"), Schedule.recurs(40)]), }), Effect.tap(() => Effect.sync(() => progress.stop("Removed Cache"))), Effect.catch((error) => Effect.sync(() => { progress.stop("Failed to remove Cache", 1) errors.push(`Cache: ${errorMessage(error)}`) }), ), ) yield* Effect.forEach(shell, (file) => fs.readFileString(file).pipe( Effect.flatMap((content) => writeShellConfig(file, cleanShellConfig(content))), Effect.catch((error) => Effect.sync(() => errors.push(`Shell config ${file}: ${errorMessage(error)}`))), ), ) if (removal) { progress.start(`Running ${removal.command.join(" ")}...`) yield* removal.run.pipe( Effect.tap(() => Effect.sync(() => progress.stop("Package removed"))), Effect.catch((error) => Effect.sync(() => { progress.stop("Package manager uninstall failed", 1) errors.push(errorMessage(error)) log.warn(`Run manually: ${removal.command.join(" ")}`) }), ), ) } if (method === "curl") { log.message("To finish removing the binary, run:") log.info(` rm '${process.execPath.replaceAll("'", "'\\''")}'`) } if (errors.length) yield* Effect.fail(new Error(errors.join("\n"))) outro("Done") }, handlePromptErrors), ) const shellConfigs = Effect.fnUntraced(function* (home: string) { const fs = yield* FileSystem.FileSystem const bin = path.dirname(process.execPath) // V1 and V2 curl installs share a PATH entry; retain it while another binary uses it. if ((yield* fs.readDirectory(bin)).some((name) => name !== path.basename(process.execPath))) return [] const xdg = process.env.XDG_CONFIG_HOME || path.join(home, ".config") const zsh = process.env.ZDOTDIR || home const candidates: Record = { fish: [path.join(home, ".config/fish/config.fish"), path.join(xdg, "fish/config.fish")], zsh: [ path.join(zsh, ".zshrc"), path.join(zsh, ".zshenv"), path.join(xdg, "zsh/.zshrc"), path.join(xdg, "zsh/.zshenv"), ], bash: [ path.join(home, ".bashrc"), path.join(home, ".bash_profile"), path.join(home, ".profile"), path.join(xdg, "bash/.bashrc"), path.join(xdg, "bash/.bash_profile"), ], ash: [path.join(home, ".ashrc"), path.join(home, ".profile")], sh: [path.join(home, ".ashrc"), path.join(home, ".profile")], } const files = [...new Set(candidates[path.basename(process.env.SHELL || "bash")] ?? candidates.bash)] return yield* Effect.filter(files, (file) => fs.readFileString(file).pipe( Effect.map((content) => cleanShellConfig(content) !== content), Effect.orElseSucceed(() => false), ), ) }) export const writeShellConfig = Effect.fnUntraced(function* (file: string, content: string) { const fs = yield* FileSystem.FileSystem // Shell rc files are commonly symlinks into a dotfiles repo: write a temp file // beside the resolved target and rename over the target, so the link itself // survives and the write is never partial. Keep the target's mode - the other // CLI writers hardcode 0o600, which would tighten a hand-maintained rc file. const target = yield* fs.realPath(file).pipe(Effect.orElseSucceed(() => file)) const mode = yield* fs.stat(target).pipe( Effect.map((info) => info.mode), Effect.orElseSucceed(() => undefined), ) // The temp file lives in the user's own directory, where a fixed name like // `.bashrc.tmp` is not reserved: create a uniquely named file exclusively // (`wx` fails if the name is somehow taken) beside the target, and clean up // only the file this invocation created. const temp = `${target}.tmp-${randomBytes(6).toString("hex")}` const write = Effect.gen(function* () { yield* Effect.scoped( Effect.gen(function* () { const handle = yield* fs.open(temp, { flag: "wx" }) yield* handle.writeAll(new TextEncoder().encode(content)) }), ) if (mode !== undefined) yield* fs.chmod(temp, mode) yield* fs.rename(temp, target) }) yield* write.pipe(Effect.onError(() => fs.remove(temp, { force: true }).pipe(Effect.ignore))) }) export function cleanShellConfig(content: string) { const lines = content.split("\n") const entry = (line: string) => /^(?:export PATH=|fish_add_path\s)/.test(line.trim()) && line.includes(".opencode/bin") return lines .filter((line, index) => !entry(line) && !(line.trim() === "# opencode" && entry(lines[index + 1] ?? ""))) .join("\n") }