microsoft / vscode

Visual Studio Code
https://code.visualstudio.com
MIT License
164.45k stars 29.35k forks source link

Environment variables disappear when the program forks when debugging a C++ program #234002

Open Ambiophonique opened 2 hours ago

Ambiophonique commented 2 hours ago

Does this issue occur when all extensions are disabled?: Yes

VScode gets rid of environment variables when I debug a C++ programs which forks.

Here is an example with a 2 files project :

Here is my launch.json

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "(gdb) Launch",
      "type": "cppdbg",
      "request": "launch",
      "program": "/home/user/MyProject/hello1",
      "args": [],
      "stopAtEntry": false,
      "cwd": "${fileDirname}",
      "environment": [
        {"name": "FOO", "value": "foo"}
      ],
      "externalConsole": false,
      "MIMode": "gdb",
      "setupCommands": [
          {
              "description": "Enable pretty-printing for gdb",
              "text": "-enable-pretty-printing",
              "ignoreFailures": true
          },
          {
              "description": "Set Disassembly Flavor to Intel",
              "text": "-gdb-set disassembly-flavor intel",
              "ignoreFailures": true
          }
      ]
    }
  ]
}

hello1.cpp

#include <boost/process.hpp>

namespace ps = boost::process;

int main() {
  std::string command = "/home/user/MyProject/hello2";
  ps::child c(command);

  c.wait();

  return 0;
}

hello2.cpp

#include <iostream>

int main() {
  std::cout << "Hello world" << std::endl;

  const char* myVarEnv = std::getenv("FOO");
  std::cout << "myVarEnv = " << myVarEnv << std::endl;

  return 0;
}

to build hello1.cpp and hello2.cpp :

g++ -g hello1.cpp -o hello1 -lpthread
g++ -g hello2.cpp -o hello2

What is astonishing is when I debug hello1 I can see my FOO environment variable is correctly defined :

-exec show environment FOO
FOO = foo

Then I switch the follow-fork-mode to child :

-exec set follow-fork-mode child

But when I fork in hello2, gdb can't find the FOO environment variable anymore :

-exec show environment FOO
Environment variable "FOO" not defined.

but oddly enough, the program hello2 manages to print the environment variable correctly :

myVarEnv = foo

Moreover, if I debug this program with gdb, gdb can find the FOO environment variable perfectly in both hello1 and hello2 :

(gdb) show environment FOO     
FOO = foo

Since, VScode just uses gdb, that means VScode should be able to find the FOO environment normally, right ?

vs-code-engineering[bot] commented 2 hours ago

Thanks for creating this issue! It looks like you may be using an old version of VS Code, the latest stable release is 1.95.3. Please try upgrading to the latest version and checking whether this issue remains.

Happy Coding!

Ambiophonique commented 2 hours ago

I tried with the latest version and the bug is still here