1

I want to setup VScode so I can use build tasks to build my project. The project is build using make, en I have defined a build task that runs make. However, before running make, I normally would source a script that sets my environment variables correctly. If I add a new build task with the source command, and set my main build tasks to first execute the source command, the environment variables are not propagated properly. How can I make sure the enviroment variables are kept between build tasks?

My tasks.json file:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558 
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "make",
            "command": "make",
            "args": [],
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "dependsOn": "Set environment"
        },
        {
            "label": "Set environment",
            "type": "shell",
            "command": "source path/to/source_me.sh",
            "group": "build",
   ]
}
tmnvnbl
  • 61
  • 8

2 Answers2

1

Not in this way. Sourcing a file is injecting file contents into current shell session, which ends as soon as the task ends. The make task is run in a separate shell session, so these two do not interact. You may try to do a single task that executes a single line: source path/to/source_me.sh && make.

raspy
  • 3,995
  • 1
  • 14
  • 18
0

I solved my problem like this:

"tasks": [
       {
           "label": "all",
           "type": "shell",
           **"command": "source gitenv.sh && cd ${fileDirname} && alfred all"**,
           "problemMatcher": [],
           "group": {
               "kind": "build",
               "isDefault": true
           }
       }
   ]

Where alfred is a macro for make command with extra args. ${fileDirname} returns a current directory in which you have open file.

So if you open a file and in the same directory you have Makefile you can CTRL + SHIFT + B to execute this default task.

benson23
  • 16,369
  • 9
  • 19
  • 38
Robin
  • 1