Search code examples
jenkinsmsbuildjenkins-pipelinenuget.net-4.5

.NET Framework project 4.5.2 fails to build in Jenkins due to missing metadata dll


Cannot build a .NET Framework Solution using Jenkins.
I Get an error that says CSC : error CS0006: Metadata file '..\packages\Microsoft.CodeAnalysis.Analyzers.1.1.0\analyzers\dotnet\cs\Microsoft.CodeAnalysis.Analyzers.dll' could not be found [C:\Users\...\.jenkins\workspace\...\My.Site.csproj]. It includes a main project and two other projects (an API and Tests) all targeting .NET Framework 4.5.2. The API project was at some point was a nuget package, because when I try to update the CodeAnalysis stack, I get Unable to resolve dependency 'Api.For.MySite'. Source(s) used: 'nuget.org', 'Microsoft Visual Studio Offline Packages', 'Package source'.
I tried:

  1. To set the NUGET_PACKAGES variable, using the windows way and also with the Jenkins EnvIject plugin, to C:\NugetCache / C:\Windows\system32\config\systemprofile\.nuget\packages as described here.
  2. Creating a nuget package for the API itself. But no luck.

My configuration is the following:

  1. Jenkins 2.361.2 with the latest plugins (MSBuild and git) installed
  2. The repository is local: `file://C:/.../MySite
  3. /p:Platform=AnyCPU;Configuration=Debug;PublishDestination="C:\inetpub\wwwroot\MySite (mysite.Site.com)" /restore
  4. The MSBuild I am using is located at C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\amd64.

What else could I try? Does Jenkins need Read and Write? Currently the box near the attribute for the Read Only (for files only) is checked with a square. The Hidden folder atribute is unchecked, which is fine.

Update 24.10.2022
So one build step before the build step mentioned in 3) is to restore packages, Is now /t:restore /p:RestorePackagesConfig=true;SolutionDir="${WORKSPACE}" in jenkins. Now the error is something like this: C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(132,5): error : Unable to find version 'X.Y.Z.W' of package 'MyPackage'. [C:\Users\user.name\.jenkins\workspace\mySite\mySite.csproj] C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(132,5): error : C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\: Package 'MyPackage.X.Y.Z.W' is not found on source 'C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\'. [C:\Users\user.name\.jenkins\workspace\mCloud\mySite\mySite.csproj] C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.targets(132,5): error : https://api.nuget.org/v3/index.json: Package 'MyPackage.X.Y.Z.W' is not found on source 'https://api.nuget.org/v3/index.json'.
The config paths and (offline) package locations are: NuGet Config files used:

  1. C:\Windows\system32\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config

  2. C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.FallbackLocation.config

  1. C:\Program Files (x86)\NuGet\Config\Microsoft.VisualStudio.Offline.config

Feeds used:

  1. C:\Users\user.name\Desktop\MyPackageFolder (MyPackage is here)

  2. https://api.nuget.org/v3/index.json

  3. C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\ (MyPackage is also here)
    One package targets .NET FW 4. I also made the same nuget package folder/file structure for'MyPackage' but still no luck.
    Also two additional packages that give me the same error in the jenkins console output are not needed for the project. The project (re)builds the same if the two packages are not installed.
    One thing I also noticed is that the aforementioned nuget packages (at least one of them) target framework 4 instead of 4.5.2, but that still doesn't explain why there are problems only in jenkins, not with visual studio.
    I admit that now what I have is everything just a big mess, but I don't know where to go or what to do now or how to recover from this as efficient as possible.


Solution

  • Before NuGet added PackageReference around 2016, it was not possible to restore via msbuild. When PackageReference was added, msbuild restore only restored PackageReference projects, not packages.config projects, probably due to an overabundance of caution to avoid possible breaking changes. So, msbuild restore of packages.config wasn't added until VS 16.5 (VS 2019), but even then it's an opt-in switch.

    The error message mentioning path ..\packages\ tells me the project is using packages.config, not PackageReference.

    Therefore, unless you have a separate nuget.exe restore step in your CI, you need to opt-in to msbuild restore via the -p:RestorePackagesConfig=true as documented here: https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#restoring-packagereference-and-packagesconfig-projects-with-msbuild

    Personally, I recommend having separate restore and build steps in your build, rather than doing both restore and build in the same step, so that 1. the logs for each step are shorter, and 2. when something fails, the step that failed gives you more information about probably failed (restore failed? Oh, it's probably a network issue. build failed? I probably have invalid syntax in my commit).