vscode
vscode copied to clipboard
Support envfile for tasks.json
The node and Go debuggers both support specifying an env file for environment variables instead of the env hash. It would be great if we could use the same file in tasks.json to have a single source of truth that can also be used by non-vscode scripts
Can you provide me with an example on how this should look like. Why isn't this a normal file
It is just a key/value file:
FOO=hello
BAR=world
It can be used with the Node debugger as envFile: https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_launch-configuration-attributes
And it can be easily read into e.g. a bash script too.
Not sure what you mean with "Why isn't it a normal file"?
Not sure what you mean with "Why isn't it a normal file"?
If it is a simple file what is preventing you from using it in a tasks.json file.
If you mean why am I not copying the settings over, it is to have a single source of truth. It's the same justification why node debugger and the Go debugger added this config option. tasks.json is not understood by other tools and not every dev uses VS Code, but an env file can be read in easily by a shell script.
I am still puzzled with this. All tasks does is calling other programs. Could the task description not simple pass the right argument (the envfile) to the program it starts. Or do you want tasks to have a setting to read envs vars from a file when you do variable substitution like ${env:xxxx}?
A lot of commands that are candidates to be run through the task system are configured through environment variables - which is why the tasks.json env option is great.
I can write a tasks.json that has a bunch of tasks that are interconnected with dependsOn and have individual problem matchers and they receive the same env vars.
But not everyone uses VS Code, so you will want to provide a way to run these tasks without VS Code, e.g. from a bash script that runs all these commands and sets the same env vars. You want to avoid fragmenting all these env vars between the tasks.json and the script.
Of course, you could now configure your tasks.json to run just a single bash script instead of all the individual commands. But then you lose the ability to specify a problem matcher per task, run them individually etc.
So it would be great to be able to point tasks.json to an external file that contains env vars as simple key=value lines and have the script also read in that external file to have a single source of truth.
Now I got it.
Another reason for this if your application needs secret environment variables like tokens - you can set them in a .gitignored envfile, but commit the tasks.json
I'm missing the envFile as an option for a .Net Core Launch configuration (for .gitignore reasons). Shouldn't it be available for all configurations and tasks that start processes?
I just ran into this with a Python app. After #14523 the stock debugger configs automatically reuse the configuration that my external (pipenv) tooling uses because they have envFile=.env in the config. It'd be really nice if that was supported in tasks as well.
I agree with @myrup, the .env constants should be equally accessible from within launch.json – not just tasks.json. I saw there was some similar discussion here in https://github.com/Microsoft/vscode/issues/14523 resulting in an envFile property but it doesn't seem to have been implemented...
Another use-case why this would be great: You want to store the tasks.json file under version control, but not your environment variables which may contain sensitive data. Without the envFile property there is no easy way to separate the two.
For the time being I am working around this by adding a prefix to my task command which essentially sources the .env file used by launch.json.
Is this issue still actual?
Would like to have this too, in the mean time. use $(cat .env | xargs) | <your command>
I support this request. having all my environment variables in a file and being able to run tasks with those values available would be very valuable
in fact, I'll go further, having the "env" key in options makes no sense at all. it means I have to hard-code all the environment variables I need into a VSCode configuration file?? that's a terrible approach
Need .env file support in tasks.
- Keeps secrets out of git
- Define my env vars in one place
- It is a standard workflow for multiple languages to use
.envfile
Here is another use case:
- I was hoping to recreate this
Makefile(link) intasks.jsonto prevent having to install make on Windows - However I need to pass build args from my
.envfile (The docker extension for VSCode isn't flexible enough for this approach) - Because I cant do the above: I now have to write separate
docker-compose.<make step>.ymlfiles in the meantime - But:
docker-composedoesn't supportBuildKityet, which means I can't bind mount local volumes into the build scope, which would allow me to export unit test results from the images at build time (also don't get the parallel stage builds either) - Some of the runtime arguments are secrets (i.e.
docker run -e password=$PASSWORD), so there is no way they are being hard-coded intotasks.json - So I don't have any means to a fully-functional multi-stage build with build-time bind mounts
The above workflow example in the Makefile will be reused in our CI pipeline.
- Essentially allows our developers to run locally what will be run in the CI pipeline
- This gives us a CI pipeline agnostic approach that prevents vendor lock-in
So the only thing we're missing at the moment is being able to give devs a clean workflow on their local computers.
** /s ** Or we tell them them to maintain both .env and tasks.json themselves and keep them out of git, and that we expect them to do this for the 50+ microservices we have built in the past 8 months, and to keep doing it for the additional ones in the future.
For the time being I am working around this by adding a prefix to my task command which essentially sources the
.envfile used bylaunch.json.
how do you do it? my .env file doesn't include the "export" keyword and thus . .env doesn't work as child processes can't see the variables. I also don't see any parameters to the source command to automatically export
A nice secondary feature would be to include an overwrite:True argument envFile to control whether the settings in the file overwrite existing environment variable values.
Hi is there any progress on this request?
I need also to define some .env variables locally to be used by tasks.json
This seems to going in a similar direction to #89825
I'd also be interested in seeing this.
This seems to going in a similar direction to #89825
The solution suggested in the other ticket is to add inputs to tasks.json, but what we are ultimately looking for is a way to run the debugger and only update all environment variables in .env, and not in multiple places.
This would be extremely useful in cases where environment variables contain sensitive data like tokens or passwords. With 'envFile' support the .env file could be added to .gitignore
In light testing, the following seems to be an OK workaround for Linux/Mac. Create a .env file with your local config and secrets, e.g.:
[email protected]
FOO_PASSWORD=SecretDoNotShare
Then in your tasks.json, where before you had this bad solution:
...
"command": "myfoo [email protected]:SecretDoNotShare ..."
...
you can now say:
...
"command": "export $(xargs < .env) && myfoo $FOO_USERNAME:$FOO_PASSWORD ..."
...
and now tasks.json is separated from your .env config, and can be safely committed to source control.
Any progress on this? Seems like a fairly necessary feature for tasks.
Aren't they implemented now?
--> "envFiles": ["../secret.env"]
At least something like this is working in the tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "docker-build",
"label": "docker-build",
"platform": "python",
"dockerBuild": {
"tag": "rapid:latest",
"dockerfile": "${workspaceFolder}/Dockerfile",
"context": "${workspaceFolder}",
"pull": true
}
},
{
"type": "docker-run",
"label": "docker-run: debug",
"dependsOn": ["docker-build"],
"dockerRun": {
"containerName": "YOUR_IMAGE_NAME",
"image": "YOUR_IMAGE_NAME:YOUR_IMAGE_TAG",
"env": {
"KEY1": "VAL1",
"KEY2": "VAL2"
},
"volumes": [
{
"containerPath": "/app/data/out",
"localPath": "${workspaceFolder}/data/out"
}
],
"envFiles": ["../secret.env"],
"ports": [
{
"containerPort": 5000,
"hostPort": 5000
}
]
},
"python": {
"file": "main.py"
}
}
]
}
This leads to a docker run --env-file "../secret.env" beeing executed.
I would also love to have this feature implemented. Currently handling multiple services which are relying on API keys, or certificates passwords which need to be passed to the docker build and debug tasks as env variables. I want to keep those out of Git and I want that each developer has its own local configuration. It would be much cleaner if this configuration can be defined in a local env file rather than having to put those infos into env scoped environment variables
+1
Still very much needed for the case where you want to not commit the env var data, but do want to commit the task itself.
I would like to have a task everyone in the team can reuse, then let them tweak the actual URIs where the task would run at by setting the env vars to locations on their machine which would be different for each developer.