Skip to content

fix(debug): Delve 在当前文件所在的包目录构建、调试和运行 - #10

Merged
xiaoyumuxi merged 1 commit into
mainfrom
fix/delve-dlvcwd-package-directory
Nov 15, 2026
Merged

xiaoyumuxi merged 1 commit into
mainfrom
fix/delve-dlvcwd-package-directory

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

背景

如果打开的文件夹根目录没有 go.mod,而是并排放着几个模块,例如课程目录下的 cnscc368-1…4,调试其中某个模块的 main.go 会报 Failed to launch: Build error ... go.mod file not found。原因是 Delve 在工作区根目录执行 go build。即使能编译,被调程序也在根目录运行,examples/topo1.txt 这类按包目录写的相对路径打不开。

用 Delve 1.27.2 的 DAP 实测,需要同时满足下面三点:

  • dlvCwd ← 包目录:Delve 先 os.Chdir(dlvCwd) 再执行 go build。
  • env.PWD ← 包目录:Delve 不更新 PWD;go 只在 PWD 和真实目录一致时用它。路径经过符号链接(macOS 的 /var)时,如果不设 PWD,会报 outside main module,断点报 could not find file。
  • cwd ← 包目录:调试引擎在 launch 参数缺 cwd 时会填工作区根目录。

改动

  • io.github.lithe-idea.go-support/plugin.json,Delve 的 launch 配置:

    • arguments:{"mode": "debug", "program": ".", "env": {"GO111MODULE": "auto"}}。program: "." 配合 dlvCwd 构建当前包;GO111MODULE=auto 让没有 go.mod 的目录按 GOPATH 模式构建目录里的全部文件。
    • targetBindings:{"cwd": "target.directory", "args": "target.arguments", "adapterArguments": {"dlvCwd": "target.directory", "env.PWD": "target.directory"}},去掉了原来的 program 和 env 绑定。
  • e2e/host/desktop/internal/workbench/official_go_plugin_debug_e2e_test.go:新增 TestOfficialGoPluginDebugsEachProjectOfAWorkspaceWithDelve。工作区根目录没有 go.mod,包含:

    • lab-1、lab-2 两个模块,其中 lab-2 读取包目录下的 examples/greeting.txt,读不到就 panic;
    • lab-2/cmd/tool 子包;
    • 没有 go.mod 的 scratch 目录。

    测试依次调试 lab-2/main.go、lab-2/cmd/tool/main.go、scratch/main.go,检查断点命中的文件和行号、程序正常结束,并确认没有留下 __debug_bin*。

  • e2e/host/lithe-host.json:测试正则加入新测试。

  • README.md:调试一节说明包目录、多模块工作区和没有 go.mod 的目录。

  • docs/feature-matrix.md:go-debug-delve 更新能力说明和宿主依赖。

依赖与发版

验证

  • node scripts/package.mjs:打包通过。
  • TestOfficialGoPluginBuildsAndServesAModule:在固定 Core 671cdc6 的宿主分支上通过。
  • 调试 e2e 在 671cdc6 上暴露了 cwd 落到工作区根目录的问题(lab-2 读文件 panic),于是 Core #55 增加了 cwd ← target.directory。
  • 合入后补跑(2026-11-15):node scripts/e2e-host.mjs --ref fix/core-pin-56-merge,宿主为 fix(desktop): Core 固定版本改到 Lithe-core-rust#56 的合入提交 1lck/Lithe-IDEA#1976(Core 9425469,即 #55 + #56 在 Core main 上的提交),插件包来自 main 8595a25。TestOfficialGoPluginDebugsEachProjectOfAWorkspaceWithDelve 和 TestOfficialGoPluginBuildsAndServesAModule 都通过。docs/feature-matrix.md 的“最近验证”和 e2e/host/lithe-host.json 的宿主固定版本,等 #1976 合入后另开 PR 更新。

打开的文件夹根目录没有 go.mod、并排放多个模块时,Delve 在工作区根目录执行 go build,
报 Build error;被调程序也在根目录运行,按包目录读的相对路径文件找不到。

- targetBindings.adapterArguments:dlvCwd 和 env.PWD 绑到 target.directory,go build 在包目录执行
- targetBindings.cwd 绑到 target.directory:程序在包目录运行
- arguments 改为 program "."、env GO111MODULE=auto:没有 go.mod 的目录也能调试
- e2e 覆盖多模块工作区、子命令包和没有 go.mod 的目录
- README 与功能矩阵说明新行为和宿主依赖(Lithe-core-rust#55)
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 15, 2026 10:34
@xiaoyumuxi
xiaoyumuxi merged commit 109ae6a into main Nov 15, 2026
1 check passed
xiaoyumuxi added a commit that referenced this pull request Nov 15, 2026
1.0.0 预发布已撤回(release 和 tag 已删除),从包含 #10 的 main 重发:
- 发布说明的调试功能补充包目录、多模块工作区、子包和没有 go.mod 的目录
- 兼容性和升级说明改为需要固定 Lithe-core-rust#55 的桌面端(1lck/Lithe-IDEA#1975)
- release.yml 的预发布说明同步
@xiaoyumuxi
xiaoyumuxi deleted the fix/delve-dlvcwd-package-directory branch November 15, 2026 10:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant