vscode icon indicating copy to clipboard operation
vscode copied to clipboard

Support envfile for tasks.json

Open felixfbecker opened this issue 8 years ago • 53 comments

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

felixfbecker avatar Oct 23 '17 18:10 felixfbecker

Can you provide me with an example on how this should look like. Why isn't this a normal file

dbaeumer avatar Oct 24 '17 08:10 dbaeumer

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"?

felixfbecker avatar Oct 24 '17 16:10 felixfbecker

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.

dbaeumer avatar Oct 25 '17 08:10 dbaeumer

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.

felixfbecker avatar Oct 25 '17 15:10 felixfbecker

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}?

dbaeumer avatar Oct 26 '17 08:10 dbaeumer

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.

felixfbecker avatar Oct 27 '17 08:10 felixfbecker

Now I got it.

dbaeumer avatar Oct 27 '17 08:10 dbaeumer

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

felixfbecker avatar Nov 28 '17 08:11 felixfbecker

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?

myrup avatar Dec 07 '17 23:12 myrup

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.

acdha avatar Dec 29 '17 15:12 acdha

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...

felixhayashi avatar Apr 13 '18 14:04 felixhayashi

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.

petermetz avatar Apr 27 '18 19:04 petermetz

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.

cdarwin avatar Aug 24 '18 16:08 cdarwin

Is this issue still actual?

gost-serb avatar Feb 12 '19 10:02 gost-serb

Would like to have this too, in the mean time. use $(cat .env | xargs) | <your command>

doomleika avatar Feb 22 '19 08:02 doomleika

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

ekkis avatar May 23 '19 22:05 ekkis

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

ekkis avatar May 23 '19 22:05 ekkis

Need .env file support in tasks.

  1. Keeps secrets out of git
  2. Define my env vars in one place
  3. It is a standard workflow for multiple languages to use .env file

Here is another use case:

  • I was hoping to recreate this Makefile (link) in tasks.json to prevent having to install make on Windows
  • However I need to pass build args from my .env file (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>.yml files in the meantime
  • But: docker-compose doesn't support BuildKit yet, 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 into tasks.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.

adriantorrie avatar May 24 '19 01:05 adriantorrie

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.

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

ekkis avatar Jun 16 '19 00:06 ekkis

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.

warnes avatar Sep 10 '19 19:09 warnes

Hi is there any progress on this request? I need also to define some .env variables locally to be used by tasks.json

ayoubserti avatar Feb 05 '20 04:02 ayoubserti

This seems to going in a similar direction to #89825

dellagustin-sap avatar Mar 17 '20 14:03 dellagustin-sap

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.

alexandrulesi avatar Aug 06 '20 11:08 alexandrulesi

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

mpelnens avatar Sep 06 '20 17:09 mpelnens

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.

pcherna avatar Oct 05 '20 17:10 pcherna

Any progress on this? Seems like a fairly necessary feature for tasks.

zkldi avatar Apr 24 '21 19:04 zkldi

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.

joundso avatar Aug 05 '21 12:08 joundso

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

fredjeck avatar Feb 10 '22 14:02 fredjeck

+1

gustavorps avatar May 04 '22 14:05 gustavorps

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.

Materie avatar Sep 21 '22 06:09 Materie