Conditionally include reference in Directory.Build.props using project name

Viewed 1042

My directory structure:

Directory.Build.props
Common
  Common.csproj
Project1
  Project1.csproj
Project2
  Project2.csproj
Project3
  Project3.csproj

I want the Common project referenced by all other projects, so I added this to Directory.Build.props:

<ItemGroup>
  <ProjectReference Include="../Common/Common.csproj" />
</ItemGroup>

But that means the Common project references itself, so I get this error:

error MSB4006: There is a circular dependency in the target dependency graph involving target "_GenerateRestoreProjectPathWalk".

I want to rewrite it to exclude the Common project, maybe using the project name somehow. Something like:

  <ProjectReference Include="../Common/Common.csproj" Condition="WHAT GOES HERE"/>

What do I put in "WHAT GOES HERE"?

1 Answers

You might want to check for a characteristic property from Common.csproj e.g. <RootNamespace>. Suppose Common project looked like this:

<Project Sdk="Microsoft.NET.Sdk" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- ... -->
  <PropertyGroup>
    <RootNamespace>Common</RootNamespace>
    <!-- ... -->
  </PropertyGroup>
  <!-- ... -->
</Project>

The project reference in Directory.Build.props should be:

<ProjectReference Include="./Common/Common.csproj" Condition="'$(RootNamespace)' != 'Common'"/>
Related