Visual Studio conditional project reference based on a constant

I suspect the problem is that you are conditioning a project reference to Module1, not whether to include Module1 in the solution.

Including a project in a solution (and hence loading it with the solution) and a project referencing another project in a solution are two different things of course.

UPDATE:

If you truly want to condition a project reference, Joe Wrobel wrote a related blog post that should help. The key takeaway is to wrap the ItemGroup that contains the ProjectReference to condition in a Choose element - for example:

<Choose>
  <When Condition="$(DefineConstants.Contains('SAMPLECONSTANT1'))">
    <ItemGroup>
      <ProjectReference Include="..\Solution1.Modules.Module1\Solution1.Modules.Module1.csproj">
        <Project>{4E378BD0-4FF8-4160-9331-1ECBFD2B6F30}</Project>
        <Name>Solution1.Modules.Module1</Name>
      </ProjectReference>
      <!-- other ProjectReference elements -->
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <!-- other ProjectReference elements -->
    </ItemGroup>
  </Otherwise>
</Choose>

From my tests this evening, this works great to condition a project reference(s) on whether a constant like SAMPLECONSTANT1 is defined. However, note that conditioned project references do not show in Solution Explorer under the (would-be) referencing project's References folder - regardless whether the conditioning constant is defined.

To see that the conditioning worked, I had to build: with SAMPLECONSTANT1 defined, the referencing project built successfully while using a class defined in Module1 - as expected; and without SAMPLECONSTANT1 defined, the referencing project failed to build because the class defined in Module1 could not be resolved - also as expected.


The accepted answer did not work for me. However, I do not imply that it is incorrect. What worked for me is this:

<Import Project="..\MnM.GWS.Chip2\MnM.GWS.Chip1.projitems" Label="Shared" Condition="$(DefineConstants.Contains('Chip1'))" />
<Import Project="..\MnM.GWS.Chip2\MnM.GWS.Chip2.projitems" Label="Shared" Condition="$(DefineConstants.Contains('Chip2'))" />

I did not get any error while changing constants.