Fody/Costura

How should one build Costura from source?

0xced opened this issue · 9 comments

0xced commented

Enter Question Below (don't delete this line)

I'm trying to build Costura from source (I'd like to propose a real solution to #633) but the build fails.

In order to build Costura, I first tried to run .\build.ps1 -Target BuildAndPackage but it failed with this error:

Preparing to run build script...
Running build script...

Error: Unknown command 'BuildAndPackage'.

       build.cake -target=BuildAndPackage
                          ^^^^^^^^^^^^^^^ No such command

So I finally tried with the following commands instead:

.\build.ps1
.\tools\Cake\Cake.exe --target=BuildAndPackage build.cake

A lot of projects build fine but eventually AssemblyToReference.csproj fails to build because of a reference issue to Catel:

Using Visual Studio 2019 Professional
Overriding ms build tool path to 'C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\MSBuild\Current\Bin\msbuild.exe'
Output directory: 'C:\Projects\GitHub\Costura\output\Release\AssemblyToReference'
Repository url is specified, enabling SourceLink to commit 'https://github.com/Costura.Fody/Costura.Fody/commit/9a101a45041ffb1908a8a36cbc9433b0e62dab1e'
C:/Program Files (x86)/Microsoft Visual Studio/2019/Professional/MSBuild/Current/Bin/msbuild.exe /fl /nologo /bl:C:\Projects\GitHub\Costura\output\Release\MsBuild_AssemblyToReference_build_log.binlog;ProjectImports=Embed /flp:logfile=C:/Projects/GitHub/Costura/output/Release/MsBuild_AssemblyToReference_build_log.log;Verbosity=quiet /logger:XmlFileLogger,C:/Projects/GitHub/Costura/tools/MSBuild.Extension.Pack.1.9.1/tools/net40/MSBuild.ExtensionPack.Loggers.dll;logfile=C:\Projects\GitHub\Costura\output\Release\MsBuild_AssemblyToReference_build_log.xml;verbosity=Detailed;encoding=UTF-8 /m /p:Configuration=Release /p:Platform=AnyCPU /p:ContinuousIntegrationBuild=true /p:ResolveNuGetPackages=false /p:OverridableOutputRootPath=C:\Projects\GitHub\Costura\output\Release /p:OverridableOutputPath=C:\Projects\GitHub\Costura\output\Release\AssemblyToReference /p:PackageOutputPath=C:\Projects\GitHub\Costura\output\Release /p:EnableSourceLink=true /p:EnableSourceControlManagerQueries=false /p:PublishRepositoryUrl=true /p:RepositoryType=git /p:RepositoryUrl=https://github.com/Costura.Fody/Costura.Fody /p:RevisionId=9a101a45041ffb1908a8a36cbc9433b0e62dab1e /target:Build /v:quiet C:/Projects/GitHub/Costura/src/AssemblyToReference/AssemblyToReference.csproj
C:\Projects\GitHub\Costura\src\MethodTimeLogger.cs(2,7): error CS0246: The type or namespace name 'Catel' could not be found (are you missing a using directive or an  assembly reference?) [C:\Projects\GitHub\Costura\src\AssemblyToReference\AssemblyToReference.csproj]
C:\Projects\GitHub\Costura\src\MethodTimeLogger.cs(2,7): error CS0246: The type or namespace name 'Catel' could not be found (are you missing a using directive or an  assembly reference?) [C:\Projects\GitHub\Costura\src\AssemblyToReference\AssemblyToReference.csproj]

I was able to "fix" this build issue by commenting out the 3 lines using Catel (for logging) in MethodTimeLogger.cs.

Am I using the correct command to build Costura from source?
Is there something special to do about Catel before building?

I'm using Visual Studio 2019 Professional, MSBuild version 16.8.2.56705 and I have the .NET Core SDK 5.0.1 installed.

We recently updated to the latest version of Cake. This means you should use:

build.ps1 --target=build

or if you want to test a local debug package:

build.ps1 --target=buildandpackagelocal

or an "official" package:

build.ps1 --target=buildandpackage

Also comment / delete the MethodTimeLogger, then it should all work.

0xced commented

So I tried with .\build.ps1 --target=build but nothing is built. ☹

Here is an excerpt from the log:

========================================
RestorePackages
========================================
Project 'AssemblyToReference' should not be processed, removing from projects to process
Project 'AssemblyToReferenceNative' should not be processed, removing from projects to process
Project 'AssemblyToReferenceMixed' should not be processed, removing from projects to process
Project 'AssemblyToReferencePreEmbedded' should not be processed, removing from projects to process
Project 'AssemblyWithoutInitialize' should not be processed, removing from projects to process
Project 'AssemblyToProcess' should not be processed, removing from projects to process
Project 'ExeToProcess' should not be processed, removing from projects to process
Project 'ExeToProcessWithNative' should not be processed, removing from projects to process
Project 'ExeToProcessWithNativeAndEmbeddedMixed' should not be processed, removing from projects to process
Project 'Costura.Template' should not be processed, removing from projects to process
Project 'Costura.Fody' should not be processed, removing from projects to process
Project 'Costura' should not be processed, removing from projects to process
Project 'Costura.Fody.Tests' should not be processed, removing from projects to process

Same with the Prepare phase.

As you can see from the timings, nothing was built:

Task                          Duration
--------------------------------------------------
Setup                         00:00:02.0415996
Clean                         00:00:11.6972462
UpdateNuGet                   00:00:01.6520763
RestorePackages               00:00:06.7807780
Prepare                       00:00:00.0292140
UpdateInfo                    00:00:00.0363474
CleanupCode                   00:00:00.0020739
Build                         00:00:00.0370386
--------------------------------------------------
Total:                        00:00:22.2763740

I have looked at the lib-generic.cake file and it seems that buildContext.General.Includes does not contain the project names but I have no idea why and no idea how to debug a cake script.

Interesting. I just did exactly the same thing and it processed everything:

image

Just for my understanding, you are running this from inside the repository root, right?

0xced commented

Yes, running from the repository root on commit 951f6d3 after running git clean -fdx to make sure there's no hidden remaining build artifacts.
I modified the ShouldProcessProject method in lib-generic.cake to simply return true and got the build going but eventually hit the missing Catel reference again.

I will push the code that clears the logging stuff. I need to setup a repository template for Fody projects but need to find the time.

0xced commented

Thanks for fixing the build in 16ffc1d. I finally understood why no project was being built: I was using a Developer Command Prompt for VS to build Costura and there, the INCLUDE environment variable was set to C:\Program Files (x86)\Microsoft Visual Studio\2019\Professional\VC\Tools\MSVC\14.28.29333\include, doh! All the projects are built as expected when running the build script in a standard command prompt.

Now, here is the next error which happens during the packaging phase when running .\build.ps1 --target=buildandpackage:

C:\Program Files\dotnet\sdk\5.0.101\Sdks\NuGet.Build.Tasks.Pack\buildCrossTargeting\NuGet.Build.Tasks.Pack.targets(207,5): error NU5100: The assembly 'netclassicweaver\Costura.Fody.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced. [C:\Projects\GitHub\Costura\src\Costura\Costura.csproj]
C:\Program Files\dotnet\sdk\5.0.101\Sdks\NuGet.Build.Tasks.Pack\buildCrossTargeting\NuGet.Build.Tasks.Pack.targets(207,5): error NU5100: The assembly 'netstandardweaver\Costura.Fody.dll' is not inside the 'lib' folder and hence it won't be added as a reference when the package is installed into a project. Move it into the 'lib' folder if it needs to be referenced. [C:\Projects\GitHub\Costura\src\Costura\Costura.csproj]

So I added NU5100 to <NoWarn> and <NoError> in the Directory.build.shared.implicit.props file. Maybe you don't get this warning (treated as error) because you are using the .NET Core 3.1 SDK and I'm using the .NET Core 5.0 SDK?

Anyway, after ignoring NU5100, the build finally succeeds and I have NuGet package in the output\Release directory. 🎉

Thanks for the update!

Sorry this is such a pain, we use these scripts everywhere in our 100+ repositories. For us they are very useful, but I totally understand that they look very complex when looking at it from a single repository point of view.

We are using the .NET Core 5.0 sdk as well, but haven't packed Costura in a while. We do use a similar thing in Catel.Fody. Maybe we fixed it there as well.

Note that there are 2 different ways to package:

Official release

This is what is used to create official releases by the build server. This creates a release build and creates the package in the output\release directory.

.\build.ps1 --target=buildandpackage

Local release

We use this to test local packages in debug mode. Using the local postfix does the following extra steps:

  1. Don't run unit tests
  2. Build in debug mode
  3. Clear the nuget cache for this specific version in %userprofile%.nuget...
  4. Create a package in C:\Source\_packages (which you can add as a nuget package source)

This way the builds are superfast, allow to change code & "rerelease locally" without having to bump a version.

.\build.ps1 --target=buildandpackagelocal
0xced commented

Thanks for your input. I have a working solution that addresses #633 when I'm testing the produced local NuGet package in a sample console app. Unfortunately, the unit test that I added doesn't pass and I'm not sure yet why. I'll probably have some more questions about it…

0xced commented

Well, I figured everything out! 🎉 I have opened #638 which proposes a real solution to the issue described in #633.