$(SolutionDir) MSBuild property incorrect when run

2019-02-12 05:58发布

问题:

When I run Sandcastle Help File Builder project file (for example myproject.shfbproj) using Windows CMD, I get an annoying issue: $(SolutionDir) has the same value as $(ProjectDir), and this means that project documentation sources won't build correctly because I'm adding custom targets which already uses $(SolutionDir).

If I build the whole Sandcastle Help File Builder from Visual Studio it builds successfully.

I'm using the following command (executed from the directory where the project is stored):

"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" /p:Configuration=Development myproject.shfbproj

Is there any workaround for this?

回答1:

You only get the the correct $(SolutionDir) if you're building your solution (.sln file). The .shfbproj is a project file, and as such has no reference to it's parent solution. You can try specifying the $(SolutionDir) in your command line explicitly:

msbuild /p:Configuration=Development /p:SolutionDir=MySolutionDir myproject.shfbproj

For reference: http://blogs.msdn.com/b/pfedev/archive/2008/09/26/solutiondir-and-msbuild.aspx



回答2:

While @m0sa has pointed out a very obvious fact (since I'm building the Sandcastle Help File Builder project, there's no actual solution directory), the issue was also happening during a TFS Build when building the solution where resides the so-called documentation project.

For now I've managed to solve the issue using a nasty workaround: adding a SolutionDir property in each C# project that needs to be built as part of documentation project:

  <PropertyGroup>
    <SolutionDir>$([System.IO.Path]::GetDirectoryName($(MSBuildProjectDirectory)))\</SolutionDir>
  </PropertyGroup>

Since my solution file will always be located in the parent directory of Sandcastle Help File Builder project diretory, in my case this workaround works...

Now referenced projects as documentation sources are able to import a custom project where I define common MSBuild properties both in Visual Studio builds or external ones:

<Import Project="$(SolutionDir)MSBuild\Common.properties" />