Cake Clean task is skipped on cake build

Viewed 484

Brand-new Cake project, build.cake written as in Setting Up A New Project, added to a net5.0 console application.

When running dotnet cake, the task Clean is silently skipped by runner.

I ran dotnet cake --target="Clean" --verbosity=normal and received this:

Error: One or more errors occurred. (Could not reach target 'Clean' since it was skipped due to a criteria.)

No idea what criteria is skipping the task.

My build.cake:

var target = Argument("target", "Test");
var configuration = Argument("configuration", "Release");

///////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////

Task("Clean")
.WithCriteria(c => HasArgument("rebuild"))
.Does(() => 
{
    CleanDirectory($"./LucroMei/bin/{configuration}");
});

Task("Build")
.IsDependentOn("Clean")
.Does(() => 
{
    DotNetCoreBuild("./LucroMei.sln", new DotNetCoreBuildSettings
    {
        Configuration = configuration,
    });
});

Task("Test")
.IsDependentOn("Build")
.Does(() => 
{
    DotNetCoreTest("./LucroMei.sln", new DotNetCoreTestSettings
    {
        Configuration = configuration,
        NoBuild = true,
    });
});

///////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////

RunTarget(target);
2 Answers

The Clean task in your script is defined with criteria, take a look at this line:

.WithCriteria(c => HasArgument("rebuild"))

It means that the task will run only in case the condition is specified. The condition is HasArgument("rebuild"), which is trying to find the argument named rebuild among other arguments specified.

If you run your script like this, the Clean target will run:

dotnet cake --rebuild

The error message is very clear:

Could not reach target 'Clean' since it was skipped due to a criteria

Looking at your Clean target, it has a criteria expectation:

Task("Clean")
.WithCriteria(c => HasArgument("rebuild")) // <<#<<#<<#<<#<<#<<#####
.Does(() => { ... });

This means that your Clean criteria will only run if you provide the argument --rebuild:

dotnet cake --target="Clean" --verbosity=normal --rebuild
Related