FrenkyB
FrenkyB

Reputation: 7207

VS2019 - msbuild is not recognized as internal or external command

I am using VS2019 community and I've set pre-build event:

msbuild "$(ProjectPath)" /t:Clean

I am getting error:

  'MSBuild' is not recognized as an internal or external command,
  operable program or batch file

How is this possible? Isn't this command supposed to be build into visual studio?

I've checked this and tried to set path environment, but it doesn't help.

Does anybody else has the same problem with this command in VS2019?

Upvotes: 9

Views: 32551

Answers (4)

Thuy
Thuy

Reputation: 1665

On my Windows computer:

  1. Search for Environment Variables
  2. Edit Path
  3. New
  4. C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin

Upvotes: 0

YanMax
YanMax

Reputation: 146

Using "dotnet" instead of "msbuild" could work if you got that installed.

So, in my case, instead of running "msbuild /t:restore" I figured I can use "dotnet build" and have the same result.

Here's the documentation in case you want to see more equivalent commands. https://learn.microsoft.com/en-us/dotnet/core/tools/

Upvotes: 3

FrenkyB
FrenkyB

Reputation: 7207

With latest update to VS2019 - version 16.3.4 - the error is no longer there.

Upvotes: 2

LoLance
LoLance

Reputation: 28216

Steps that work in my machine:

  1. See this, first we need to make sure MSBuild can be recognized by cmd.exe.

  2. If the command can be recognized by cmd.exe but not build-event from VS, restart the PC can help resolve this issue.

    (Something strange is that for my VS still can't recognize it until a restart of the computer)

  3. For VS2019, the correct msbuild path is C:\Program Files (x86)\Microsoft Visual Studio\2019\xxx\MSBuild\Current\Bin

And here's another workaround:

Apart from adding the path of msbuild.exe into Environment Path and call it in pre-build event, you can also consider using MSBuild Task.

Add script below into xx.csproj:(work for .net framework...)

  <Target Name="MyCleanBeforeBuild" BeforeTargets="BeforeBuild">
    <MSBuild Projects="$(ProjectPath)" Targets="clean"/>
    <!--<Message Text="Custom Clean" Importance="high"/>-->
  </Target>

Upvotes: 9

Related Questions