Sometimes I get a popup saying that it failed to launch GDB. I should mention that uploading via STLink does not work either. I assume that PIO uses a local GDB executable and doesnt rely on a system installation?

I deleted upload_protocol from the config (so its on the default value which is stlink)

I then tried to upload and got a connection error, so I tried again to upload but this time I held down the physical RESET button on the board during the upload - this gave me a new error:

Error: timed out while waiting for target halted
TARGET: stm32l4x.cpu - Not halted
in procedure ‘program’

embedded:startup.tcl:480: Error: ** Unable to reset target **

It seemed to actually connect, but it could not reset the device, so I tried a third time (I did not hold down the reset button), and the upload worked fine. I then launched the debug, and it worked perfectly.