Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 483
FrenkyB Avatar asked Oct 04 '19 14:10

FrenkyB


2 Answers

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>
like image 132
LoLance Avatar answered Sep 20 '22 10:09

LoLance


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

like image 43
FrenkyB Avatar answered Sep 21 '22 10:09

FrenkyB