在使用VSCode调试Golang文件时出现了`cannot load plugin`错误。

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

`cannot load plugin` when debugging golang file in VSCode

问题

我的程序在Ubuntu上运行时首先需要一个插件:

go build -race -buildmode=plugin ../mrapps/wc.go

当我创建了launch.json并尝试在VSCode中调试一个文件时:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch mrworker",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "src/main/mrworker.go",
            "args": ["wc.so"]
        }
    ]
}

控制台显示cannot load plugin wc.so

Starting: /home/lzq/go/bin/dlv-dap dap --check-go-version=false --listen=127.0.0.1:44503 --log-dest=3 from /home/lzq/Document/MapReduce/6.824/src/main
DAP server listening at: 127.0.0.1:44503
Type 'dlv help' for list of commands.
2022/01/09 22:38:32 cannot load plugin wc.so
Process 17678 has exited with status 1
Detaching
dlv dap (17594) exited with code: 0

当我使用dlv查找更多信息时,错误是plugin was built with a different version of package

我阅读了很多帖子,但没有一个有效。我该如何解决这个问题?

英文:

My program first needs a plugin when running in Ubuntu:

go build -race -buildmode=plugin ../mrapps/wc.go

When I made launch.json and tried to debug a file in VSCode:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch mrworker",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "src/main/mrworker.go",
            "args": ["wc.so"]
        }
    ]
}

The console showed cannot load plugin wc.so:

Starting: /home/lzq/go/bin/dlv-dap dap --check-go-version=false --listen=127.0.0.1:44503 --log-dest=3 from /home/lzq/Document/MapReduce/6.824/src/main
DAP server listening at: 127.0.0.1:44503
Type 'dlv help' for list of commands.
2022/01/09 22:38:32 cannot load plugin wc.so
Process 17678 has exited with status 1
Detaching
dlv dap (17594) exited with code: 0

When I used dlv to find more information, the error is plugin was built with a different version of package.

I read many posts but none of them works. How can I fix this problem?

答案1

得分: 1

根据文档

目前,插件仅在Linux、FreeBSD和macOS上受支持,正如Golang的最新版本中所提到的。
可执行文件和插件对象之间的依赖关系并不完全匹配,这就是为什么插件加载器会抛出上述错误的原因。

Go使用最小版本选择。很难固定传递依赖关系,因为Go会选择它认为满足插件代码项目中的依赖树的最小版本,而没有任何方法明确指出还有另一个依赖树需要考虑。

有很多解决此问题的方法:

参考此链接:https://forum.heroiclabs.com/t/plugin-was-built-with-a-different-version-of-package/556/5,https://github.com/golang/go/issues/31354

或者

您可以尝试使用-gcflags "all=-N -l"标志构建插件。

为了更好地理解,请参考:https://youtrack.jetbrains.com/issue/GO-6288

您还可以尝试在go.mod文件中使用replace关键字
示例:

replace github.com/zimnx/central => ../central
英文:

As per Documentation :

Currently plugins are only supported on Linux, FreeBSD, and macOS as mentioned in latest version of Golang.
Dependencies between exe binary and plugin object do not match exactly that's why plugin loader is throwing the above error.

Go uses minimal version selection. It’s quite hard to pin transitive dependencies because Go will select the minimal version that it thinks satisfies the dependency tree in just your plugin code project without any way to make it clear that there’s another dependency tree to consider.

There are plenty of workaround to deal with this issue :

Refer this link : https://forum.heroiclabs.com/t/plugin-was-built-with-a-different-version-of-package/556/5 , https://github.com/golang/go/issues/31354

OR

You can try to build plugin with -gcflags "all=-N -l" flag.

For better understanding refer this : https://youtrack.jetbrains.com/issue/GO-6288

Also you can try with replace in go.mod file
example :

replace github.com/zimnx/central => ../central

答案2

得分: 1

从dlv的github issues中添加一个参考文献

对应的解决方案是:

> Delve将-gcflags="all=-N -l"传递给go build,因为插件的工作方式需要插件也以这种方式构建。

英文:

Add a reference from the dlv github issues

And the corresponding solution:

> Delve passes -gcflags="all=-N -l" to go build, because of how plugin
> works the plugin needs to also be built like that.

huangapple
  • 本文由 发表于 2022年1月9日 23:12:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/70642618.html
匿名

发表评论

匿名网友

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

确定