Skip to content

Commit f5ac1cd

Browse files
committed
feat: add OPENCODE_CACHE_DIR env var for portable mode
Ref anomalyco#8968
1 parent b1f6d62 commit f5ac1cd

6 files changed

Lines changed: 26 additions & 2 deletions

File tree

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { Flag } from "../flag/flag"
77
const app = "opencode"
88

99
const data = Flag.OPENCODE_DATA_DIR || path.join(xdgData!, app)
10-
const cache = path.join(xdgCache!, app)
10+
const cache = Flag.OPENCODE_CACHE_DIR || path.join(xdgCache!, app)
1111
const config = path.join(xdgConfig!, app)
1212
const state = path.join(xdgState!, app)
1313

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

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

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,22 @@ or `%LOCALAPPDATA%\opencode` on Windows.
154154

155155
---
156156

157+
### Custom cache directory
158+
159+
Specify a custom cache directory using the `OPENCODE_CACHE_DIR`
160+
environment variable. This controls where OpenCode stores cached data
161+
like npm plugins and their dependencies.
162+
163+
```bash
164+
export OPENCODE_CACHE_DIR=/path/to/my/cache-directory
165+
opencode run "Hello world"
166+
```
167+
168+
By default, OpenCode stores cache in `~/.cache/opencode` on Linux/macOS
169+
or `%LOCALAPPDATA%\opencode` on Windows.
170+
171+
---
172+
157173
## Schema
158174

159175
The config file has a schema that's defined in [**`opencode.ai/config.json`**](https://opencode.ai/config.json).

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Browse available plugins in the [ecosystem](/docs/ecosystem#plugins).
4545

4646
### How plugins are installed
4747

48-
**npm plugins** are installed automatically using Bun at startup. Packages and their dependencies are cached in `~/.cache/opencode/node_modules/`.
48+
**npm plugins** are installed automatically using Bun at startup. Packages and their dependencies are cached in the cache directory (`~/.cache/opencode/node_modules/` by default, or `$OPENCODE_CACHE_DIR/node_modules/` if set).
4949

5050
**Local plugins** are loaded directly from the plugin directory. To use external packages, you must create a `package.json` within your config directory (see [Dependencies](#dependencies)), or publish the plugin to npm and [add it to your config](/docs/config#plugins).
5151

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,12 @@ To resolve provider package issues:
129129
rm -rf ~/.cache/opencode
130130
```
131131

132+
Or if you have a custom cache directory set:
133+
134+
```bash
135+
rm -rf $OPENCODE_CACHE_DIR
136+
```
137+
132138
2. Restart opencode to reinstall the latest provider packages
133139

134140
This will force opencode to download the most recent versions of provider packages, which often resolves compatibility issues with model parameters and API changes.

0 commit comments

Comments
 (0)