From e3419d47ee9e5899b696c22653de83c4684465f5 Mon Sep 17 00:00:00 2001 From: Dax Raad Date: Wed, 7 Oct 2026 16:45:58 -0400 Subject: [PATCH] fix(cli): keep state on uninstall --- packages/cli/src/commands/commands.ts | 2 +- .../cli/src/commands/handlers/uninstall.ts | 47 +++++++------------ services/www/src/docs/content/cli/index.mdx | 4 +- 3 files changed, 20 insertions(+), 33 deletions(-) diff --git a/packages/cli/src/commands/commands.ts b/packages/cli/src/commands/commands.ts index 11691bbb8b5..51855c2d327 100644 --- a/packages/cli/src/commands/commands.ts +++ b/packages/cli/src/commands/commands.ts @@ -73,7 +73,7 @@ const Root = Spec.make(typeof OPENCODE_CLI_NAME === "string" ? OPENCODE_CLI_NAME }, }), Spec.make("uninstall", { - description: "Uninstall OpenCode, keeping session data and configuration", + description: "Uninstall OpenCode, keeping session data, configuration, and state", params: { dryRun: Flag.boolean("dry-run").pipe( Flag.withDescription("Show what would be removed without removing"), diff --git a/packages/cli/src/commands/handlers/uninstall.ts b/packages/cli/src/commands/handlers/uninstall.ts index b435774c71a..7f6393c8387 100644 --- a/packages/cli/src/commands/handlers/uninstall.ts +++ b/packages/cli/src/commands/handlers/uninstall.ts @@ -20,12 +20,8 @@ export default Runtime.handler( const updater = yield* Updater.Service const method = yield* updater.method() const removal = method ? updater.removal(method) : undefined - // Data and config hold sessions, credentials, and user-authored settings; uninstall never removes them. - const directories = [ - { path: global.cache, label: "Cache" }, - { path: global.state, label: "State" }, - ] - // All channels share these directories. Stop their owners before deleting state or data. + // 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)) @@ -34,12 +30,7 @@ export default Runtime.handler( log.info(`Installation method: ${method ?? "unknown"}`) log.message("The following global files will be removed (shared by OpenCode versions and channels):") - yield* Effect.forEach(directories, (directory) => - Effect.gen(function* () { - if (!(yield* fs.exists(directory.path))) return - log.info(` ${directory.label}: ${directory.path}`) - }), - ) + 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)}`), ) @@ -82,25 +73,21 @@ export default Runtime.handler( // 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[] = [] - yield* Effect.forEach(directories, (directory) => - Effect.gen(function* () { - progress.start(`Removing ${directory.label}...`) - yield* fs.remove(directory.path, { recursive: true, force: true }).pipe( - // Windows reports a terminated service as gone before it releases its database - // and log 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 ${directory.label}`))), - Effect.catch((error) => - Effect.sync(() => { - progress.stop(`Failed to remove ${directory.label}`, 1) - errors.push(`${directory.label}: ${errorMessage(error)}`) - }), - ), - ) + 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( diff --git a/services/www/src/docs/content/cli/index.mdx b/services/www/src/docs/content/cli/index.mdx index ec370c62b03..b6c997d536a 100644 --- a/services/www/src/docs/content/cli/index.mdx +++ b/services/www/src/docs/content/cli/index.mdx @@ -96,8 +96,8 @@ opencode uninstall --dry-run ``` Run `opencode uninstall` to confirm removal. OpenCode stops registered background services and persistent terminals before -removing the global cache and state directories. These directories are shared by OpenCode versions and channels. Session -data and configuration are never removed. +removing the global cache directory, which is shared by OpenCode versions and channels. Session data, configuration, and +state are never removed. - `--force` (`-f`) skips confirmation; use it for noninteractive removal. - Package-manager installations use the detected package manager. Curl installations print the final command to remove