Self-hosted Azure Devops build cant resolve packages - visual-studio

I have a Azure DevOps build pipeline that runs as expected on a hosted vs2017 agent, but fails on a self-hosted agent.
The error I get in the Visual Studio build step is:
C:\WINDOWS\TEMP\.NETStandard,Version=v2.0.AssemblyAttributes.cs(4,20): Error CS0400: The type or namespace name 'System' could not be found in the global namespace (are you missing an assembly reference?)
The two agents seems to run the same version of msbuild.
From the diagnostic output from msbuild I can see that the output from the ResolvePackageDependencies task contains a lot of packages where the ResolvedPath is empty, for instance:
runtime.native.System/4.3.0
Name=runtime.native.System
Path=runtime.native.system/4.3.0
ResolvedPath=
Type=package
Version=4.3.0
But the NuGet restore step seems to complete without problems.
Any suggestions for what I am missing?

I believe I had a similar issue. I ended up having to install the latest Nuget and then run Nuget on the solution including a NuGet.config file.
Add a nuget.config to your solution so it is part of your repo/pull. Mine is in the same directory as the solution file. Example below
Add a task "NuGet Tool Installer" - I install NuGet 4.4.1, just put 4.4.1 in the Version to install input.
Add a task "NuGet Installer" - Different from above. Version 0.* - I have not tried the other versions.
Set the Path to the solution. IE. $(Build.Repository.LocalPath)/Source/Sample.sln
Add the path to the Nuget config file. Example $(Build.Repository.LocalPath)/Source/nuget.config
Nuget.config contains how to get the packages. Add other locations if you get packages from other sources like a local folder or something.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<!--
Used to specify the default Sources for list, install and update.
See: nuget.exe help list
See: nuget.exe help install
See: nuget.exe help update
-->
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
</packageSources>
<packageRestore>
<!-- Allow NuGet to download missing packages -->
<add key="enabled" value="True" />
<!-- Automatically check for missing packages during build in Visual Studio -->
<add key="automatic" value="True" />
</packageRestore>
Your build task should run fine now and find all the packages.

Self-hosted Azure Devops build cant resolve packages
According to the error message, it seem nuget not restore the reference from SDK.
To resolve this issue, we need update our nuget.exe version to 4.0 and above.
In the NuGet tool installer we could specify the nuget.exe version:
As you comment above, it seems you have already use nuget installer, in this case, you can try to update Visual Studio to 15.3 and above on the build server. Because VS only adds proper support for .NET Core 2.0 SDK in version 15.3.
Finally, if your project/solution is .net core/standard you can use dotnet restore and then run dotnet build to compile your app.
Hope this helps.

Related

msbuild can't find references in projects.assets.json

I have a .net standard library project targeting .net standard 2.0 and I'm trying to build it in VSTS using MSBuild, the build server where the build agent is installed doesn't have internet access so I copied the dependencies to the C:\users\username.nuget\packages folder however MSBuild fails saying "Netsdk1064, Package microsoft.csharp, version 4.3.0 was not found".
I've tried copying the dependencies on the packages folder of the solution but still now working. I can't use Nuget restore on the build server so I was wondering if there is a way to redirect project.assets.json to look in the folder I created?
You need to check if package Netsdk1064, Package microsoft.csharp successfully copied in C:\users\username\.nuget\packages folder, since it errored this package was not found.
As workaround, you can create a nuget.config file and commit to your repo. And add the local directory where the all your packages reside under the packageSources element of nuget.config file. See below example.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="local" value="C:\users\username\.nuget\packages" />
</packageSources>
</configuration>
Check Package source sections of nuget.config for more information.
Then you can use nuget restore task to restore the packages. And Set nugetConfigPath point to the nuget.config created above.
- task: NuGetCommand#2
inputs:
command: 'restore'
restoreSolution: 'SmartFuel.sln'
feedsToUse: 'config'
nugetConfigPath: 'nuget.config'
If the nuget restore task failed to restore from the local package source. Please check the nuget version used in your pipeline. If the nuget version is 4.1.0 or older. You need to use NuGet Tool Installer task to use a higher versions of nuget. See this thread for more information.
Another possible workaround is to edit the .csproj file and manually point the referenced package to the .dll in local folder using hintpath. For example below:
<ItemGroup>
<Reference Include="DependPackage">
<HintPath>..\..\..\..\..\..\package\DependPackage.dll</HintPath>
</Reference>
</ItemGroup>

Nuget Packages are not found in nexus repo

I am using nexus OSS 3.15.2-01 and its new instance.
Issue we are facing is with nuget proxy repos, if i try it manually curl nuget org on nexus server it reaches the URL. but when i try to download it from VS or from cmd it says file not found.
I have created a seperate blob for nuget and new repo nuget_gallery, and provided the nuget org in proxy configuring it but its not working
From VS it says,
Severity Code Description Project File Line Suppression State
Error The feed ‘nexus prod [repo URL]’ lists package ‘Microsoft.AspNet.WebApi.Client.5.2.7’ but multiple attempts to download the nupkg have failed. The feed is either invalid or required packages were removed while the current operation was in progress. Verify the package exists on the feed and try again.
Unable to find package ‘Microsoft.AspNet.WebApi.Client.5.2.7’.
How ever the version is fetched in nexus if we browse the repo but even when we try to download it says file not found (0 bytes)
Bit late to the party, but I had this same problem. It seems to be down to lack of NuGet protocol 3 support in Nexus.
Workaround was to add protocolVersion="2" to my nuget.config like this;
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<!--To inherit the global NuGet package sources remove the <clear/> line below -->
<clear />
<add key="your-nexus-repo" value="http://your-nexus-repo/nexus/service/local/nuget/your-repo-name/" protocolVersion="2"/>
</packageSources>
</configuration>
I still had problems with dotnet core 3 packages, so also had to add back nuget.org (or remove <clear/> if nuget.org is in your global config)
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
You can try tips below to check if it helps:
1.Clean all the NuGet package cache by the command line: nuget locals all -clear.
2.Close all Visual Studio instance, then delete nuget.config file in the location: C:\Users\xxx\AppData\Roaming\NuGet\NuGet.Config, then re-open the Visual Studio to restore nuget packages.
3.Check if there is a firewall policy or other proxy settings that block the nuget installation package.
And please check if the issue only occurs when featching packages from Nexus or it also occurs when you download packages from nuget.org. Maybe you can get some help from this thread.

NuGet restoring PostSharp package before the build begins

I am using PostSharp and I have the following target description in my project file:
<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
<Error Condition="!Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://www.postsharp.net/links/nuget-restore." />
<Error Condition="Exists('..\..\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
</Target>
As far as I understand, this is added to the project when PostSharp is referenced through NuGet, and the error conditions check the following:
The first error condition breaks the build when PostSharp is not available (i.e. NuGet did not restore it successfully).
The second error condition breaks the build when PostSharp was successfully restored by NuGet on the last build but was therefore not included in the project, so therefore a rebuild is necessary.
BUT, if I have the following configuration in NuGet.Config and .csproj file, is the second error condition even necessary?
NuGet.Config file:
<configuration>
<packageRestore>
<!-- Allow NuGet to download missing packages -->
<add key="enabled" value="True" />
<!-- Automatically check for missing packages during build in Visual Studio -->
<add key="automatic" value="True" />
</packageRestore>
...
</configuration>
.csproj file:
<RestorePackages>true</RestorePackages>
As far as I understand, NuGet will then restore the missing packages BEFORE the build even starts. The second error condition will essentially break the build for no reason at all.
Note: I am using Visual Studio 2013 and NuGet 2.8.
It depends on how the restore is done and which version of NuGet you have installed. It looks like the error messages are trying to cover three scenarios:
Building without the MSBuild based package restore enabled (which is configured inside Visual Studio by right clicking the solution and selecting Enable Package restore).
Building outside of Visual Studio when the MSBuild based package restore is not enabled.
Building with Visual Studio using an old version of NuGet which does not support the automatic restore before a build.
If you are using the MSBuild based package restore then the restore will occur during the build and the PostSharp files will not be imported at this point so the $(PostSharp30Imported) will be empty and the second error message will be displayed. At least I suspect that is the case.
If you building from the command line and not using the MSBuild based package restore then you would see the first error message if the NuGet packages were missing.
If you are not using the MSBuild based package restore, and are building from within Visual Studio with a recent version of NuGet, then you are correct that the packages will be restored before anything is built at all. So the PostSharp imports should be available to MSBuild before it is even executed.
As PostSharp dlls are required during msbuild loading (so targets referencing this dlls are available during build) they must be available during final call to msbuild.
While in VS it is acceptable to click build twice, I was using PostSharp in CI environment, and requirement to call build on solution two times was frustrating (first build restore nugets but also failed build due to error).
I ended up with separate build steps:
Restore nuget Packages (this downloads PostSharp packages and return success code to environment):
NuGet.exe restore SolutionWithProjectsUsingPostSharp.sln
Build solution.
You need to edit the second error condition in the target in the csproj
<Target Name="EnsurePostSharpImported" BeforeTargets="BeforeBuild" Condition="'$(PostSharp30Imported)' == ''">
<Error Condition="!Exists('....\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://www.postsharp.net/links/nuget-restore." />
<Error Condition="Exists('....\packages\PostSharp.3.1.33\tools\PostSharp.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://www.postsharp.net/links/nuget-restore." />
</Target>>
I have answered in detail in a different post at SO
We are using 'old' MSBuild-Integrated package restore ( .nuget\NuGet.targets file is present) and normally do not store in source control packages, but rely on build to restore them for each build.
But for PostSharp on TeamCity build server I am getting error :
The build restored NuGet packages. Build the project again to include
these packages in the build.
The simplest way is to explicitly include in source control packages\PostSharp.VerXXX.
Alternatively solution could be migrating to automatic package restore,
as it was advised in Issue Using PostSharp 3.x with NuGet Auto Restore
Right click on the solution, Manage Nuget packages; and remove the packages you dont want .
This error also shows up , when trying to restore the packages from the web. Just connect your self to the internet and then try opening the project.
The errors went away for me on following the above steps.

NuGet for solution included in TFS Build

I am having problems with NuGet: it's actually driving me crazy!
I am trying to make a TFS build run for a solution that includes nuget.exe.
I don't have any issues running it from Visual Studio on my local workstation, but when I queue a new build in the TFS server, the build fails with these very known errors:
D:\<...SolutionPath...>.nuget\nuget.targets (88): Package restore is disabled by default. To give consent, open the Visual Studio Options dialog, click on Package Manager node and check 'Allow NuGet to download missing packages during build.' You can also give consent by setting the environment variable 'EnableNuGetPackageRestore' to 'true'.
D:\<...SolutionPath...>.nuget\nuget.targets (88): The command ""D:\<...SolutionPath...>.nuget\nuget.exe" install "D:\<...SolutionPath...>\packages.config" -source "" -RequireConsent -solutionDir "D:\<...SolutionPath...>\ "" exited with code 1.
The problem is: I already set the EnableNuGetPackageRestore to true in the TFS Build server, but it's still throwing the same error. I also tried running nuget.exe with the same parameters FROM a cmd windows in the same TFS server and I don't have any issues.
BTW, this is the content of my NuGet.config:
<configuration>
<solution>
<add key="disableSourceControlIntegration" value="true" />
</solution>
<packageRestore>
<add key="enabled" value="true" />
</packageRestore>
</configuration>
Any ideas?
Thanks in advance.
Please check that your TFS build service account is the same as the account you used to make the change. Also reboot the server to make sure the environment variable makes it the service.
Log on to the build server using the same account that executes the build.
Open Visual Studio.
Install the NuGet package manager in Visual Studio.
Restart Visual Studio.
Tools --> Options --> Package Manager --> General: tick 'Allow NuGet to download missing packages during build'.
Enable package restore in your solution.

How should I tell TeamCity's NuGet Installer build step to use both NuGet.org and the TeamCity provided packages source?

I'm having trouble with my NuGet Installer build step.
We're using both official NuGet.org packages and our own packages hosted on the TeamCity NuGet server. If I leave Packages Sources blank, then packages from nuget.org are found, but as soon as I specify %teamcity.nuget.feed.server% as the package source, then packages from nuget.org are not found.
I tried setting Packages Sources to include both, but it still isn't working for official nuget.org packages.
https://nuget.org/api/v2/
%teamcity.nuget.feed.server%
Is that not the right URL for the nuget.org package source? How do I tell it to use both sources?
I asked this on the JetBrains Developer discussion board, but haven't gotten any responses.
Had same problem, funny enough my Nuget sources were specified as
https://www.nuget.org/api/v2/
http://nugetserver/nuget
Adding a forward slash on the second url to make it http://mynugetserver/nuget/ fixed the problem.
Took me a while to figure out. Now my Nuget-installer build step is running fine.
Apparently the NuGet Installer build step is not even needed. I edited the .nuget/NuGet.targets file to include both paths and removed the NuGet Installer build step and it works now.
When originally setting up TeamCity for this solution, it didn't work without the NuGet Installer step, so I don't know what else I've done differently to make this work, but maybe the NuGet.targets file was the key all along.
The comment on this blog post pointed me in the right direction.
You can modify the NuGet.Config in AppData local folder for the user that TeamCity runs under and not modify each project's .targets file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<config>
<!--<add key="repositoryPath" value="J:\TeamCity7\buildAgent\work\my_shared_packages_dir" />-->
</config>
<packageRestore>
<add key="enabled" value="True" />
</packageRestore>
<packageSources>
<add key="NuGet official package source" value="https://nuget.org/api/v2/" />
<add key="MMG TeamCity Nuget Server" value="http://myteamcityserver/guestAuth/app/nuget/v1/FeedService.svc" />
</packageSources>
<activePackageSource>
<add key="All" value="(Aggregate source)" />
</activePackageSource>
</configuration>
The NuGet.exe inside of .nuget folder in each project will respect the configs set here first, then apply any "overrides" done at the .targets file.
Same problem here. I am using TeamCity v10, Nuget step is required (no .targets file in my solution). However, I used another approach to add the "extra" package source:
c:\BuildAgent\tools\NuGet.CommandLine.2.7.0\tools\Nuget.exe sources Add -Name TeamCity-feed -Source http://myteamcityserver/guestAuth/app/nuget/v1/FeedService.svc/
After that, I added a Nuget installer step and did not specify anything in the package source box in TeamCity, now both packages from public feed nuget.org and my internal feed could be restored without problems.

Resources