codelldb icon indicating copy to clipboard operation
codelldb copied to clipboard

Support for env files

Open MOZGIII opened this issue 6 years ago • 5 comments

I'd like to have a way to pass env file to read the environment variables from, instead of specifying the variables in the .vscode/launch.json.

MOZGIII avatar May 16 '19 00:05 MOZGIII

When using "native" adapter, you can modify launch environment of the future debuggee process from inside of the "preRunCommands" hook. So you could do something like this:

"preRunCommands": "command script import ${workspaceRoot}/prelaunch.py"

and then in prelaunch.py:

import lldb
launch_info = lldb.target.GetLaunchInfo()
launch_info.SetEnvironmentEntries(['FOO=foo','BAR=bar'], True)
lldb.target.SetLaunchInfo(launch_info)

See also: https://lldb.llvm.org/python_reference/lldb.SBLaunchInfo-class.html

vadimcn avatar May 16 '19 01:05 vadimcn

Nice workaround, I'll give it a try later today!

Still I'd prefer to have an simpler config for this, as it seems to me like a wide spread approach to manage env vars and it worth providing a built-in solution.

Additional context for my use case: I'd like to commit the .vscode/launch.json into a repo, but for it to not include env vars for various reasons (security concerns, portability of a setup and the ability to change the variable values without changing the launch.json itself, so that they'not picked up by git every time). I'd typically put my env vars I'm using during development in a .env file (that's git-ignored), and run the commans in a shell that has those variables loaded. If I could import env vars from a file or run a helper wrapper-script that would provision the env vars for the debugger (I'm thinking of an equivalent of a simple bash script the exports a bunch of stuff and then does exec "$@", on the contrary to using lldb API via python) - it'd be much more straightforward, and I'd greatly apprecite it!

MOZGIII avatar May 16 '19 01:05 MOZGIII

You could have the Python script reading that .env file, rather than hardcoding values into the script itself.
I'd rather avoid adding an option for every little thing someone might want to do, if there are ways to accomplish the same thing with a bit of scripting.

vadimcn avatar May 16 '19 20:05 vadimcn

You could have the Python script reading that .env file, rather than hardcoding values into the script itself.

Yes, I figured. I'd have carry this script around, plus the dependency to parse .env files. Even though I could just put all that into the .vscode dir - it's not cool to carry around those dependencies around for every project. Seems like a lot of boilerplate to begin with, and no way to efficiently reuse and version control those scripts across multiple projects.

I'd rather avoid adding an option for every little thing someone might want to do, if there are ways to accomplish the same thing with a bit of scripting.

I don't agree it's a little thing. It's a major feature that every other debugger plugin I've used offers already. I face the need for it not in one particular project, but in pretty much everywhere, where the configuration is done via env vars, and this is a very common pattern nowadays. The argument is also not consistent - why even have env config var there? It can be scripted too.

MOZGIII avatar May 17 '19 02:05 MOZGIII

Was also really missing this feature, so here is how you can do this with Python:

import os
import lldb

# Read the .env file and store the key-value pairs in a array with format ["key=value"]
env_array = []
with open(os.path.join(".env")) as f:
    for line in f:
        env_array.append(line.strip())

target = lldb.debugger.GetSelectedTarget()

launch_info = target.GetLaunchInfo()
launch_info.SetEnvironmentEntries(env_array, True)
target.SetLaunchInfo(launch_info)

And save it somewhere you like. Then, in your launch.json add the following line with the correct file path

"preRunCommands": ["command script import ${workspaceFolder}/.vscode/prelaunch.py"],

JesseVelden avatar Dec 11 '22 16:12 JesseVelden