cakebuild

Parse csproj file using cake's ParseProject


Create minimal repro project:

dotnet new classlib --no-restore --output /tmp/Foo
cd /tmp/Foo
dotnet new tool-manifest
dotnet tool install cake.tool
touch build.cake

Put this minimal script in build.cake:

Task("Default").Does(() => {
  var file = "./Foo.csproj";
  Information(FileExists(file).ToString());
  var props = ParseProject(file);
});
RunTarget("Default");

Run it:

dotnet cake

Result:

========================================
Default
========================================
True
An error occurred when executing task 'Default'.
Error: Failed to parse project properties

Surely this minimal script should succeed - have I made a mistake, or is this a bug?

My environment: linux, dotnet 6.0.302.


Solution

  • Out of box, the ParseProject alias within Cake (version 2.2.0), only knows how to parse Visual Studio Project files that are using the "old" format.

    Since the Visual Studio Project file that you are creating is using the "new" format (i.e. it was generated using the dotnet CLI, and is targetting .NET Core), the ParseProject in Cake will not be able to recognise it.

    There is however an alternative ParseProject alias in the Cake.Incubator addin that can do what you want. You can make use of this by doing:

    #addin nuget:?package=Cake.Incubator&version=7.0.0
    
    Task("Default").Does(() => {
      var file = "./cake-test.csproj";
      Information(FileExists(file).ToString());
      var props = ParseProject(file, "Release");
    });
    
    RunTarget("Default");
    

    Result:

    ========================================
    Default
    ========================================
    True
    
    Task                          Duration
    --------------------------------------------------
    Default                       00:00:00.0496415
    --------------------------------------------------
    Total:                        00:00:00.0496415
    

    At some point, the work in the Cake.Incubator addin will likely be merged into Cake, and will ship as a new version, however, for now, you will need to bring in the addin directly.