How to integrate Developer Command Prompt for VS 2022 as Terminal in VS Code
Asked Answered
H

2

5

I would like to have the Microsoft VS C++ compiler cl available in VisualStudio Code.

I have Visual Studio Build Tools installed and can call cl from the Developer Command prompt.

The default way recommended by Microsoft to get VS Code to use the cl compiler is to call VS Code from the Developer Command prompt.

I would like to do it differently, with a terminal that I can call from VS Code and make it the default terminal if needed.

In Windows Terminal, I get the automatically generated Developer Command prompt entry with the command line:

cmd.exe /k "C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\Tools\VsDevCmd.bat" -arch=x64 -host_arch=x64`

or

powershell.exe -NoExit -Command "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 0d0637f3 -SkipAutomaticLocation -DevCmdArguments """-arch=x64 -host_arch=x64"""}"

If I paste this line into an existing vscode terminal, it works and I can use the cl compiler. But I can't manage to get it into an integrated terminal in settings.json.

This is what I tried in the settings.json:

"Developer Command Prompt for VS 2022": {
    "path": [
        "${env:windir}\\Sysnative\\cmd.exe /k \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\Common7\\Tools\\VsDevCmd.bat\" -arch=x64 -host_arch=x64",
        "${env:windir}\\System32\\cmd.exe /k \"C:\\Program Files (x86)\\Microsoft Visual Studio\\2022\\BuildTools\\Common7\\Tools\\VsDevCmd.bat\" -arch=x64 -host_arch=x64"
    ],
    "overrideName": true,
    "args": [],
    "icon": "terminal-cmd"
},

Since VScode is pretty smart, it recognizes that this won't work and doesn't even list it as an entry within the available terminals.

Furthermore, i have absolutely no idea what to do with the commandline given for PowerShell, notably the three doublequotes after Import-Module and how to handle them.

Helmick answered 4/12, 2022 at 10:27 Comment(0)
C
6

Try the following to load VsDevCmd.bat in cmd.exe from the VSCode integrated terminal.

For running it through PowerShell, you should be able to just replace the path with PowerShell, and alter the arguments to match accordingly (/k would become -NoExit, etc).

"VsDevCmd (2022)": {
    "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
    ],
    "args": [
        "/k",
        // Path below assumes a VS2022 Community install; 
        // update as appropriate if your IDE installation path
        // is different, or if using the standalone build tools
        "C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/Tools/VsDevCmd.bat",
        "-arch=x64",
        "-host_arch=x64"
    ],
    "overrideName": true,
    "icon": "terminal-cmd"
},

enter image description here

Calv answered 4/12, 2022 at 13:3 Comment(7)
You are correct; see my updated answer above to allow for REPL (read-eval-print-loop) interaction.Calv
Yess- thank you using the "/k" as a seperate Argument from the actual commandline passed to cmd was the trick.. It works!Helmick
I thought by getting an answer for developer cmd, it would be easy to transfer it to the powershell prompt. Turns out, it isn't. The Commandline i get from Windows terminal is powershell.exe -NoExit -Command "&{Import-Module """C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\Common7\Tools\Microsoft.VisualStudio.DevShell.dll"""; Enter-VsDevShell 0d0637f3 -SkipAutomaticLocation -DevCmdArguments """-arch=x64 -host_arch=x64"""}" Again im stuck with the amount of parameters that i don't know how to properly translate into settings.json terminal setup.Helmick
As now i have a working command prompt with access to cl compiler, this isn't urgent in any way, but i feel the solution should be noted here too, as it turns out to be more difficult to accomplish than expected. So i'm thinking about wheater i should expand this question to cover both, cmd and PowerShell, or having another question?Helmick
first of all, in the Powershell command, why the heck three doublequotes after Import-Module and how to handle them -all the same, different, escape them, or some of them or whatever???Helmick
@Helmick I stumbled across this SO answer while testing; let me know if it does the trick for you. A couple of your other questions are answered at that link also. Keep in mind, if an arg string contains a quoted path, VSCode will strip the quotes if the path isn't an executable. """ is an escape sequence for quotes, but for VSCode you're better off using ' or \". Inserting a space-escape also works: "Program^ Files...".Calv
FYI, to see how your args array is being parsed by VSCode, you can set the first arg to "powershell.exe",; when it fails, the return code and actual args used will show in your notifications: i.imgur.com/67m5jn8.pngCalv
T
2

Came upon this answer in my search for how to do this. Figured out the powershell version that worked for me and thought I'd share for posterity.

Note: the default profile was my choice but included it in case someone wants to apply the same setting.

"terminal.integrated.defaultProfile.windows": "VSDev_PowerShell",
"terminal.integrated.profiles.windows": {
   "VSDev_PowerShell": {
      "args": [
        "-noe",
        "-c",
        "&{Import-Module \"C:/Program Files/Microsoft Visual Studio/2022/Community/Common7/Tools/Microsoft.VisualStudio.DevShell.dll\"; Enter-VsDevShell 1d058d31 -SkipAutomaticLocation -DevCmdArguments \"-arch=x64 -host_arch=x64\"}"
      ],
      "source": "PowerShell",
      "icon": "terminal-powershell"
   }
},
Tinkle answered 5/3, 2023 at 23:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.