Can I reference variables from the active configuration environment in tasks.vs.json?

CR 136 Reputation points
2020-10-29T20:19:20.327+00:00

Hello,
I am new to Visual Studio and am trying to set up an "Open folder" development and created the appropriate CppProperties.json and tasks.vs.json files with their configurations, environments and tasks.

I am using tasks.vs.json to define tasks to run a build script. This works, but I have one secondary question here:
I have multiple tasks that are almost exactly the same. The only thing that differs between the tasks is the "taskLabel" and one entry in "args". The relevant parts are:

  "tasks": [
    {
      // some properties that are the same for all tasks ...
      "taskLabel": "Build",    // different between tasks
      "args": [
        "${env.SHELL_ARGS}",
        "build"     // different between tasks
      ]
    },
    {
      // repeat of all the identical properties ...
      "taskLabel": "Install",    // different between tasks
      "args": [
        "${env.SHELL_ARGS}",
        "install"    // different between tasks
      ]
    }
  ]

Looking at the reference, I don't see a way to define some "base" task and just take that, edit the few entries that differ, and use the result as a new task. I guess I am stuck having to repeat most of the fields in every task?

To my main question - I am working with two different toolchains, but all the tasks I defined work for both of them. The only different thing are the values of the environment variables, e.g. ${env.SHELL_ARGS} as I used above. Is there any way to refer to these ${env. ...} variables only from the current active configuration (i.e. the one selected in the VS toolbar)? In CppProperties.json, according to the reference I can define per-configuration environments, but this does not seem to work as expected.

E.g. I have something like

"configurations": [
  {
    "inheritEnvironments": [ "env_A" ],
    "name": "A",
    // other properties ..
    "environments": [
      {
        "SHELL_ARGS": "args_A",
        // other variables ...
        "environment": "env_A"
      }
    ]
  },
  {
    "inheritEnvironments": [ "env_B" ],
    "name": "B",
    // ...
    "environments": [
      {
        "SHELL_ARGS": "args_B",
        // ...
        "environment": "env_B"
      }
    ]
  }
]

However when I add both to my tasks, i.e. "inheritEnvironments": [ "env_A", "env_B" ], the tasks always uses "env_A", even when configuration B is selected ..

I want e.g. the task "Build" to use "SHELL_ARGS = args_A", when I select configuration A, and "SHELL_ARGS = args_B" when I select configuration B.
Is this possible? Or do I really have to copy paste not only the whole tasks, but also each task for each configuration, only adjusting the "inheritEnvironments" field?

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,049 questions
0 comments No comments
{count} votes

Accepted answer
  1. Dylan Zhu-MSFT 6,416 Reputation points
    2020-10-30T09:04:26.203+00:00

    Hi CR-2420,

    Thank you for feedback.

    You could try to add this argument into the task in the task.vs.json file,

    "inheritEnvironments": [ "${cpp.activeConfiguration}" ],  
    "args": [  
    ...  
    

    By the way, you could also read this blog: Customizing your Environment with Visual C++ and Open Folder

    Best Regards,
    Dylan

    ----
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.