Skip to content

Commit 310e319

Browse files
refactor: unify path resolve
1 parent aef0e58 commit 310e319

4 files changed

Lines changed: 26 additions & 16 deletions

File tree

packages/opencode/src/config/config.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,10 +1162,7 @@ export namespace Config {
11621162
if (lineIndex !== -1 && lines[lineIndex].trim().startsWith("//")) {
11631163
continue // Skip if line is commented
11641164
}
1165-
let filePath = match.replace(/^\{file:/, "").replace(/\}$/, "")
1166-
if (filePath.startsWith("~/")) {
1167-
filePath = path.join(os.homedir(), filePath.slice(2))
1168-
}
1165+
const filePath = Filesystem.resolveTilde(match.replace(/^\{file:/, "").replace(/\}$/, ""))
11691166
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(configDir, filePath)
11701167
const fileContent = (
11711168
await Bun.file(resolvedPath)
@@ -1222,14 +1219,26 @@ export namespace Config {
12221219
await Bun.write(configFilepath, updated).catch(() => {})
12231220
}
12241221
const data = parsed.data
1225-
if (data.plugin) {
1226-
for (let i = 0; i < data.plugin.length; i++) {
1227-
const plugin = data.plugin[i]
1228-
try {
1229-
data.plugin[i] = import.meta.resolve!(plugin, configFilepath)
1230-
} catch (err) {}
1222+
const expand = (arr?: string[]) => arr?.forEach((v, i) => (arr[i] = Filesystem.resolveTilde(v)))
1223+
1224+
expand(data.instructions)
1225+
expand(data.plugin)
1226+
if (data.skills) expand(data.skills.paths)
1227+
1228+
for (const mcp of Object.values(data.mcp ?? {}))
1229+
if (typeof mcp === "object" && "type" in mcp && mcp.type === "local") expand(mcp.command)
1230+
for (const lsp of Object.values(data.lsp ?? {}))
1231+
if (typeof lsp === "object" && "command" in lsp) expand(lsp.command)
1232+
for (const fmt of Object.values(data.formatter ?? {}))
1233+
if (typeof fmt === "object" && "command" in fmt) expand(fmt.command)
1234+
1235+
data.plugin = data.plugin?.map((p: string) => {
1236+
try {
1237+
return import.meta.resolve!(p, configFilepath)
1238+
} catch {
1239+
return p
12311240
}
1232-
}
1241+
})
12331242
return data
12341243
}
12351244

packages/opencode/src/session/instruction.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,6 @@ export namespace InstructionPrompt {
9191
if (config.instructions) {
9292
for (let instruction of config.instructions) {
9393
if (instruction.startsWith("https://") || instruction.startsWith("http://")) continue
94-
if (instruction.startsWith("~/")) {
95-
instruction = path.join(os.homedir(), instruction.slice(2))
96-
}
9794
const matches = path.isAbsolute(instruction)
9895
? await Array.fromAsync(
9996
new Bun.Glob(path.basename(instruction)).scan({

packages/opencode/src/skill/skill.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,7 @@ export namespace Skill {
127127
// Scan additional skill paths from config
128128
const config = await Config.get()
129129
for (const skillPath of config.skills?.paths ?? []) {
130-
const expanded = skillPath.startsWith("~/") ? path.join(os.homedir(), skillPath.slice(2)) : skillPath
131-
const resolved = path.isAbsolute(expanded) ? expanded : path.join(Instance.directory, expanded)
130+
const resolved = path.isAbsolute(skillPath) ? skillPath : path.join(Instance.directory, skillPath)
132131
if (!(await Filesystem.isDir(resolved))) {
133132
log.warn("skill path not found", { path: resolved })
134133
continue

packages/opencode/src/util/filesystem.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import { realpathSync } from "fs"
22
import { dirname, join, relative } from "path"
3+
import os from "os"
34

45
export namespace Filesystem {
6+
export function resolveTilde(p: string): string {
7+
return p.startsWith("~/") ? join(os.homedir(), p.slice(2)) : p
8+
}
9+
510
export const exists = (p: string) =>
611
Bun.file(p)
712
.stat()

0 commit comments

Comments
 (0)