${file} macro in "open folder" development when a startup item is selected

Sándor Deák 0 Reputation points
2025-03-23T21:03:56.9866667+00:00

Hello!

I recently switched to the "open folder" feature in Visual Studio 2022. My build tasks in tasks.vs.json use the ${file} macro to figure out what file(s) should be built. I usually run these tasks with Ctrl+B . However, if there is a selected startup item (an .exe) for debugging, the ${file} macro always resolves to that .exe instead of the .cpp file that I'm editing. This means that I can't use Ctrl+B for building, only if I deselect the active startup item, which is very annoying as I'm constantly switching between compiling and debugging.

Is there a way to make the ${file} macro resolve to the currently active text file regardless of the selected startup item? Or is there another way to reference the active text file in tasks.vs.json

? Thank you for your answer in advance.

Developer technologies Visual Studio Setup
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2025-03-24T03:26:40.4966667+00:00

    Hello Sándor Deák,

    Welcome to Microsoft Q&A forum.

    As we can see from this document,

    ${file}: The arguments for this task include a macro that retrieves the file currently selected in Solution Explorer

    If you have set the active startup item(.exe file), it could be detected as the currently selected file in Solution Explorer when building. I would suggest you can set appliesTo property in tasks.vs.json file to limit the scope of ${file} to .cpp files.

    For example

    {
      "version": "0.2.1",
      "tasks": [
        {
          "taskName": "Echo filename",
          "appliesTo": "*.cpp",
          "type": "default",
          "command": "${env.COMSPEC}",
          "args": [
            "echo ${file}"
          ]
        }
      ]
    }
    

    Best Regards,

    Dou


    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.


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.