Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 46 additions & 5 deletions packages/opencode/src/global/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,53 @@ export namespace Global {
} as const
}

function ensureDir(dir: string) {
return fs.mkdir(dir, { recursive: true }).catch((err) => {
if (err.code === "EACCES") {
const parent = path.dirname(dir)
const isWindows = process.platform === "win32"

const remediation = isWindows
? [
"On Windows, adjust the folder permissions so your user can write to it.",
"For example, in an elevated PowerShell prompt you can run:",
` icacls "${parent}" /grant "${process.env.USERNAME}:(OI)(CI)M" /T`,
].join("\n")
: [
"To fix this on Unix-like systems, run:",
` sudo chown -R $(whoami) "${parent}"`,
].join("\n")

const dataDirInstruction = isWindows
? ' $env:XDG_DATA_HOME="$HOME\\.opencode-data"'
: ' export XDG_DATA_HOME="$HOME/.opencode-data"'

const message = [
`Error: Permission denied creating directory: ${dir}`,
"",
`The parent directory "${parent}" exists but opencode cannot write to it.`,
"This can happen when another application created it with restrictive permissions.",
"",
remediation,
"",
"Or set a custom data directory:",
dataDirInstruction,
"",
].join("\n")

console.error(message)
process.exit(1)
}
throw err
})
}

await Promise.all([
fs.mkdir(Global.Path.data, { recursive: true }),
fs.mkdir(Global.Path.config, { recursive: true }),
fs.mkdir(Global.Path.state, { recursive: true }),
fs.mkdir(Global.Path.log, { recursive: true }),
fs.mkdir(Global.Path.bin, { recursive: true }),
ensureDir(Global.Path.data),
ensureDir(Global.Path.config),
ensureDir(Global.Path.state),
ensureDir(Global.Path.log),
ensureDir(Global.Path.bin),
])

const CACHE_VERSION = "14"
Expand Down