6

I am using Visual Studio 2017 and I want to add a post-build command. when I go to 'Macros' window I see the correct path coresponging to $(ProjectDir) variable. So I added the command:

$(ProjectDir)ClientApp\npm run build

but when I execute build I get the following error:

The command 'ClientApp\npm run build'exited with code 3.

I change MSBuild verbose to Diagnose and in the Output window I saw:

1>Target "PostBuildEvent" in file "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets": 1> Using "Exec" task from assembly "Microsoft.Build.Tasks.Core, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a". 1> Task "Exec" 1> Task Parameter:WorkingDirectory=bin\Debug\netcoreapp1.1\ 1> Task Parameter:Command=ClientApp\npm run build 1> ClientApp\npm run build 1> The system cannot find the path specified. 1>
C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets(4933,5): error MSB3073: The command "ClientApp\npm run build" exited with code 3. 1> Done executing task "Exec" -- FAILED.

In the targets file I got these lines:

  <Target
      Name="PostBuildEvent"
      Condition="'$(PostBuildEvent)' != '' and ('$(RunPostBuildEvent)' != 'OnOutputUpdated' or '$(_AssemblyTimestampBeforeCompile)' != '$(_AssemblyTimestampAfterCompile)')"
      DependsOnTargets="$(PostBuildEventDependsOn)">

    <Exec WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" />

  </Target>

Can I change the working dir from $(OutDir) to $(ProjectDir) and how?

Any ideas where I am mistaken? NB - I added the command in a BAT file, just to test but the result was the same

Vasil Indzhev
  • 635
  • 1
  • 7
  • 17
  • Does this answer your question? [Visual Studio post-build event macros are empty](https://stackoverflow.com/questions/26748846/visual-studio-post-build-event-macros-are-empty) – AdrianHHH Mar 22 '22 at 07:45

2 Answers2

19

Try $(MSBuildProjectDirectory) instead

Felix Lehmann
  • 263
  • 2
  • 4
  • 7
    The question is why $(ProjectDir) is empty? – MistyK Jul 17 '17 at 08:50
  • 1
    $(ProjectDir) seems to be correct inside targets(`AfterTargets="BeforeBuild"`), but not at "root level" properties. For those cases $(MSBuildProjectDirectory) works well – Davi Fiamenghi Mar 15 '19 at 21:52
  • 1
    I would guess because it occurs before ``. Putting it /behind/ that Import solved problem for me. – jifb Oct 13 '22 at 14:18
5

The problem could affect other VS variable as well. The universal solution is described here.

Bohdan
  • 468
  • 5
  • 6