msbuild

csproj Import Project only in release mode


In my .csproj I have a Import Project that I only want to use if I am in release mode. I have tried to add it to the Condition section but that is not working. Is there any other way to do this?

Error

The project file could not be loaded. '"' is an unexpected token. The expected token is '='. Line 3164, position 141.

Project file:

<Import 
     Project="packages\RazorGenerator.MsBuild.2.4.1\build\RazorGenerator.MsBuild.targets" 
     Condition="'$(Configuration)'=='Release'" and "Exists('packages\RazorGenerator.MsBuild.2.4.1\build\RazorGenerator.MsBuild.targets')" />

https://learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-conditions?view=vs-2022


Solution

  • You did not write valid XML. It might become more obvious if I format it differently:

    <Import 
      Project="packages\RazorGenerator.MsBuild.2.4.1\build\RazorGenerator.MsBuild.targets" 
      Condition="'$(Configuration)'=='Release'" 
      and
      "Exists('packages\RazorGenerator.MsBuild.2.4.1\build\RazorGenerator.MsBuild.targets')" 
    />
    

    It your condition attribute closes after the release, then you have an empty and attribute, then suddenly text inside quotes. That is where the error occurs.

    Your import should look something like:

    <Import 
      Project="packages\RazorGenerator.MsBuild.2.4.1\build\RazorGenerator.MsBuild.targets" 
      Condition="'$(Configuration)'=='Release' And Exists('packages\RazorGenerator.MsBuild.2.4.1\build\RazorGenerator.MsBuild.targets')" 
    />
    

    For more information, see learn.microsoft.com, boolean operators are explained on that page in more detail.