Skip to content

Commit aa1e3b0

Browse files
Apply PR #11402: refactor: unify path resolve
2 parents 85b3a7f + 66941b6 commit aa1e3b0

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
@@ -1164,10 +1164,7 @@ export namespace Config {
11641164
if (lineIndex !== -1 && lines[lineIndex].trim().startsWith("//")) {
11651165
continue // Skip if line is commented
11661166
}
1167-
let filePath = match.replace(/^\{file:/, "").replace(/\}$/, "")
1168-
if (filePath.startsWith("~/")) {
1169-
filePath = path.join(os.homedir(), filePath.slice(2))
1170-
}
1167+
const filePath = Filesystem.resolveTilde(match.replace(/^\{file:/, "").replace(/\}$/, ""))
11711168
const resolvedPath = path.isAbsolute(filePath) ? filePath : path.resolve(configDir, filePath)
11721169
const fileContent = (
11731170
await Bun.file(resolvedPath)
@@ -1224,14 +1221,26 @@ export namespace Config {
12241221
await Bun.write(configFilepath, updated).catch(() => {})
12251222
}
12261223
const data = parsed.data
1227-
if (data.plugin) {
1228-
for (let i = 0; i < data.plugin.length; i++) {
1229-
const plugin = data.plugin[i]
1230-
try {
1231-
data.plugin[i] = import.meta.resolve!(plugin, configFilepath)
1232-
} catch (err) {}
1224+
const expand = (arr?: string[]) => arr?.forEach((v, i) => (arr[i] = Filesystem.resolveTilde(v)))
1225+
1226+
expand(data.instructions)
1227+
expand(data.plugin)
1228+
if (data.skills) expand(data.skills.paths)
1229+
1230+
for (const mcp of Object.values(data.mcp ?? {}))
1231+
if (typeof mcp === "object" && "type" in mcp && mcp.type === "local") expand(mcp.command)
1232+
for (const lsp of Object.values(data.lsp ?? {}))
1233+
if (typeof lsp === "object" && "command" in lsp) expand(lsp.command)
1234+
for (const fmt of Object.values(data.formatter ?? {}))
1235+
if (typeof fmt === "object" && "command" in fmt) expand(fmt.command)
1236+
1237+
data.plugin = data.plugin?.map((p: string) => {
1238+
try {
1239+
return import.meta.resolve!(p, configFilepath)
1240+
} catch {
1241+
return p
12331242
}
1234-
}
1243+
})
12351244
return data
12361245
}
12371246

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
@@ -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)