当前工作空间中未找到任何模块;(在VScode中调试.go文件)

huangapple go评论157阅读模式
英文:

no modules were found in the current workspace; (debugging a .go file in VScode)

问题

我正在尝试使用Go v1.20.3在VSCode中调试一个.go文件。
在VSCode的左侧面板中,点击调试图标 > 点击齿轮图标以创建一个配置文件(launch.json)。

我的launch.json文件如下所示(自动生成):

"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
// "program": "${fileDirname}"
"program": "${workspaceRoot}"

我将原来的"program": "${fileDirname}"更改为"program": "${workspaceRoot}",希望其中一个能起作用。

当我按下F5(运行调试器)时,在调试控制台(终端部分)中,我得到以下错误信息:

Starting: C:\Users\Shawn\go\bin\dlv.exe dap --listen=127.0.0.1:60824 from C:\Users\Shawn\go\vsCodeExam.vscode
DAP server listening at: 127.0.0.1:60824
Build Error: go build -o C:\Users\Shawn\go\vsCodeExam.vscode__debug_bin.exe -gcflags all=-N -l .
go: no modules were found in the current workspace; see 'go help work' (exit status 1)

我每次都注释掉了("program":...),分别运行了两个("program":...),但无论("program":...)的值是什么,我都得到了上面显示的错误。

我仍在阅读有关在VSCode中调试的更多信息,但没有指出缺少哪些模块?
除了模块之外,还可能有什么问题?

英文:

I am trying to debug a .go file in VSCode with Go v1.20.3.
In the left panel of vscode, clicked on Debug icon > click on the Gear Icon to create a configuration file.(launch.json)

My launch.json file shown below.(created automatically)

"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
// "program": "${fileDirname}"
"program": "${workspaceRoot}"

I changed the original "program": "${fileDirname}" to "program": "${workspaceRoot}" hoping one of them would work.

when I do F5 (running debugger), in the Debug Console (Terminal section), I get the following error:

Starting: C:\Users\Shawn\go\bin\dlv.exe dap --listen=127.0.0.1:60824 from C:\Users\Shawn\go\vsCodeExam.vscode
DAP server listening at: 127.0.0.1:60824
Build Error: go build -o C:\Users\Shawn\go\vsCodeExam.vscode__debug_bin.exe -gcflags all=-N -l .
go: no modules were found in the current workspace; see 'go help work' (exit status 1)

I commented out both of the ("program":...) each time.(meaning running debug with both ("program":...) separately but NO luck no matter what the value of ("program":...) is, I get the error shown above.

I am still reading more about debugging in vscode but nothing points out which modules are missing?
or what the problem might be besides the modules.

答案1

得分: 1

在孤立地调试一个文件是不可能的;需要有某种可执行文件来调用被调试的代码。这可以是一个测试文件,也可以是代码所属的程序(实际程序或某种测试框架)。

所以,“调试一个.go文件”取决于你的具体意思。

如果该文件是测试的一部分,那么你根本不需要启动文件;你可以使用VS Code的Go扩展提供的go test集成来调试(测试)文件中的测试或单个测试。

如果该文件(代码)没有被任何测试覆盖,那么你将需要一个启动文件,该文件标识一个包含main()函数的模块文件夹,该函数会执行你要调试的代码。

根据我的经验,编写测试是以一种最简单的方式进行go代码的临时、交互式调试(同样,编写测试意味着不必经常使用交互式调试!)。

英文:

It is not possible to "debug a file" in isolation; there needs to be some sort of executable that calls the code being debugged. That is either a test or a program that the code is a part of (either an actual program or some form of test harness).

So a lot hinges on what you mean by "debug a .go file".

If that file is part of a test then you don't need a launch file at all; you can use the go test integration provided with the Go extension for VS Code to debug the tests in the (test) file or individual tests.

If the (code in the) file is not covered by any test then you will need a launch file that identifies a folder that is a module with a main() func that exercises the code you are trying to debug.

It is my experience that having tests is the easiest way to perform ad-hoc, interactive debugging of go code (and equally, having tests means having to resort to interactive debugging less often!)

huangapple
  • 本文由 发表于 2023年6月23日 03:16:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76535039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定