Visual Studio $(ProjectDir) is empty string
Asked Answered
L

2

10

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

Lohengrin answered 18/4, 2017 at 7:55 Comment(1)
Does this answer your question? Visual Studio post-build event macros are emptyMarketa
M
26

Try $(MSBuildProjectDirectory) instead

Materi answered 18/4, 2017 at 8:10 Comment(3)
The question is why $(ProjectDir) is empty?Sorites
$(ProjectDir) seems to be correct inside targets(AfterTargets="BeforeBuild"), but not at "root level" properties. For those cases $(MSBuildProjectDirectory) works wellRalina
I would guess because it occurs before <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />. Putting it /behind/ that Import solved problem for me.Pore
S
6

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

Sheep answered 22/3, 2022 at 7:41 Comment(1)
Dumbest bug ever. How is this not fixed after all this time.Profitable

© 2022 - 2024 — McMap. All rights reserved.