Skip to content

[bug] 项目目录变更后 opencode serve Web 界面报错,TUI 模式正常 #40222

Closed

Description

@LazyGrep

Bug 报告:项目目录移动后 opencode serve Web 界面报错

标题

[bug] 项目目录变更后,Web 模式(opencode serve)报错,TUI 模式正常

环境

  • opencode 版本:1.18.4(message=created ... version=1.18.4)
  • 平台:Linux(root)
  • 触发方式:opencode serve 后用浏览器访问

复现步骤

  1. 在 /usr/local/zhuque-ledger 目录使用过 opencode(TUI 或 serve),产生会话与快照数据
  2. 将项目整个移动/重命名到 /projects/zhuque-ledger
  3. 在新目录启动 opencode serve,用浏览器访问
  4. 结果:Web 界面报错,无法正常加载

预期行为

项目目录变更后,Web 模式应能正常加载会话;或至少能识别 path 已迁移。

实际行为

  • TUI 模式正常(直接基于当前工作目录工作)
  • Web 模式报错。排查发现持久化数据中残留旧路径 /usr/local/zhuque-ledger:

1. 快照 Git 仓库引用已删除目录

~/.local/share/opencode/snapshot/<projectID>/<snapshotID>/:

  • config 中 worktree = /usr/local/zhuque-ledger
  • objects/info/alternates 指向 /usr/local/zhuque-ledger/.git/objects(目录已不存在,git 操作失败)

同一项目下新旧两个快照并存:

030dcaab.../config      worktree = /usr/local/zhuque-ledger
12e88a97.../config      worktree = /projects/zhuque-ledger

2. 数据库 project 表 worktree 未更新

~/.local/share/opencode/opencode.db:

project: ('1b5fceac...', '/usr/local/zhuque-ledger', 'git', ...)
project_directory: ('1b5fceac...', '/projects/zhuque-ledger', ...)

project.worktree 与 project_directory 不一致。

3. 数据库 session 表残留旧路径

('ses_039c968e...', 'global', '/usr/local/zhuque-ledger', 'usr/local/zhuque-ledger', ...)

directory / path 字段仍为旧路径。

建议修复

  • 项目路径变更时(或 serve 启动时检测到 worktree 不存在但 project_directory 存在),自动迁移/更新:
    • project.worktree
    • 快照 config 的 worktree 与 alternates 路径(或按需重建快照)
    • session.directory / session.path
  • 或提供显式的「迁移项目路径」能力/文档。

临时规避

手动执行以下修复后 Web 模式恢复正常:

# 1. 修正快照 git 仓库路径
sed -i 's|/usr/local/zhuque-ledger|/projects/zhuque-ledger|g' \
  ~/.local/share/opencode/snapshot/*/<旧快照ID>/config \
  ~/.local/share/opencode/snapshot/*/<旧快照ID>/objects/info/alternates

# 2. 修正数据库路径(先备份)
python3 -c "
import sqlite3
conn = sqlite3.connect('/root/.local/share/opencode/opencode.db')
cur = conn.cursor()
cur.execute(\"UPDATE project SET worktree='/projects/zhuque-ledger' WHERE id='<projectID>'\")
cur.execute(\"UPDATE session SET directory=REPLACE(directory,'/usr/local/zhuque-ledger','/projects/zhuque-ledger'), path=REPLACE(path,'/usr/local/zhuque-ledger','/projects/zhuque-ledger')\")
conn.commit(); conn.close()
"

Activity

  1. github-actions commented on Aug 3, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

  2. Hona commented on Sep 2, 2026

    @Hona
    Member

    Fixed for opencode2 launch via #46713
    You can try the desktop beta here: https://github.com/anomalyco/opencode-beta

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions