microsoft / vscode-cpptools

Official repository for the Microsoft C/C++ extension for VS Code.
Other
5.4k stars 1.51k forks source link

Not able to setup environment variables before the debugger is launched #10618

Open Ampaex opened 1 year ago

Ampaex commented 1 year ago

Environment

Bug Summary and Steps to Reproduce

Bug Summary: I'm trying to run a custom gdb(qnx neutrino - ntoaarch64-gdb), but for that, I need to set up some environment variables before. I've tried to use the "environment" field, the "envFile" approach, using "terminal.integrated.env.linux", doing a "preLaunchTask" and many other things but I'm not able to set them before the call of the binary. Every try results in gdb being executed without the environment variables. I currently have an environment script(.sh) for setting up all the variables.

Steps to reproduce:

  1. I'm trying to run the launch gdb that needs some environment variables to be set.
  2. Try to add the envFile, environment field or any other approach.
  3. Launch the debugger
  4. See that the gdb debugger fails because variables are not set.

Could you help me with this issue? Thanks

Debugger Configurations

{
          "name": "Launch debug (GDB)",
          "type": "cppdbg",
          "request": "launch",
          "miDebuggerPath": "/path/to/ntoaarch64-gdb",
          "program": "/path/to/binary",
          "stopAtEntry": false,
          "cwd": "${workspaceFolder}",
          "miDebuggerServerAddress": "(ip):(port)",
          "externalConsole": true,
          "MIMode": "gdb",
          "setupCommands": [
            {
                "description": "Enable pretty-printing for gdb",
                "text": "-enable-pretty-printing",
                "ignoreFailures": true
            }
          ],
          "envFile": "${workspaceFolder}/.env",
          "environment": [
              {
                "name":"ENVVAR",
                "value": "/some/path/"
              }
           ]
        }

Debugger Logs

¡QNX environment is not set!

Other Extensions

I'm using the extensions for C/C++.

Additional Information

No response

pawelszramowski commented 1 year ago

I have also been struggling with this trying to make my ~/.gdbinit work.

I observed that the MI debugger is started with the current working directory set to the directory containing the MI debugger executable (so the CWD would be /path/to/ for "miDebuggerPath": "/path/to/ntoaarch64-gdb"), and with the original environment variables. The values from "cwd" and "environment" are only applied later, after ~/.gdbinit has been executed.

Ampaex commented 1 year ago

Please, any help with this? Could you indicate us how to add environment variables before the call of the debugger placed in "miDebuggerPath"?

pawelszramowski commented 1 year ago

If the required environment variables do not depend on your VScode workspace then you could create an adapter script that would set the variables and call GDB, passing through to it all the command-line arguments. Untested example:

Ampaex commented 1 year ago

Yes, could be an option, but in my case is not so useful because has to be run in multiple machines. I not a good idea to be modifying the installation on each of them

pawelszramowski commented 1 year ago

Right, the location of the script doesn't really matter, it may (and probably should) be versioned within the workspace. I'd then just make sure to change the current working directory to /path/to/ before calling ntoaarch64-gdb, just like VSCode does.