Cake build - NugetRestore with PackageReferences

3.7k views Asked by At

I have a Visual Studio 2017 solution which contains .NET Standard, .NET Core, and .NET Framework projects. My .NET Framework 4.6.2 project is using PackageReferences in the .csproj file - instead of a packages.config.

This seems to be the new way of specifying NuGet packages and will allow future migration to .NET Standard 2.0 (I hope). https://learn.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files

csproj:

<ItemGroup>
    <PackageReference Include="LanguageExt.Core">
        <Version>2.1.1</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.SqlServer.Dac">
          <Version>1.0.3</Version>

VS displays cool icons:

project references

Anyway, the solution compiles fine under Visual Studio but fails with build errors when compiling via cake.

Databases\DatabaseInstaller.cs(5,27): error CS0234: The type or namespace name 'Dac' does not exist in the namespace 'Microsoft.SqlServer' (are you missing an assembly reference?) [C:\code\Core.AcceptanceTesting\Core.AcceptanceTesting.csproj]

Are these newer "PackageReferences" supported by cake build?

Here's a segment from my cake script:

// NuGet restore packages for .NET Framework projects (and .NET Core projects)
Task("NuGet-Restore")
    .IsDependentOn("Clean")
    .Does(() =>
    {
        NuGetRestore(solutionFile);
    });

// NuGet restore packages for .NET Core projects only
Task("DotNetCoreRestore")
    .IsDependentOn("NuGet-Restore")
    .Does(() =>
    {
        var settings = new DotNetCoreRestoreSettings
        {
            ArgumentCustomization = args => args.Append("/p:Version=" + versionPrefix + "-" + versionSuffix)
        };
        DotNetCoreRestore(settings);
    });

// Build our solution
 Task("Build")
    .IsDependentOn("DotNetCoreRestore")
    .Does(() =>
    {
        DotNetCoreBuild(
            solutionFile,
            new DotNetCoreBuildSettings()
            {
                ArgumentCustomization = args => args.Append("/p:Version=" + versionPrefix + "-" + versionSuffix),
                Configuration = configuration
            });
    });
2

There are 2 answers

0
pfx On BEST ANSWER

I downloaded your repo and got it building. Use the build definition below. Reason it works in Visual Studio is because it uses MSBuild and Nuget.exe. Don't call DotNetCoreX-commands on NET Framework.

Task("Build")
    .IsDependentOn("NuGet-Restore")
    .Does(() => {
        MSBuild(solutionFile);
});
3
Gary Ewan Park On

What version of Cake are you using? The .Net Core aliases were recently updated to reflect some changes in the API surface. What happens if you try the following:

var settings = new DotNetCoreRestoreSettings
{
    ArgumentCustomization = args => args.Append("/p:Version=" + versionPrefix + "-" + versionSuffix)
};

DotNetCoreRestore(solutionFile, settings);

i.e. include the path to the Solution File.

As per this example:

https://github.com/cake-contrib/Cake.Recipe/blob/develop/Cake.Recipe/Content/build.cake#L128