'dotnet build' command not finding NuGet packages (they exist) - c#

I'm trying to build my .NET Core 2.1 application from the command-line on my Jenkins server.
It builds fine on my local machine (Windows 10, Visual Studio 2017 Enterprise), and if I manually build it on the server Jenkins is running on (manually checkout Git repository and running dotnet build). It's only when I'm doing it through Jenkins in a Docker container that it fails.
The error:
/usr/share/dotnet/sdk/2.1.403/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(198,5): error NETSDK1064: Package Microsoft.CodeQuality.Analyzers, version 2.6.2 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. [/var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj]
The package exists in ~/.nuget/packages/.
I'm aware my Jenkinsfile is a bit weird at the moment, but that has to do with me trying to make this all work. It seems dotnet restore is working fine, but dotnet build is somehow not locating these files.
I've tried specifying the package directory (through the environment variable, with dotnet restore --sources and dotnet build --sources.
I attached pretty much anything I could think of at the moment, but if I forgot something please tell. I'm pretty lost at the moment.
My Jenkinsfile:
pipeline {
agent {
docker {
image 'microsoft/dotnet:2.1-sdk'
}
}
environment {
HOME = '.'
NUGET_PACKAGES = './.nuget/packages/'
}
stages {
stage('pre-build') {
steps {
// logging tooling versions
sh 'dotnet --info'
sh 'find ~/.nuget/packages/microsoft.codequality.analyzers/ -type f'
sh 'dotnet nuget locals all --list'
sh 'dotnet restore'
// sh 'dotnet clean'
}
}
stage('build') {
steps {
// sh 'dotnet restore --force --no-cache'
// sh 'ls ./packages/'
// sh 'find ./packages/microsoft.codequality.analyzers/ -type f'
sh 'dotnet build --no-restore'
}
}
stage('test') {
steps {
sh 'no test project configured'
// sh 'dotnet test'
}
}
}
}
My .csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.6.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>
<CodeAnalysisRuleSet></CodeAnalysisRuleSet>
<Features>IOperation</Features>
</PropertyGroup>
</Project>
Some build details:
$ docker --version
Docker version 18.06.1-ce, build e68fc7a
Inside docker container:
$ dotnet --info
.NET Core SDK (reflecting any global.json):
Version: 2.1.403
Commit: 04e15494b6
Runtime Environment:
OS Name: debian
OS Version: 9
OS Platform: Linux
RID: debian.9-arm
Base Path: /usr/share/dotnet/sdk/2.1.403/
Host (useful for support):
Version: 2.1.5
Commit: 290303f510
.NET Core SDKs installed:
2.1.403 [/usr/share/dotnet/sdk]
.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.5 [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.5 [/usr/share/dotnet/shared/Microsoft.NETCore.App]
$ find ./.nuget/packages/microsoft.codequality.analyzers/ -type f
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/analyzers/dotnet/vb/Microsoft.CodeQuality.VisualBasic.Analyzers.dll
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/analyzers/dotnet/vb/Microsoft.CodeQuality.Analyzers.dll
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/analyzers/dotnet/cs/Microsoft.CodeQuality.Analyzers.dll
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/analyzers/dotnet/cs/Microsoft.CodeQuality.CSharp.Analyzers.dll
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/.signature.p7s
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/ThirdPartyNotices.rtf
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/build/Microsoft.CodeQuality.CSharp.Analyzers.props
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/build/Microsoft.CodeQuality.VisualBasic.Analyzers.props
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/build/Microsoft.CodeQuality.Analyzers.props
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/DesignRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/DocumentationRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/PerformanceRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/UsageRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/ReliabilityRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/MaintainabilityRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/SecurityRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/PerformanceRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/MaintainabilityRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/UsageRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/DocumentationRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/ReliabilityRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/AllRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/NamingRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/AllRulesDisabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/SecurityRulesEnabled.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/DesignRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/NamingRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/rulesets/AllRulesDefault.ruleset
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/microsoft.codequality.analyzers.2.6.2.nupkg.sha512
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/microsoft.codequality.analyzers.2.6.2.nupkg
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/microsoft.codequality.analyzers.nuspec
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/tools/uninstall.ps1
./.nuget/packages/microsoft.codequality.analyzers/2.6.2/tools/install.ps1
$ dotnet nuget locals all --list
info : http-cache: ./.local/share/NuGet/v3-cache
info : global-packages: ./.nuget/packages/
info : temp: /tmp/NuGetScratch
info : plugins-cache: ./.local/share/NuGet/plugins-cache
$ dotnet restore
Restoring packages for /var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj...
Restore completed in 13.5 sec for /var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj.
$ dotnet build --no-restore
Microsoft (R) Build Engine version 15.8.169+g1ccb72aefa for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
/usr/share/dotnet/sdk/2.1.403/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(198,5): error NETSDK1064: Package Microsoft.CodeQuality.Analyzers, version 2.6.2 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. [/var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj]
Build FAILED.
/usr/share/dotnet/sdk/2.1.403/Sdks/Microsoft.NET.Sdk/targets/Microsoft.PackageDependencyResolution.targets(198,5): error NETSDK1064: Package Microsoft.CodeQuality.Analyzers, version 2.6.2 was not found. It might have been deleted since NuGet restore. Otherwise, NuGet restore might have only partially completed, which might have been due to maximum path length restrictions. [/var/lib/jenkins/jobs/pr-builder/jobs/cgi-api/branches/PR-18/workspace/API/API.csproj]
0 Warning(s)
1 Error(s)

I've resolved the issue by specifying where dotnet build should look for the NuGet packages. It that within the image it doesn't look at the right location (although I'm not sure where it is looking either...)
I've specified the location with the --packages option. This option is listed in the dotnet restore documentation, but not in the dotnet build ones, even though it is available there as well (and you need it there, specifically).
You can specify it as follows:
dotnet restore --packages <path>
and
dotnet build --packages <path>
It works with the --no-restore option as well, if you need that.
My final Jenkinsfile looks as follows:
pipeline {
agent {
docker {
image 'microsoft/dotnet:2.1-sdk'
}
}
environment {
HOME = '.'
}
stages {
stage('pre-build') {
steps {
// logging tooling versions
sh 'dotnet --info'
sh 'dotnet nuget locals all --list'
sh 'dotnet clean'
}
}
stage('build') {
steps {
sh 'dotnet build --packages ./.nuget/packages'
}
}
stage('test') {
steps {
sh 'echo no test project configured'
// sh 'dotnet test'
}
}
}
}

I ran into a similar issue solved by clearing global packages and cache with dotnet nuget locals all --clear as explained here.

Related

Azure devops private nuget takes a long time to find package

We are using Azure DevOps pipelines. We have a step that publishes the our private nuget package to the Azure Artifacts. However, the build breaks at other steps because the nuget package (that was published on previous steps) is not found. The strange thing is that after the package is published, I can see it in the package-manager console or on Visual Studio and even in the Artifacts in Azure DevOps. But for some reason, the pipeline doesn't find the package. After 30-50min, I re-run the pipeline and then it finds the package.
What could be happening to take so long time for the pipeline to find the my package?
Edit 1:
This is my yaml for the step with error
- script: |
pwd && ls -la
dotnet restore "$(solution_path)" $(nuget_args)
dotnet publish -c Release -o $(System.DefaultWorkingDirectory)/bin "$(main_project_path)"
mkdir artifact
cp -r $(System.DefaultWorkingDirectory)/bin artifact/bin
displayName: Build Application
The error is:
/data/vstsagent/user/389/s/src/MyProject.csproj
: error NU1102: Unable to find package MyPackage with
version (>= 2.1.0) [/data/vstsagent/user/389/s/src/MySolution.sln]
/data/vstsagent/user/389/s/src/MyProject.csproj
: error NU1102: - Found 28 version(s) in MyPrivateRepository [ Nearest
version: 2.1.0-preview.6 ]
[/data/vstsagent/user/389/s/src/MySolution.sln]
It is not clear what feed is used by dotnet restore command.
Confirm that you have configured your custom nuget feed in nuget.config and using it.
Try adding "--verbose" switch in dotnet restore command and check if your feed is used for restoring packages.
Use the --no-cache option so dotnet restore doesn't use the local cache.

Azure Pipeline: dotnet publish fails - assets.json doesn't have a target for '.NETCoreApp,Version=v3.1/win-x64'

I have a .NetCore 3.1 commandline app. When buidling locally and publishing, it works completely fine using below commandline
dotnet publish -c dev -r win-x64 --self-contained true
In the Azure pipeline - I had to do the dotnet restore before doing a publish using the above command. Whilst publishing I had to add extra param --no-restore, as per Microsoft's recommendation here as I have private nuget feeds.
dotnet publish -c dev -r win-x64 --self-contained true --no-restore
Most dotnet commands, including build, publish, and test include an
implicit restore step. This will fail against authenticated feeds,
even if you ran a successful dotnet restore in an earlier step,
because the earlier step will have cleaned up the credentials it used.
To fix this issue, add the --no-restore flag to the Arguments textbox.
Now, the publish part of the pipeline has started failing with the error -
C:\Program Files\dotnet\sdk\3.1.401\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): error NETSDK1047: Assets file 'MyProject\obj\project.assets.json' doesn't have a target for '.NETCoreApp,Version=v3.1/win-x64'. Ensure that restore has run and that you have included 'netcoreapp3.1' in the TargetFrameworks for your project. You may also need to include 'win-x64' in your project's RuntimeIdentifiers.
Am not using a publish xml, but specifying all the arguments as shown above in the command line. I've checked the csproj has the target framework specified
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Configurations>Debug;Release;dev;test;pre;prod</Configurations>
</PropertyGroup>
Need any pointers as to what might be going wrong here?
Thanks
Please add RuntimeIdentifier as mentioned in the error message:
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Configurations>Debug;Release;dev;test;pre;prod</Configurations>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
Please also check PlatformTarget.
<PlatformTarget>AnyCPU</PlatformTarget>
I guess the local machine has a different OS than the build agent.

Unable to locate repository containing directory during dotnet build or publish

I am adding support for Docker on my ASP.NET Core 2.2 app and while testing the commands for docker locally I've found that the dotnet publish -f netcoreapp2.2 -c Release -o out errors out.
./IdentityServer.sln
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServer", "IdentityServer\IdentityServer.csproj", "{A435DE31-3D1C-4228-BBD9-0157E849D07D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Dependencies", "Dependencies", "{0A7E3F1D-5162-463F-BAF8-714C7FD37B8C}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServer4", "dependencies\identityserver4\src\IdentityServer4.csproj", "{3A32B19C-B6FC-4A2D-9421-5A16849B7C2A}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServer4.AspNetIdentity", "dependencies\identityserver4.aspnetidentity\src\IdentityServer4.AspNetIdentity.csproj", "{0E36AE9A-B9B6-4E1C-B446-82A3265432F8}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "IdentityServer.Email", "IdentityServer.Email\IdentityServer.Email.csproj", "{BD699934-C404-4CF4-B77C-511E584D1754}"
EndProject
./IdentityServer/IdentityServer.csproj: https://gist.github.com/paulcsiki/4d3b827af418044dacea0149a2361b81
Output of dotnet restore on the same folder as the solution:
Restore completed in 52.96 ms for /Users/paul/docker/dependencies/identityserver4.aspnetidentity/src/IdentityServer4.AspNetIdentity.csproj.
Restore completed in 52.96 ms for /Users/paul/docker/dependencies/identityserver4/src/IdentityServer4.csproj.
Restore completed in 54.56 ms for /Users/paul/docker/IdentityServer/IdentityServer.csproj.
Restore completed in 52.97 ms for /Users/paul/docker/IdentityServer.Email/IdentityServer.Email.csproj.
Output of the dotnet publish -f netcoreapp2.2 -c Release -o out on the same folder as the solution:
Microsoft (R) Build Engine version 15.9.20+g88f5fadfbe for .NET Core
Copyright (C) Microsoft Corporation. All rights reserved.
Restore completed in 44.29 ms for /Users/paul/docker/dependencies/identityserver4/src/IdentityServer4.csproj.
Restore completed in 44.3 ms for /Users/paul/docker/IdentityServer.Email/IdentityServer.Email.csproj.
Restore completed in 45.91 ms for /Users/paul/docker/IdentityServer/IdentityServer.csproj.
Restore completed in 44.3 ms for /Users/paul/docker/dependencies/identityserver4.aspnetidentity/src/IdentityServer4.AspNetIdentity.csproj.
/Users/paul/.nuget/packages/microsoft.build.tasks.git/1.0.0-beta-63127-02/build/Microsoft.Build.Tasks.Git.targets(20,5): error : Unable to locate repository containing directory '/Users/paul/docker/dependencies/identityserver4/src'. [/Users/paul/docker/dependencies/identityserver4/src/IdentityServer4.csproj]
IdentityServer.Email -> /Users/paul/docker/IdentityServer.Email/bin/Release/netcoreapp2.2/IdentityServer.Email.dll
IdentityServer.Email -> /Users/paul/docker/IdentityServer.Email/bin/Release/netcoreapp2.2/IdentityServer.Email.Views.dll
IdentityServer.Email -> /Users/paul/docker/IdentityServer.Email/out/
I've tried running the restore and publish command from the WebApp folder (/Users/paul/docker/IdentityServer) and it yields the same output.
Output of the file /Users/paul/docker/dependencies/identityserver4/src/IdentityServer4.csproj:
/Users/paul/docker/dependencies/identityserver4/src/IdentityServer4.csproj: exported SGML document text, UTF-8 Unicode (with BOM) text
The problem was that I have referenced a project called IdentityServer4 which had a reference to the Sourcelink NuGet package which was looking for the .git folder. Removing the package reference has resolved the issue.
I can confirm #Paul solution. I edited the .csproj file and removed the package reference to Microsoft.SourceLink.GitHub:
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<!-- Remove following line: -->
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0-beta-62925-02" PrivateAssets="All" />
</ItemGroup>
</Project>

Any way to run dotnet-ef from a published app

I'm constructing a docker container from a Dotnet Core 2.0 WebApp that was packaged with dotnet publish, but publishing this way doesn't copy the extensions in Microsoft.EntityFrameworkCore.Tools.DotNet that enable the dotnet-ef command.
Specifically, I want to run dotnet ef commands in the container, but I get this error:
No executable found matching command "dotnet-ef"
My .csproj file contains this, so dotnet-ef works fine on the host:
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
Is there a way to run migrations from a dotnet app that's been packaged with dotnet publish?
Based on #bricelam's suggestion and Ben Day's blog entry, this works for dotnet 2.x on Linux:
$ dotnet exec \
--depsfile Api.deps.json \
/usr/share/dotnet/sdk/NuGetFallbackFolder/microsoft.entityframeworkcore.tools.dotnet/2.0.2/tools/netcoreapp2.0/ef.dll \
database update \
--assembly ./MyDllWithMigrations.dll \
--startup-assembly Api.dll \
--project-dir . \
--verbose
(This is based on the microsoft/aspnetcore-build base image.)

How do I use a .NET core class library in a .NET core application?

Specifically on ubuntu, this doesn't appear to work.
For example, if I do this:
dotnet new sln -n HelloWorld
dotnet new classlib -n HelloLib
dotnet new console -n HelloApp
dotnet sln add ./HelloApp/HelloApp.csproj
dotnet sln add ./HelloLib/HelloLib.csproj
dotnet restore
dotnet build
cd HelloApp/
dotnet add reference ../HelloLib/HelloLib.csproj
And modify Program.cs to be:
using System;
using HelloLib;
namespace HelloApp
{
class Program
{
static void Main(string[] args)
{
var x = new Class1();
Console.WriteLine("Hello World!");
}
}
}
Then the application will compile, generating these artifacts:
HelloApp/bin$ du -a
4 ./Debug/netcoreapp1.1/HelloApp.deps.json
4 ./Debug/netcoreapp1.1/HelloApp.runtimeconfig.json
4 ./Debug/netcoreapp1.1/HelloApp.pdb
4 ./Debug/netcoreapp1.1/HelloLib.pdb
8 ./Debug/netcoreapp1.1/HelloApp.dll
4 ./Debug/netcoreapp1.1/HelloApp.runtimeconfig.dev.json
4 ./Debug/netcoreapp1.1/HelloLib.dll
36 ./Debug/netcoreapp1.1
40 ./Debug
44 .
...but executing the application fails:
$ dotnet HelloApp.dll
Unhandled Exception: System.IO.FileNotFoundException: Could not load file or assembly 'HelloLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
Aborted (core dumped)
If I remove the using HelloLib line, it works:
$ dotnet HelloApp.dll
Hello World!
What's up with that?
I presume its got something to do with the confusing incompatibility in the project files:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard1.4</TargetFramework> <-- **THIS**
</PropertyGroup>
</Project>
vs:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp1.1</TargetFramework> <-- **And THIS**
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\HelloLib\HelloLib.csproj">
<Project>{1607a379-5bae-423b-8efc-796a06556be0}</Project>
<Name>HelloLib</Name>
</ProjectReference>
</ItemGroup>
</Project>
I assumed this was just a bug.
...but other people seem to use .NET core without too much trouble.
So, am I doing something wrong?
Or are the people 'using' .NET core just not using class libraries, because they don't work?
(edit: versions:
Microsoft .NET Core Shared Framework Host
Version : 1.1.0
Build : 928f77c4bc3f49d892459992fb6e1d5542cb5e86
$ dpkg -l |grep dotnet
ii dotnet-dev-1.0.3 1.0.3-1 amd64 .NET Core SDK 1.0.3
$ cat /etc/issue
Ubuntu 16.10 \n \l
After you do
dotnet add reference ../HelloLib/HelloLib.csproj
You must do an additional
dotnet restore
so the references will be properly tracked by the project. If you follow your same steps but add a dotnet restore after the dotnet add reference step it works fine.
Any time you change or update your refrences you must call dotnet restore afterward or change your dotnet build statement to be dotnet build /t:restore so it will do the restore before the build for you.

Categories

Resources