diff --git a/README.md b/README.md index 3c16b3d..61bae49 100644 --- a/README.md +++ b/README.md @@ -76,11 +76,14 @@ 打开「调试」窗口,适配器选 **Go (Delve)**,打开要调试的 `.go` 文件,在行号旁设好断点后点「开始」。 -- 调试的是当前文件所在的包:Delve 先构建这个包,再在断点处停下。所以要调试 `cmd/server` 的程序, +- 调试的是当前文件所在的包:Delve 在这个包的目录里构建它,再在断点处停下。所以要调试 `cmd/server` 的程序, 打开 `cmd/server` 里的任意文件即可。 -- 程序的工作目录是项目根目录。 +- 打开的文件夹里可以并排放好几个项目,根目录不需要 `go.mod`:Delve 从包所在的目录往上找它所属的模块。 +- 没有 `go.mod` 的目录也能调试,目录里的所有 `.go` 文件一起构建;这时 Delve 以 `GO111MODULE=auto` 构建, + 只能导入标准库和 `GOPATH/src` 里的包。 +- 程序的工作目录是包所在的目录,所以 `os.ReadFile("examples/input.txt")` 这样的相对路径从包目录算起。 - 程序结束后会话自动结束,程序的输出留在调试控制台里。 -- Delve 在项目根目录生成名为 `__debug_bin…` 的临时程序,会话结束时自己删除;新建项目生成的 `.gitignore` +- Delve 在包所在的目录生成名为 `__debug_bin…` 的临时程序,会话结束时自己删除;新建项目生成的 `.gitignore` 已经忽略它。 - 目前只支持启动调试(launch),不支持附加到运行中的进程,也不支持调试测试。 diff --git a/docs/feature-matrix.md b/docs/feature-matrix.md index 886b484..268809a 100644 --- a/docs/feature-matrix.md +++ b/docs/feature-matrix.md @@ -84,13 +84,14 @@ gofumpt 设置立即生效,以及工作流结束后安装目录逐字节不变 ## go-debug-delve **能力**:「调试」窗口里的 Go (Delve) 适配器。Lithe 启动在 Lithe 里安装时记下的或用户 `PATH` 里的 `dlv dap`,读到 Delve 在标准输出打印的 -`DAP server listening at: <地址>` 后连接这个本机地址;调试当前文件所在的包(`target.directory` 绑定到 `program`), -工作目录是项目根目录。支持断点、单步、调用栈、变量和表达式求值,程序输出显示在调试控制台,程序结束后会话自动结束。 +`DAP server listening at: <地址>` 后连接这个本机地址;在当前文件所在的包目录里构建并调试这个包(`dlvCwd` 和 `env.PWD` 绑定到 `target.directory`,`program` 是 `.`), +程序的工作目录也是包目录(`cwd` 绑定到 `target.directory`,不再是调试引擎默认的工作区根目录)。打开的文件夹根目录不需要 `go.mod`,可以并排放多个模块;没有 `go.mod` 的目录以 +`GO111MODULE=auto` 构建目录里的全部文件。支持断点、单步、调用栈、变量和表达式求值,程序输出显示在调试控制台,程序结束后会话自动结束。 缺少 `dlv` 时可以在插件详情的能力卡片上安装。 | 平台 | 实现 | 验证 | 说明 | | --- | --- | --- | --- | -| macOS | implemented | pending | 需要宿主支持 `tcpAnnounced` 调试适配器(1lck/Lithe-IDEA#1954)、Rust Core a58e07e 或更新版本的 `target.directory` 绑定,以及宿主按工作目录设置子进程 `PWD`(1lck/Lithe-IDEA#1958;没有它时,项目路径经过符号链接的话 Delve 编译会失败) | +| macOS | implemented | pending | 需要宿主支持 `tcpAnnounced` 调试适配器(1lck/Lithe-IDEA#1954),以及带 `targetBindings.adapterArguments`、且 `targetBindings.cwd` 可绑到 `target.directory` 的 Rust Core(Lithe-core-rust#55,宿主固定版本在 1lck/Lithe-IDEA 的跟进 PR 里)。旧宿主不认识这个字段,会拒绝安装本版本 | | Windows | missing | not-applicable | 没有 Windows 包 | **证据**: @@ -100,11 +101,12 @@ gofumpt 设置立即生效,以及工作流结束后安装目录逐字节不变 - `e2e/host/desktop/internal/workbench/official_go_plugin_debug_e2e_test.go`:宿主端到端测试。 **依赖的宿主能力**:`debugAdapters` 的 `tcpAnnounced` 传输(宿主启动适配器进程,交给 Core 判断哪一行是地址公告, -再连接公告的本机端口)、`target.directory` 绑定(Core a58e07e,Lithe-core-rust#47)、 +再连接公告的本机端口)、`targetBindings.adapterArguments` 的点分路径绑定(Lithe-core-rust#55)、 `commandInstallers`(Core c550c2b,宿主 1lck/Lithe-IDEA#1971)。 **限制**:只支持 launch,不支持 attach 和调试测试;适配器只在打开 `.go` 文件时可选(选择器是 `file` 加 `go` 语言), -从构建窗口的运行入口启动调试还不支持。Delve 在项目根目录生成 `__debug_bin…` 临时程序,会话结束时删除。 +从构建窗口的运行入口启动调试还不支持。Delve 在包目录生成 `__debug_bin…` 临时程序,会话结束时删除。 +没有 `go.mod` 的目录以 GOPATH 模式构建,只能导入标准库和 `GOPATH/src` 里的包。 适配器名称 Go (Delve) 目前不能翻译:插件语言文件还不覆盖调试适配器。 **最近验证**:2026-11-15,macOS(Apple Silicon),插件 0.1.0 发布包,宿主 `cebbe0def`(1lck/Lithe-IDEA#1958 的分支提交), @@ -115,10 +117,13 @@ Go 1.27.4、Delve 1.27.2。`node scripts/e2e-host.mjs` 通过 `TestOfficialGoPlu **验证方法**(macOS): 1. 安装插件包,确认安装对话框列出启动调试器权限;`PATH` 里有 `dlv`。 -2. 打开含 `go.mod` 的项目,打开 `main.go`,在一行上设断点,在「调试」窗口选 Go (Delve) 后点「开始」。 +2. 打开根目录没有 `go.mod`、里面并排放了几个模块的文件夹,打开其中一个模块的 `main.go`,在一行上设断点, + 在「调试」窗口选 Go (Delve) 后点「开始」。 3. 程序停在断点:调用栈顶是 `main.main` 和断点所在行,变量里能看到局部变量的值;单步、表达式求值可用。 -4. 点「继续」:程序运行到结束,输出显示在调试控制台,会话自动结束,项目根目录里没有留下 `__debug_bin…`。 -5. 调试 `cmd/<名字>` 这样的子包:打开该包里的文件后开始,调试的是这个包。 +4. 点「继续」:程序运行到结束,按相对路径读到包目录里的文件,输出显示在调试控制台,会话自动结束, + 包目录和文件夹根目录里都没有留下 `__debug_bin…`。 +5. 调试 `cmd/<名字>` 这样的子包:打开该包里的文件后开始,调试的是这个包;再调试一个没有 `go.mod`、 + 由两个文件组成的目录,两个文件一起构建。 6. 没有 `dlv` 时,插件详情「功能」页的调试器卡片显示“未找到 dlv”和安装按钮;安装后开始调试能停在断点。 7. 整个过程中插件安装目录不被写入。 diff --git a/e2e/host/desktop/internal/workbench/official_go_plugin_debug_e2e_test.go b/e2e/host/desktop/internal/workbench/official_go_plugin_debug_e2e_test.go index 5976dfd..6b14f8e 100644 --- a/e2e/host/desktop/internal/workbench/official_go_plugin_debug_e2e_test.go +++ b/e2e/host/desktop/internal/workbench/official_go_plugin_debug_e2e_test.go @@ -12,9 +12,34 @@ import ( "github.com/1lck/Lithe-IDEA/desktop/internal/core" ) -// goDebugSample is main.go of the debugged module. Zero-based line 6 is the -// Printf call, where main's local name already holds "Gopher". -const goDebugSample = "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tname := \"Gopher\"\n\tfmt.Printf(\"Hello, %s!\\n\", name)\n}\n" +// goDebugWorkspace lays out a workspace like a course folder: the root has no +// go.mod and holds several projects side by side. lab-2 is a module whose main +// package reads a file by a path relative to its own directory and whose +// cmd/tool is a second main package; scratch has no go.mod at all and spreads +// its main package over two files. +var goDebugWorkspace = map[string]string{ + "lab-1/go.mod": "module example.com/lab1\n\ngo 1.21\n", + "lab-1/main.go": "package main\n\nfunc main() {}\n", + "lab-2/go.mod": "module example.com/lab2\n\ngo 1.21\n", + "lab-2/main.go": "package main\n\nimport (\n\t\"fmt\"\n\t\"os\"\n\t\"strings\"\n)\n\n" + + "func main() {\n\tdata, err := os.ReadFile(\"examples/greeting.txt\")\n\tif err != nil {\n\t\tpanic(err)\n\t}\n" + + "\tname := strings.TrimSpace(string(data))\n\tfmt.Printf(\"Hello, %s!\\n\", name)\n}\n", + "lab-2/examples/greeting.txt": "Gopher\n", + "lab-2/cmd/tool/main.go": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tname := \"Tool\"\n\tfmt.Printf(\"Hello, %s!\\n\", name)\n}\n", + "scratch/main.go": "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tname := greeting()\n\tfmt.Printf(\"Hello, %s!\\n\", name)\n}\n", + "scratch/greeting.go": "package main\n\nfunc greeting() string { return \"Loose\" }\n", +} + +// goDebugProgram is one program of goDebugWorkspace the test debugs. +type goDebugProgram struct { + // source is the file, relative to the workspace, the breakpoint goes in. + source string + // line is the one-based line of the Printf call, where main's local name + // already holds its value. + line int + // local is that value as Variables shows it. + local string +} // samePath reports whether two paths name the same file, so a path Delve // reports through a symbolic link (macOS's /var is /private/var) still matches. @@ -51,12 +76,16 @@ func clickNamedWithoutSettling(t *testing.T, h *harness, name string) { } } -// TestOfficialGoPluginDebugsAModuleWithDelve covers the Delve adapter of the Go -// Support archive: the Debug window starts the user's dlv dap, the host reads the -// address Delve announces and connects, a breakpoint in main.go stops main.main -// with its local variable, Continue runs the program to its end and the session -// ends by itself, and Delve removes the program it built for the session. -func TestOfficialGoPluginDebugsAModuleWithDelve(t *testing.T) { +// TestOfficialGoPluginDebugsEachProjectOfAWorkspaceWithDelve covers the Delve +// adapter of the Go Support archive in a workspace whose root has no go.mod: +// the Debug window starts the user's dlv dap, the host reads the address Delve +// announces and connects, and for a module's main package, a second main +// package of that module and a directory without go.mod, a breakpoint stops +// main.main with its local variable, the program runs in its own directory, +// Continue runs it to its end, the session ends by itself, and Delve removes +// the program it built. The temporary directory is under macOS's /var, a +// symbolic link, which is where go build once read the wrong directory. +func TestOfficialGoPluginDebugsEachProjectOfAWorkspaceWithDelve(t *testing.T) { archive := os.Getenv("LITHE_TEST_GO_PLUGIN_ARCHIVE") if archive == "" { t.Skip("set LITHE_TEST_GO_PLUGIN_ARCHIVE to a go-support--darwin.zip") @@ -78,32 +107,50 @@ func TestOfficialGoPluginDebugsAModuleWithDelve(t *testing.T) { before := packageDigest(t, installed) h.app.reloadPluginLanguages() root := t.TempDir() - writePluginFixture(t, root, map[string]string{ - "go.mod": "module example.com/hello\n\ngo 1.21\n", - "main.go": goDebugSample, - }) + writePluginFixture(t, root, goDebugWorkspace) p := openHostedPanel(t, h, root) if declaration := p.debug.adapters[0].Declaration; declaration.Transport != "tcpAnnounced" || declaration.LaunchKind != "systemCommand" { t.Fatalf("declaration = %+v, want Delve started as dlv dap and announcing its address", declaration) } - // Opening main.go starts gopls, whose tasks run until the project closes, so from - // here on every step waits for its own state instead of for all background work. - source := filepath.Join(root, "main.go") - h.app.openFileAtPosition(p, source, 6, 0) - awaitStateWithin(t, h, 30*time.Second, "main.go with the caret on the Printf line", func() bool { + for _, program := range []goDebugProgram{ + {source: "lab-2/main.go", line: 15, local: `"Gopher"`}, + {source: "lab-2/cmd/tool/main.go", line: 7, local: `"Tool"`}, + {source: "scratch/main.go", line: 7, local: `"Loose"`}, + } { + debugGoProgram(t, h, p, root, program) + } + if binaries := debugBinaries(t, root); len(binaries) != 0 { + t.Fatalf("Delve built in the workspace root: %v", binaries) + } + + h.app.closeProject() + awaitStateWithin(t, h, 30*time.Second, "the closed project", func() bool { return h.app.project == nil }) + h.settle() + assertPackageUnchanged(t, installed, before) +} + +// debugGoProgram debugs one program of the open workspace from a breakpoint on +// the Printf line of its source to its end. +func debugGoProgram(t *testing.T, h *harness, p *project, root string, program goDebugProgram) { + t.Helper() + source := filepath.Join(root, filepath.FromSlash(program.source)) + // Opening a Go file starts gopls, whose tasks run until the project closes, so + // every step waits for its own state instead of for all background work. + h.app.openFileAtPosition(p, source, program.line-1, 0) + awaitStateWithin(t, h, 30*time.Second, program.source+" with the caret on the Printf line", func() bool { d := p.selectedDocument(p.docs.group) if d == nil || !samePath(d.path, source) { return false } line, _ := d.view.Caret() - return line == 6 + return line == program.line-1 }) clickNamedWithoutSettling(t, h, h.shared("native.viewBreakpoints")) awaitStateWithin(t, h, 10*time.Second, "the Breakpoints dialog", func() bool { return p.panels.breakpointsOpen }) clickNamedWithoutSettling(t, h, h.app.text.Debug.ToggleBreakpoint) - awaitStateWithin(t, h, 10*time.Second, "the breakpoint on main.go:7", func() bool { + awaitStateWithin(t, h, 10*time.Second, "the breakpoint in "+program.source, func() bool { for path, lines := range p.debug.breakpoints { - if samePath(path, source) && len(lines) == 1 && lines[0].line == 7 { + if samePath(path, source) && len(lines) == 1 && lines[0].line == int64(program.line) { return true } } @@ -114,15 +161,15 @@ func TestOfficialGoPluginDebugsAModuleWithDelve(t *testing.T) { clickNamedWithoutSettling(t, h, h.app.text.Debug.Start) // Delve builds the package before it runs it, so the first stop waits for a build. - awaitStateWithin(t, h, 2*time.Minute, "Delve to stop at the breakpoint", func() bool { + awaitStateWithin(t, h, 2*time.Minute, "Delve to stop in "+program.source, func() bool { if p.debug.failure != "" { - t.Fatalf("debug start failed: %s\noutput: %q", p.debug.failure, p.debug.output) + t.Fatalf("debugging %s failed: %s\noutput: %q", program.source, p.debug.failure, p.debug.output) } return p.debug.state == "paused" && len(p.debug.frames) > 0 }) top := p.debug.frames[0] - if top.Name != "main.main" || top.Line != 7 || !samePath(top.SourcePath, source) { - t.Fatalf("top frame = %+v, want main.main at main.go:7", top) + if top.Name != "main.main" || top.Line != int64(program.line) || !samePath(top.SourcePath, source) { + t.Fatalf("top frame = %+v, want main.main at %s:%d", top, program.source, program.line) } // The locals open by themselves once the stop's scopes arrive. awaitStateWithin(t, h, time.Minute, "main's local name in Variables", func() bool { @@ -131,7 +178,7 @@ func TestOfficialGoPluginDebugsAModuleWithDelve(t *testing.T) { continue } for _, node := range scope.container.nodes { - if node.name == "name" && node.value == `"Gopher"` { + if node.name == "name" && node.value == program.local { return true } } @@ -144,20 +191,18 @@ func TestOfficialGoPluginDebugsAModuleWithDelve(t *testing.T) { } clickNamedWithoutSettling(t, h, h.app.text.Debug.Continue) - awaitStateWithin(t, h, time.Minute, "the program to end its session", func() bool { + awaitStateWithin(t, h, time.Minute, program.source+" to end its session", func() bool { return p.debug.activeSession() == nil && p.debug.state == "terminated" }) - if output := strings.Join(p.debug.output, "\n"); !strings.Contains(output, "Hello, Gopher!") { - t.Fatalf("the console does not show the program's output: %q", output) + // lab-2 prints what it read from examples/greeting.txt, which it finds only + // when it runs in its own directory. + greeting := "Hello, " + strings.Trim(program.local, `"`) + "!" + if output := strings.Join(p.debug.output, "\n"); !strings.Contains(output, greeting) { + t.Fatalf("the console does not show %q: %q", greeting, output) } // The ended session closes Delve's connection and asks Delve to stop within the stop - // time; Delve then deletes the program it built, so nothing is left in the module. + // time; Delve then deletes the program it built in the package's directory. awaitStateWithin(t, h, 30*time.Second, "Delve to remove its debug binary", func() bool { - return len(debugBinaries(t, root)) == 0 + return len(debugBinaries(t, filepath.Dir(source))) == 0 }) - - h.app.closeProject() - awaitStateWithin(t, h, 30*time.Second, "the closed project", func() bool { return h.app.project == nil }) - h.settle() - assertPackageUnchanged(t, installed, before) } diff --git a/e2e/host/lithe-host.json b/e2e/host/lithe-host.json index 349ffae..d1463e1 100644 --- a/e2e/host/lithe-host.json +++ b/e2e/host/lithe-host.json @@ -3,7 +3,7 @@ "revision": "a9bddd922e28b4f4dcda4e92b581cbbe7b0f4eb7", "packages": { "./internal/projectgen": "^TestGoPlugin", - "./internal/workbench": "^TestOfficialGoPlugin(BuildsAndServesAModule|DebugsAModuleWithDelve)$" + "./internal/workbench": "^TestOfficialGoPlugin(BuildsAndServesAModule|DebugsEachProjectOfAWorkspaceWithDelve)$" }, "requiredEnvironment": { "LITHE_TEST_GO_PLUGIN_ARCHIVE": "go-support--darwin.zip to install, for example dist/go-support-1.0.0-darwin.zip" diff --git a/io.github.lithe-idea.go-support/plugin.json b/io.github.lithe-idea.go-support/plugin.json index 25a2d4e..4bb1efa 100644 --- a/io.github.lithe-idea.go-support/plugin.json +++ b/io.github.lithe-idea.go-support/plugin.json @@ -432,13 +432,19 @@ "configurations": { "launch": { "arguments": { - "mode": "debug" + "mode": "debug", + "program": ".", + "env": { + "GO111MODULE": "auto" + } }, "targetBindings": { - "program": "target.directory", - "cwd": "target.workingDirectory", + "cwd": "target.directory", "args": "target.arguments", - "env": "target.environment" + "adapterArguments": { + "dlvCwd": "target.directory", + "env.PWD": "target.directory" + } } } },