msbuild not picking packages from restored location - yaml

I would like to restore my nugets one level up. My repo directory points to say for ex. c:\repo\s and my solutin is in c:\repo\s\src, when I am restoring packages with nuget restore , its restoring the packages at C:\repo\s\src\packages and I would like that to be C:\repo\s\packages. Appreciate your help.
I have the below nuget.config file in C:\repo\s\src directory.
$<configuration>
<config>
<add key="repositoryPath" value="..\..\packages" />
</config>
</configuration>
My Yaml job looks like this
$steps:
- task: NuGetToolInstaller#0
displayName: 'Use NuGet 4.3.0'
- task: NuGetCommand#2
displayName: 'NuGet restore'
inputs:
restoreSolution: src/myproject.sln
vstsFeed: '4448b1e2-8ac8-45ef-870c-1ebab90f3348'
restoreDirectory: '$(Build.SourcesDirectory)'
- task: VSBuild#1
displayName: 'Build solution src/myproject.sln'
inputs:
solution: src/myproject.sln
vsVersion: 15.0
msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true'
platform: '$(BuildPlatform)'
configuration: '$(BuildConfiguration)'

msbuild not picking packages from restored location
Whether you use the nuget.config file or you directly specify restoreDirectory: '$(Build.SourcesDirectory)' in the nuget restore task, nuget will restore the package to the folder C:\repo\s\packages.
However, NuGet Restore only restores packages to the restore directory, but does not modify your project file.
When we add the nuget packages to the project, it will add following code in the project file to specify the dll location:
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=12.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.12.0.3-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
</ItemGroup>
There is node HintPath to specify the location of the dll file.
When we use nuget.config or restoreDirectory: '$(Build.SourcesDirectory)' to change the location of the package restore, MSBuild will not pick up the package/dll based on the HintPath. The correct HintPath should be:
<HintPath>..\..\packages\Newtonsoft.Json.12.0.3-beta1\lib\net45\Newtonsoft.Json.dll</HintPath>
That the reason why the msbuild not picking packages from restored location.
To resolve this issue, you need use the NuGet command line in the Package Manager Console (On local VS):
Update-Package -reinstall
to force reinstall the package references into project, it will update the HintPath. Upload the changes file to the Azure devops and build it.
Hope this helps.

Related

include the XML documentation from a NuGet package

I have a NuGet package with an xml documentation file (doc.xml).
I have installed the NuGet package in my project.
I know want to add the NuGet documentation file doc.xml to my solution.
I am running .net core 3.1 but I have no idea how this can be achieved.
Thank you!
If your nuget project is net standard or net core, please check these steps:
1) create a file called <package_id>.props file under a folder called build on your project.
Note that, you should make sure that the your package_id of your nuget project is the same as the .props file, otherwise, it will not work. See this link's description.
In my side, my nuget package called test.1.0.0.nupkg, so I should rename the file as test.props file.
2) please add these content in test.props file.
<Project>
<Target Name="CopyFilesToProject" BeforeTargets="Build">
<ItemGroup>
<SourceScripts Include="$(MSBuildThisFileDirectory)..\File\*.*"/>
</ItemGroup>
<Copy
SourceFiles="#(SourceScripts)"
DestinationFolder="$(ProjectDir)"
/>
</Target>
</Project>
The propose of this target is to copy the xml file from the File folder of the nupkg into the target project's folder when you install this nuget into the main project.
3) add these under xxx.csproj file:
<ItemGroup>
<None Include="bin\Debug\netcoreapp3.1\test.xml(the path of the xml file in your nuget project)" Pack="true" PackagePath="File"></None>
<None Include="build\test.props(the path of the test.props file in your nuget project)" Pack="true" PackagePath="build"></None>
</ItemGroup>
4) Then, when you pack your project, the structure should be this:
Before you install this new version of nuget package, you should clean nuget caches first or just delete all cache files under C:\Users\xxx(current user)\.nuget\packages to remove the old ones in case you still install the old one.
After that, rebuild your main project to execute the target and you will see the xml document file exist under the main project folder.
In addition, there is a similar issue you can refer to and if you use the net framework project, the link also provide the method.
===========================
Update 1
If you want to copy the file into bin\Release or bin\Debug, you should modify step 2, change to use this in the .props file:
<Copy
SourceFiles="#(SourceScripts)"
DestinationFolder="$(ProjectDir)$(OutputPath)"
/>
or just
<Copy
SourceFiles="#(SourceScripts)"
DestinationFolder="$(OutDir)"
/>
as you want.
Before you install this new version, you should first delete nuget caches under C:\Users\xxx(current user)\.nuget\packages.

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>

Azure Devops Build Fail when using GeneratePathProperty true for PackageReference

We have set up a Project in Visual Studio where we are using NuGet Packages references. For one of the NuGet Packages we are setting the GeneratePathProperty to true, so that we can copy the files from the NuGet package location to the Output bin folder of our Project
He have configured the .csproj file as below:
<PackageReference Include="ilmerge" GeneratePathProperty="true">
<Version>3.0.29</Version>
</PackageReference>
<None Include="$(Pkgilmerge)\tools\net452\ILMerge.exe">
<Link>ILMerge.exe</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="$(Pkgilmerge)\tools\net452\System.Compiler.dll">
<Link>System.Compiler.dll</Link>
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
This works perfectly fine locally (using Visual Studio 2019) - project compiles and files are copied to the bin folder, however in our Build Pipeline using the windows-2019 hosted agent (that should also have VS 2019 as per the documentation https://github.com/actions/virtual-environments/blob/master/images/win/Windows2019-Readme.md) this fails during the Build Task:
[error]C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4601,5): Error MSB3030: Could not copy the file 'd:\tools\net452\ILMerge.exe' because it was not found.
[error]C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\MSBuild\Current\Bin\Microsoft.Common.CurrentVersion.targets(4601,5): Error MSB3030: Could not copy the file 'd:\tools\net452\System.Compiler.dll' because it was not found.
The Build Definition has following tasks
steps:
- task: NuGetToolInstaller#0
- task: NuGetCommand#2
displayName: 'Restore Nugets'
inputs:
restoreSolution: '$(solutions)'
vstsFeed: 'Project-Packages'
- task: VSBuild#1
inputs:
solution: '$(solutions)'
platform: 'Any CPU'
configuration: 'Release'
logProjectEvents: false
- task: PublishBuildArtifacts#1
inputs:
pathtoPublish: '$(Build.StagingDirectory)'
artifactName: $(pluginName)
condition: always()
Any ideas what could be the issue or what we need to change in order for the build in Azure DevOps to work?
Later Edit: I added some logging
<Target Name="¨ReMerge" AfterTargets="ILRepacker" >
<Message Text="Remerging Assemblies using ILMerge from $(Pkgilmerge) and setting AssemblyVersion = 2.0.0.0" Importance="High" />
<Exec Command="dir $(Pkgilmerge)"></Exec>
<Exec Command="$(Pkgilmerge)\tools\net452\ILMerge.exe /ver:2.0.0.0 /out:$(TargetDir)$(TargetName)Merged.dll /keyfile:$(ProjectDir)Zurich.Zkp.Key.snk $(TargetDir)$(TargetName)MergedTemp.dll"></Exec>
</Target>
And it seems that in AzureDevops the $(Pkgilmerge) is empty
Thank you
The above error was caused by an old version(4.x) Nuget being used to restore your solution.
You need to specify the Nuget version to the newest for your NuGetToolInstaller task, if unspecified, a version will be chosen automatically.
Check below example to specify NuGetToolInstaller task to use 5.4.x version of Nuget.
- task: NuGetToolInstaller#0
inputs:
versionSpec: 5.4.x

Is it possible to publish a Xamarin project to a private nuget package feed in VSTS (DevOps)

A Xamarain project was created and a Devops (VSTS) pipeline was created to build and publish the project to a private nuget feed. Everything builds great, but the step that would Pack the nuget package fails.
1) My first attempt was to use the "Macos-latest" to do an msbuild#1 on the solution. The Nuget installer uses a nuspec file to do the packing. An error show's up in the packing step
2) I then tried to do a VSBuild#1, followed by a DotNetCoreClI#2 without success.
3) I also attempted to split the 3 projects (iOS, Android, UWP) into 3 separated jobs but they failed in the packing step too.
4) I tried various techniques in packing, nuspec files, the csproj file without success.
My YAML file looks like this:
'
- task: NuGetToolInstaller#1
displayName: 'Install nuget.exe 4.4.1'
inputs:
versionSpec: 4.4.1'
- task: NuGetCommand#2
displayName: "restore the ble solution"
inputs:
command: 'restore'
restoreSolution: '**/*.sln'
feedsToUse: 'select'
vstsFeed: '$(packageFeedName)'
- task: MSBuild#1
displayName: 'Build BLE solution'
inputs:
solution: "**/*.sln"
configuration: '$(buildConfiguration)'
msbuildArguments: '/p:OutputPath=$(outputDirectory) /p:JavaSdkDirectory="$(JAVA_HOME)/"'
- task: NuGetCommand#2
displayName: "pack nuget"
inputs:
command: pack
packagesToPack: './myproject.nuspec'
packDestination: '$(Build.ArtifactStagingDirectory)'
versioningScheme: byEnvVar
versionEnvVar: 'nugetVersion'
includeSymbols: true
'
It always comes down to the same pathing problem with I use a nuspec file.
Attempting to build package from 'BLE.nuspec'.
[error]The nuget command failed with exit code(1) and error(Could not find a part of the path /Users/vsts/agent/2.155.1/work/1/s/Plugin.BLE/bin/Release/netstandard2.0.
System.IO.DirectoryNotFoundException: Could not find a part of the path /Users/vsts/agent/2.155.1/work/1/s/Plugin.BLE/bin/Release/netstandard2.0.
When I use a **/*.csproj for the pack, I get:
Build FAILED.
d:\a\1\s\Plugin.BLE.Android\Plugin.BLE.Android.csproj" (pack target) (1:2) ->d:\a\1\s\Plugin.BLE.Android\Plugin.BLE.Android.csproj(94,3): error MSB4019: The imported project "C:\Program Files\dotnet\sdk\2.2.105\Xamarin\Android\Xamarin.Android.CSharp.targets" was not found.
Confirm that the path in the <Import> declaration is correct, and that the file exists on disk.
Is it possible to publish a Xamarin project to a private nuget package feed in VSTS (DevOps)
The answer is yes. It depends on how many packages you want to generate.
If you want to create one nuget package for 3 projects (iOS, Android, UWP), you should use the .nuspec file.
The path problem is because you are not set the correct path in the .nuspec file. The .nuspec file should be set at the folder that's one level below where the .sln file is. The path to the dll is the relative path where the .nupsec file is stored.
So, the path should be like:
<files>
<!-- Cross-platform reference assemblies -->
<file src="XamarinDemo\bin\Release\XamarinDemo.dll" target="lib\netstandard2.0\XamarinDemo.dll" />
<file src="XamarinDemo\bin\Release\XamarinDemo.xml" target="lib\netstandard2.0\XamarinDemo.xml" />
<!-- iOS reference assemblies -->
<file src="XamarinDemo.iOS\bin\Release\XamarinDemo.dll" target="lib\Xamarin.iOS10\XamarinDemo.dll" />
<file src="XamarinDemo.iOS\bin\Release\XamarinDemo.xml" target="lib\Xamarin.iOS10\XamarinDemo.xml" />
<!-- Android reference assemblies -->
<file src="XamarinDemo.Android\bin\Release\XamarinDemo.dll" target="lib\MonoAndroid10\XamarinDemo.dll" />
<file src="XamarinDemo.Android\bin\Release\XamarinDemo.xml" target="lib\MonoAndroid10\XamarinDemo.xml" />
<!-- UWP reference assemblies -->
<file src="XamarinDemo.UWP\bin\Release\XamarinDemo.dll" target="lib\UAP10\XamarinDemo.dll" />
<file src="XamarinDemo.UWP\bin\Release\XamarinDemo.xml" target="lib\UAP10\XamarinDemo.xml" />
</files>
Check the document Create packages for Xamarin with Visual Studio 2015 for some details.
If you want to create three nuget package for each platform, you can use the a **/*.csproj for the pack.
The reason for your issue is that you need install the .NET core SDK before you build the project/solution:
- task: UseDotNet#2
displayName: 'Use .Net Core sdk 2.2.105'
inputs:
version: 2.2.105
Hope this helps.
So, my problem wasn't in my nuspec file; the real problem comes down to using the vmImage : "macos-latest", which is required if you want to build an iOS project (iOS won't build under the agents 2019, or 2017, and you must use MSBuild (not VSBuild)
The macos-latest image has several issues (which I'll refer to as the iOS agent)
The nuspec package for System.Threading.Task.Extensions (STTE) isn't included when you bring in another nuget package that has a dependency on it. If the package is in the 'Standard .net 2.0 project", then the STTE package must be included in the standard project and the ios project; this only occurs in the ios agent.
The variable I used for the solution path $(solution) did not expand to myproj.sln; the logged showed the path as /$($solution) and the project did not build, no warning or errors that zero projects were built; the problem did not occur in any other agent type that I tested.
The ios agent's Xamarin sdk is woefully out of date and has to be set.
yaml required:
variables:
mono: 5_18_1
xamarinSDK: 12_8_0_2
- task: Bash#3
displayName: "Set xamarinSDK to version $(mono)"
inputs:
targetType: 'inline'
script: /bin/bash -c "sudo $AGENT_HOMEDIRECTORY/scripts/select-xamarin-sdk.sh $(mono)"
The technique used to figure this out was to split a Xamarin project into 4 separate projects and publish them as separate projects:
.Net Standard 2.0
UWP
Android
iOS
Every project but the iOS one worked using a VSBuild and the 2019 agent; it was then a process of elimination on the ios agent.

DotNet CLI publish command to include packages from other NuGet source

For one of my .Net Core project, I have a BuldProject.cmd CLI which publish the project. CLI Command I used is this
dotnet publish --framework %DotNetCoreVersionToTarget% --configuration %ConfigurationModeToUse% --force --verbosity normal
Above command build the project fine using Teamcity build server.
Recently I added a DLL to my project not from
nuget.org
but from my some other source say
nuget.mypack.com/nuget
csproj file has an entry for new DLL
<ItemGroup>
<PackageReference Include="TestCoreAPI" Version="1.0.1" />
</ItemGroup>
Now when I deploy the project using TeamCity I get the following error
error NU1101: Unable to find package TestCoreAPI. No packages exist with this id in source(s): nuget.org
The problem I see, it always looks into nuget.org for packages.
How can I modify my BuldProject.cmd CLI to look for other NuGet sources as well?

Resources