VS Code use VS Developer prompt as integrated Shell
Asked Answered
V

3

2

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, e.g. 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?

Volz answered 18/8, 2021 at 19:38 Comment(1)
You do not really mix path separators \ and / in your actual code, do you? On Windows \ should be used…Osana
A
3

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.

Autocorrelation answered 7/12, 2022 at 9:13 Comment(0)
U
1

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 hackhelpful 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 (e.g.: 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 (e.g.: ${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"
          }
      }
    
Unveil answered 1/9, 2021 at 20:43 Comment(0)
M
0

I encountered a similar issue when trying to run the Visual Studio Developer Command Prompt after installing Visual Studio, and I managed to resolve it. If you're experiencing the following error:

**********************************************************************
** 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

Here's how I resolved it:

  1. Check Your Path Placement:

Ensure that your system's environment variables (specifically the PATH variable) are correctly configured. In my case, I had a mismatch in the order of paths, with the MinGW compiler directory (C:\Dev\mingw64\bin) appearing before Visual Studio paths.

Incorrect order:

  • C:\Dev\mingw64\bin (It could be another compiler before VS)
  • C:\Dev\Microsoft Visual Studio\2022\Community
  • C:\Dev\Microsoft Visual Studio\2022\Community\VC
  • C:\Dev\Microsoft Visual Studio\2022\Community\VC\bin
  1. Reorder Your Paths:

To resolve the issue, I adjusted the order of my paths as follows:

Corrected order:

  • C:\Dev\Microsoft Visual Studio\2022\Community
  • C:\Dev\Microsoft Visual Studio\2022\Community\VC
  • C:\Dev\Microsoft Visual Studio\2022\Community\VC\bin
  • C:\Dev\mingw64\bin (I changed order of the other compiler)

After making these changes, it was seen a prompt to this without any errors:

**********************************************************************
** Visual Studio 2022 Developer Command Prompt v17.7.4
** Copyright (c) 2022 Microsoft Corporation
**********************************************************************

C:\Dev\Microsoft Visual Studio\2022\Community>

I hope this helps you resolve the error.

Modestamodeste answered 13/9, 2023 at 19:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.