最近将开发环境都搬到了 wsl 上,配了一下 gpg,一开始 vscode commit 报错,然后手动在终端测试了一下 gpg 就正常了,后来重启了问题复现,研究了一下,顺便记录一下解决方法。
vscode 的报错:
error: gpg failed to sign the data
fatal: failed to write commit object
解决方法:
-
首先需要确保你的 wsl 版本是 2,因为我这个解决方案是用到了 wslg 的。 可以输入
wsl --list -v
查看版本。 如果 VERSION 是 1,可以运行wsl --set-version <distro name> 2
参考微软文档 -
sudo apt install kleopatra scdaemon
-
sudo bash -c 'dbus-uuidgen > /var/lib/dbus/machine-id'
然后你可以尝试启动kleopatra
,如果出现报错类似👇kf.dbusaddons: DBus session bus not found. To circumvent this problem try the following command (with bash): export $(dbus-launch) 请运行
export $(dbus-launch) && echo -e '\nexport $(dbus-launch)' >> ~/.bashrc
tips:如果你是其他 shell 请自行添加export $(dbus-launch)
到你的启动脚本
好了,到这里你的 vscode 应该可以使用 gpg 签名了。
如果你还是有问题,可以尝试在 ~/.gnupg/gpg-agent.conf
里添加 pinentry-program /usr/bin/pinentry
。
然后执行 gpg-connect-agent reloadagent /bye
然后这里是一些探索过程:
首先为什么 vscode 会报错呢? 因为 git 报错了。 那为什么 git 报错了呢。 因为 git 没办法调用 gpg 默认的 cli 让你输密码,如果你是在 shell 里运行的git 你应该会看到一个密码框,但是 vscode 的 git 不一样,它没有终端给它用。 那么我们就需要找一个 gui 程序给 gpg 输密码。 一开始我找到了这篇文章。
https://stackoverflow.com/questions/63440623/no-gpg-passphrase-prompt-in-visual-studio-code-on-windows-10-for-signed-git-comm
有一个解决方法是调用 Windows 的 pinentry.exe 我尝试过了,但是这对我来说没用。 经过一些排查之后,我发现在 shell 里也没办法调用 gpg 了。 gpg报错👇
gpg: 签名时失败: 缺少 pinentry
gpg: [stdin]: clear-sign failed: 缺少 pinentry
这就奇怪了,然后我去手动 call pinentry.exe。 然后报错👇
<3>WSL (1344) ERROR: UtilAcceptVsock:246: accept4 failed 110
搜索之后发现,在 2022 年 8 月 2 日 GitHub 就有了相关 issue。
简单来说就是微软写 bug 了,子系统叫不起来 Windows 上的程序了。
我当时就
然后想起来我原来在 Windows 上用的 Kleopatra 也支持 Linux,一番尝试之后,就有了本文。