Skip to content

Commit b1f6d62

Browse files
committed
feat: add OPENCODE_DATA_DIR env var for portable mode
Allow users to override the data directory location via the OPENCODE_DATA_DIR environment variable. This enables full portable mode when combined with OPENCODE_CONFIG_DIR. Closes anomalyco#4526
1 parent da78b75 commit b1f6d62

4 files changed

Lines changed: 18 additions & 1 deletion

File tree

‎packages/opencode/src/flag/flag.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export namespace Flag {
44
export const OPENCODE_CONFIG = process.env["OPENCODE_CONFIG"]
55
export const OPENCODE_CONFIG_DIR = process.env["OPENCODE_CONFIG_DIR"]
66
export const OPENCODE_CONFIG_CONTENT = process.env["OPENCODE_CONFIG_CONTENT"]
7+
export const OPENCODE_DATA_DIR = process.env["OPENCODE_DATA_DIR"]
78
export const OPENCODE_DISABLE_AUTOUPDATE = truthy("OPENCODE_DISABLE_AUTOUPDATE")
89
export const OPENCODE_DISABLE_PRUNE = truthy("OPENCODE_DISABLE_PRUNE")
910
export const OPENCODE_DISABLE_TERMINAL_TITLE = truthy("OPENCODE_DISABLE_TERMINAL_TITLE")

‎packages/opencode/src/global/index.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import fs from "fs/promises"
22
import { xdgData, xdgCache, xdgConfig, xdgState } from "xdg-basedir"
33
import path from "path"
44
import os from "os"
5+
import { Flag } from "../flag/flag"
56

67
const app = "opencode"
78

8-
const data = path.join(xdgData!, app)
9+
const data = Flag.OPENCODE_DATA_DIR || path.join(xdgData!, app)
910
const cache = path.join(xdgCache!, app)
1011
const config = path.join(xdgConfig!, app)
1112
const state = path.join(xdgState!, app)

‎packages/web/src/content/docs/cli.mdx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,7 @@ OpenCode can be configured using environment variables.
558558
| `OPENCODE_CONFIG` | string | Path to config file |
559559
| `OPENCODE_CONFIG_DIR` | string | Path to config directory |
560560
| `OPENCODE_CONFIG_CONTENT` | string | Inline json config content |
561+
| `OPENCODE_DATA_DIR` | string | Path to data directory |
561562
| `OPENCODE_DISABLE_AUTOUPDATE` | boolean | Disable automatic update checks |
562563
| `OPENCODE_DISABLE_PRUNE` | boolean | Disable pruning of old data |
563564
| `OPENCODE_DISABLE_TERMINAL_TITLE` | boolean | Disable automatic terminal title updates |

‎packages/web/src/content/docs/config.mdx‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,20 @@ opencode run "Hello world"
138138

139139
The custom directory is loaded after the global config and `.opencode` directories, so it **can override** their settings.
140140

141+
### Custom data directory
142+
143+
Specify a custom data directory using the `OPENCODE_DATA_DIR`
144+
environment variable. This controls where OpenCode stores runtime data
145+
like authentication tokens, logs, cached files, and LSP binaries.
146+
147+
```bash
148+
export OPENCODE_DATA_DIR=/path/to/my/data-directory
149+
opencode run "Hello world"
150+
```
151+
152+
By default, OpenCode stores data in `~/.local/share/opencode` on Linux/macOS
153+
or `%LOCALAPPDATA%\opencode` on Windows.
154+
141155
---
142156

143157
## Schema

0 commit comments

Comments
 (0)