visual-studiomsbuildazure-devops

Is there an MSBuild property providing the repository root?


When running from Visual Studio, MSBuild is given the $(SolutionDir) macro/property. Running from Azure DevOps, this property does not exist (rightfully so).

Azure DevOps has the Build.SourceBranch variable which can be passed to MsBuild as a property. This variable is better because it points directly to the repository root while $(SolutionDir) may point to a sub-directory if the solution is not in the root.

Is there a consistent MsBuild property I can use in both Visual Studio and Azure DevOps which can point to the repository root?

One way to achieve this is to have a Directory.Build.props file in the repository root and Specify a property <RepositoryRoot>$(MSBuildThisFileDirectory)</RepositoryRoot> however one restriction I have prevents me from doing this.


Solution

  • Short answer: No, there isn't a default understanding of source control systems in MSBuild.

    The Directory.Build.props file is likely the most robust approach, showing up regardless of building a solution or individual projects, since it stays the same regardless of the VCS system you use or migrate to/from.

    Other approaches would need to hard code the VCS structure, e.g. looking for the a directory that contains a .gitignore file (which will not work if you don't use such a file):

    <PropertyGroup>
      <RepoRoot>$([System.IO.Path]::GetDirectoryName($([MSBuild]::GetPathOfFileAbove('.gitignore', '$(MSBuildThisFileDirectory)'))))</RepoRoot>
    </PropertyGroup>