简体   繁体   中英

VS Code use VS Developer prompt as integrated Shell

I am trying to use the MSVC build tools inside of VS Code, and as such wanted to use the developer command prompt as an integrated shell.

My current approach was to add a terminal profile and pass the arguments \k "Path/to/VisualStudio\\2019\\Community\\Common7\\Tools\\VsDevCmd.bat" , which works for manually using the terminal.

Unfortunately, this doesn't work with any Tasks, eg NMAKE (which I planned on using).
Executing an NMAKE (or any other programm) in a task results in the following error:

> Executing task in folder C-Project: nmake -f ../Makefile.mk <

[ERROR:parse_cmd.bat] Invalid command line argument: '/d'. Argument will be ignored.
[ERROR:parse_cmd.bat] Invalid command line argument: '/c'. Argument will be ignored.
[ERROR:parse_cmd.bat] Invalid command line argument: 'nmake'. Argument will be ignored.
[ERROR:parse_cmd.bat] Invalid command line argument: '-f'. Argument will be ignored.
[ERROR:parse_cmd.bat] Invalid command line argument: '../Makefile.mk'. Argument will be ignored.
**********************************************************************
** Visual Studio 2019 Developer Command Prompt v16.11.1
** Copyright (c) 2021 Microsoft Corporation
**********************************************************************
[ERROR:VsDevCmd.bat] *** VsDevCmd.bat encountered errors. Environment may be incomplete and/or incorrect. ***
[ERROR:VsDevCmd.bat] In an uninitialized command prompt, please 'set VSCMD_DEBUG=[value]' and then re-run
[ERROR:VsDevCmd.bat] vsdevcmd.bat [args] for additional details.
[ERROR:VsDevCmd.bat] Where [value] is:
[ERROR:VsDevCmd.bat]    1 : basic debug logging
[ERROR:VsDevCmd.bat]    2 : detailed debug logging
[ERROR:VsDevCmd.bat]    3 : trace level logging. Redirection of output to a file when using this level is recommended.
[ERROR:VsDevCmd.bat] Example: set VSCMD_DEBUG=3
[ERROR:VsDevCmd.bat]          vsdevcmd.bat > vsdevcmd.trace.txt 2>&1

It the command prompt is not yet initialized while the task is already trying to execute the command. I tried writing a batch script that first waits and then executes the command, but calling a batch script in the an uninitialized state just results in a syntax error, followed by the terminal shutting down.

Are there any solutions or workarounds for this?

Not sure if this should be considered an "answer" or not - it's more like a hack methinks.

In my workspace's tasks.json , I override the editor's shell (for Windows) and "chain" whatever command I want after the argument to call the Developer Command Prompt batch. I do this using && and <whatever.exe> as the additional shell arguments (in my example below, you'll see I'm basically running path\to\vsdevcmd.bat && powershell.exe ).

Here goes, hope this is hack helpful to you. A couple of notes:

  • The powershell in the task's command seems redundant to me; not sure if it can be omitted.

  • I run && exit at the end of task to get out of the shell; otherwise, it dumps you to the VS command prompt and keeps the task in a running state until you manually exit out.

  • I have the path to my VS installation set in an environment variable (eg: env:VSINSTALLDIR ) - you can hard-code the path if you want, or set up that location in VS Code's global settings ( "terminal.integrated.env.windows" ).

  • Not shown below - I also take advantage of VS Codes input prompt variables (eg: ${input:...} )

     { "label": "update-packages", "type": "shell", "windows": { "options": { "shell": { "executable": "${env:windir}\\System32\\cmd.exe", "args": [ "/k", "\"${env:VSINSTALLDIR}\\Common7\\Tools\\VsDevCmd.bat\"", "-no_logo", "&&", "powershell.exe" ] } } }, "command": "powershell", "args": [ "./scripts/Run-PowershellPackageThingy.ps1", "-nugetLocation", "./.nuget", "-packages", "${input:packages-to-update}", "-source", "${input:package-source}", "&&", "exit" ], "options": { "cwd": "${workspaceFolder}" }, "group": "build", "presentation": { "reveal": "always" } }

Coming from December 2022, here is my solution for integrating Developer Command Prompt and Developer Powershell to VS Code:

    "Developer Powershell": {
      "path": "powershell",
      "args": [
        "-noexit",
        "-c",
        "$vsPath = & \"${env:ProgramFiles(x86)}/Microsoft Visual Studio/Installer/vswhere.exe\" -property installationpath; Import-Module \"$vsPath/Common7/Tools/Microsoft.VisualStudio.DevShell.dll\"; Enter-VsDevShell -VsInstallPath $vsPath -SkipAutomaticLocation"
      ],
      "icon": "terminal-powershell"
    },    
    "Developer Command Prompt": {
      "path": [
        "${env:windir}\\Sysnative\\cmd.exe",
        "${env:windir}\\System32\\cmd.exe"
      ],
      "args": [
        "/k",
        "C:\\Program^ Files\\Microsoft^ Visual^ Studio\\2022\\Enterprise\\Common7\\Tools\\VsDevCmd.bat"
      ],
      "icon": "terminal-cmd"
    },

Notes:

  • If you are using Visual Studio Professional , change Enterprise to Professional. If you are using Community then change Enterprise to Community!
  • I have used "path": "powershell", instead of "path": "pwsh", . Because they are different. pwsh is the new powershell which is cross-platform and there are many people using it. So if you have installed pwsh you can change that line.
  • I did not use %ProgramFiles% because i got error. Program^ Files did the trick. If you are using whitespace in VS Code's settings.json file you can escape them by using ^ . I do not know why nobody mentioned this?
  • Some people use ${env:VSINSTALLDIR} , but this did not work for me. Apparently this environmental variable is not set in my windows 10.

For your specific question on NMAKE, I have cloned libxmp library and compiled it with NMAKE without errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM