fix(data-path): writable directory fallback for data/cache paths

getDataDir() and getCacheDir() now verify the directory is writable and
fall back to os.tmpdir() if not.

🤖 GENERATED WITH ASSISTANCE OF [OhMyOpenCode](https://github.com/code-yeongyu/oh-my-opencode)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
YeonGyu-Kim
2026-03-25 11:44:21 +09:00
parent 78a3e985be
commit 919f7e4092

View File

@@ -1,5 +1,18 @@
import * as path from "node:path"
import * as os from "node:os"
import { accessSync, constants, mkdirSync } from "node:fs"
function resolveWritableDirectory(preferredDir: string, fallbackSuffix: string): string {
try {
mkdirSync(preferredDir, { recursive: true })
accessSync(preferredDir, constants.W_OK)
return preferredDir
} catch {
const fallbackDir = path.join(os.tmpdir(), fallbackSuffix)
mkdirSync(fallbackDir, { recursive: true })
return fallbackDir
}
}
/**
* Returns the user-level data directory.
@@ -10,7 +23,8 @@ import * as os from "node:os"
* including Windows, so we match that behavior exactly.
*/
export function getDataDir(): string {
return process.env.XDG_DATA_HOME ?? path.join(os.homedir(), ".local", "share")
const preferredDir = process.env.XDG_DATA_HOME ?? path.join(os.homedir(), ".local", "share")
return resolveWritableDirectory(preferredDir, "opencode-data")
}
/**
@@ -27,7 +41,8 @@ export function getOpenCodeStorageDir(): string {
* - All platforms: XDG_CACHE_HOME or ~/.cache
*/
export function getCacheDir(): string {
return process.env.XDG_CACHE_HOME ?? path.join(os.homedir(), ".cache")
const preferredDir = process.env.XDG_CACHE_HOME ?? path.join(os.homedir(), ".cache")
return resolveWritableDirectory(preferredDir, "opencode-cache")
}
/**