IsraelCena
IsraelCena

Reputation: 49

git commit with gpg key does not work from VSCode

Added a gpg key to sign commits.

After all the configuration is done, I am able to sign via git cli.

But when I commit using vscode it opens a terminal window so I can enter my key password. The window appears a little buggy and does not accept the password and returns the error in vscode:

error: gpg failed to sign the data

When I restart gpg-agent and manage to commit via cli.

When I type the password through the cli, it is stored in that section of the terminal and I manage to commit through vscode. When you close the section, the error returns.

I'm using wsl2 with linux Ubuntu 20

Upvotes: 1

Views: 3888

Answers (3)

9662e103-129a
9662e103-129a

Reputation: 75

Also, when using gitconfig files to enable GPG signing, make sure to use the long key from when you created your gpg key i.e.:

[user]
email = [email protected]
name = My Name
signingkey = ########################################
[gpg]
    program = gpg
[commit]
    gpgsign = true

Which should work if you get errors like not being able to sign because a private key does not exist.

If you want to find out these credentials, go to git bash and hit the gpg --list-secret-keys --keyid-format=long command which should output something along the lines of:

sec   rsa4096/SHORT_KEY_ID yyyy-mm-dd [SC] [expires: yyyy-mm-dd]
  LONG_KEY_ID
uid                 [ultimate] My Name (comment) <[email protected]>
ssb   rsa4096/SHORT_KEY_ID yyyy-mm-dd [E] [expires: yyyy-mm-dd]

You will want to use the LONG_KEY_ID for the signing key!

Hope that helps.

Signed, 9662e103-129a

Upvotes: 0

IsraelCena
IsraelCena

Reputation: 49

I found a solution...

Steps required:

Install GPG4Win from https://www.gpg4win.org. Nothing other than the default gnupg is required, but I installed Kleopatra too in case it came in handy elsewhere.

Edit ~/.gnupg/gpg-agent.conf and change the pinentry

pinentry-program "/mnt/c/Program Files (x86)/GnuPG/bin/pinentry-basic.exe"

Upvotes: 0

LeGEC
LeGEC

Reputation: 51780

I guess your issue is :

  • you started VSCode from an environment where the gpg-agent wasn't started, and hence VSCode doesn't have access to the environment variables that indicate how to access the gpg-agent
  • when you start the gpg-agent from a terminal opened in VSCode, it updates the environment for that terminal (each terminal is a separate process) but not for the IDE or other terminals

To confirm that having the correct environment works, you can try the following :

  • close all running instances of VSCode
  • open a fresh terminal (whatever shell you use: bash, powershell, cmd.exe ...)
  • from that terminal, start your gpg-agent
  • from that same terminal, launch VSCode from the command line
  • check if you still have issues committing from that instance of VSCode

You can check the environment before and after starting your gpg-agent to confirm that variables do get updated (in bash, this simply means running env or env | sort from the command line).

Upvotes: 0

Related Questions