Skip to content

Commit 26f12ec

Browse files
Apply PR #11402: refactor: unify path resolve
2 parents 88aabee + 66941b6 commit 26f12ec

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
@@ -1176,10 +1176,7 @@ export namespace Config {
11761176
if (lineIndex !== -1 && lines[lineIndex].trim().startsWith("//")) {
11771177
continue // Skip if line is commented
11781178
}
1179-
let filePath = match.replace(/^\{file:/, "").replace(/\}$/, "")
1180-
if (filePath.startsWith("~/")) {
1181-
filePath = path.join(os.homedir(), filePath.slice(2))
1182-
}
1179+
const filePath = Filesystem.resolveTilde(match.replace(/^\{file:/, "").replace(/\}$/, ""))
11831180
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(configDir, filePath)
11841181
const fileContent = (
11851182
await Bun.file(resolvedPath)
@@ -1236,14 +1233,26 @@ export namespace Config {
12361233
await Bun.write(configFilepath, updated).catch(() => {})
12371234
}
12381235
const data = parsed.data
1239-
if (data.plugin) {
1240-
for (let i = 0; i < data.plugin.length; i++) {
1241-
const plugin = data.plugin[i]
1242-
try {
1243-
data.plugin[i] = import.meta.resolve!(plugin, configFilepath)
1244-
} catch (err) {}
1236+
const expand = (arr?: string[]) => arr?.forEach((v, i) => (arr[i] = Filesystem.resolveTilde(v)))
1237+
1238+
expand(data.instructions)
1239+
expand(data.plugin)
1240+
if (data.skills) expand(data.skills.paths)
1241+
1242+
for (const mcp of Object.values(data.mcp ?? {}))
1243+
if (typeof mcp === "object" && "type" in mcp && mcp.type === "local") expand(mcp.command)
1244+
for (const lsp of Object.values(data.lsp ?? {}))
1245+
if (typeof lsp === "object" && "command" in lsp) expand(lsp.command)
1246+
for (const fmt of Object.values(data.formatter ?? {}))
1247+
if (typeof fmt === "object" && "command" in fmt) expand(fmt.command)
1248+
1249+
data.plugin = data.plugin?.map((p: string) => {
1250+
try {
1251+
return import.meta.resolve!(p, configFilepath)
1252+
} catch {
1253+
return p
12451254
}
1246-
}
1255+
})
12471256
return data
12481257
}
12491258

packages/opencode/src/session/instruction.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,6 @@ export namespace InstructionPrompt {
9393
if (config.instructions) {
9494
for (let instruction of config.instructions) {
9595
if (instruction.startsWith("https://") || instruction.startsWith("http://")) continue
96-
if (instruction.startsWith("~/")) {
97-
instruction = path.join(os.homedir(), instruction.slice(2))
98-
}
9996
const matches = path.isAbsolute(instruction)
10097
? await Array.fromAsync(
10198
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
@@ -129,8 +129,7 @@ export namespace Skill {
129129
// Scan additional skill paths from config
130130
const config = await Config.get()
131131
for (const skillPath of config.skills?.paths ?? []) {
132-
const expanded = skillPath.startsWith("~/") ? path.join(os.homedir(), skillPath.slice(2)) : skillPath
133-
const resolved = path.isAbsolute(expanded) ? expanded : path.join(Instance.directory, expanded)
132+
const resolved = path.isAbsolute(skillPath) ? skillPath : path.join(Instance.directory, skillPath)
134133
if (!(await Filesystem.isDir(resolved))) {
135134
log.warn("skill path not found", { path: resolved })
136135
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)