fix(cli): keep state on uninstall

This commit is contained in:
Dax Raad
2026-10-07 16:45:58 -04:00
Unverified
parent d016db856c
commit e3419d47ee
3 changed files with 20 additions and 33 deletions
+1 -1
View File
@@ -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"),
+17 -30
View File
@@ -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(
+2 -2
View File
@@ -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