cake-build / cake-build/resources

NuGet 4.4.0 Error Code 1 Packages Already Installed

Open
#41 13 comments 0 reactions 0 assignees View on GitHub
Dominant language
PowerShell
Stars
57
Forks
72
PR merge metrics
No merged PRs in 30d

Description

@thnk2wn commented on [Tue Oct 24 2017](https://github.com/cake-build/cake/issues/1892)

### What You Are Seeing?
Having nuget.exe 4.4.0 in Cake/Tools with packages already installed results in error code 1 "all packages are already installed". This causes build.ps1 to fail early restoring tools/addins and stops the build process. Generally this isn't a problem on the build server with fresh downloads and builds but it happens regularly for local builds. Perhaps LASTEXITCODE changes with NuGet 4.4.0. This version of NuGet was needed for some .NET Standard 2.0 issues.

### What is Expected?
Expecting Cake's build.ps1 to detect such "error" (warning at best?) and to proceed regardless of whether packages are already installed.

### What version of Cake are you using?
0.23.0

### Are you running on a 32 or 64 bit system?
64

### What environment are you running on? Windows? Linux? Mac?
Windows 10 Fall Creators Update.

### Are you running on a CI Server? If so, which one?
Both CI and local. CI is TeamCity 9.x.

### How Did You Get This To Happen? (Steps to Reproduce)
Download NuGet 4.4.0 from https://www.nuget.org/downloads (it appears to be preview currently).
Place this download in Cake/tools.
Invoke the build once via build.ps1, all is fine.
Invoke the build a 2nd time with the packages already installed.
Receive "error" 1 that packages are already installed.

Some more context in this post:
https://geoffhudik.com/tech/2017/10/24/build-blues-with-net-standard-2-0/

### Output Log

C:\source\projectname\cake [feature/Win10FallCreatorsUpdate ≡]> .\build.ps1 -Verbosity diagnostic

Preparing to run build script...
Feeds used:
C:\Users\geoffh\.nuget\packages\
https://api.nuget.org/v3/index.json
https://www.myget.org/F/msbuildsdkextras/api/v3/index.json
C:\Program Files (x86)\Microsoft SDKs\NuGetPackages\

An error occured while restoring NuGet tools.
At C:\source\projectname\cake\build.ps1:173 char:9
+ Throw "An error occured while restoring NuGet tools."
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (An error occure...ng NuGet tools.:String) [], RuntimeException
+ FullyQualifiedErrorId : An error occured while restoring NuGet tools.

### Possible Issues Here
1) Cake should perhaps treat exit code 1 with "packages already installed" as not an error that stops the script. Perhaps this behavior changed with Nuget 4.4.0.

2) The error message indicates neither the exit code nor the nuget output so it says nothing useful to help the situation.

3) The word "occured" is misspelled - should be "occurred".

4) When an error happens here, Pop-Location isn't executed so it leaves things at a different location than where things were when the script started, leading to possible issues later.

5) Ideally there'd be a way to specify the NuGet version (like when referencing Nuget package tools/addins) of NuGet.exe itself instead of having to place a version here and commit to source control. I don't think the Cake build script could even download NuGet as it would have to happen before #tool or #addin and I think those have to be first.

### Workaround Changes Made (build.ps1)

```powershell
if(-Not $SkipToolPackageRestore.IsPresent) {
Push-Location
Set-Location $TOOLS_DIR

# Check for changes in packages.config and remove installed tools if true.
[string] $md5Hash = MD5HashFile($PACKAGES_CONFIG)
if((!(Test-Path $PACKAGES_CONFIG_MD5)) -Or
($md5Hash -ne (Get-Content $PACKAGES_CONFIG_MD5 ))) {
Write-Verbose -Message "Missing or changed package.config hash..."
Remove-Item * -Recurse -Exclude packages.config,nuget.exe
}

Write-Verbose -Message "Restoring tools from NuGet..."
$NuGetOutput = Invoke-Expression "&`"$NUGET_EXE`" install -ExcludeVersion -OutputDirectory `"$TOOLS_DIR`""

if ($LASTEXITCODE -ne 0) {
Write-Warning ($NuGetOutput | out-string)

if ($LASTEXITCODE -eq 1 -and $NuGetOutput -match ' are already installed') {
Write-Verbose -Message 'Packages already installed, continuing.'
}
else {
Pop-Location
throw ("NuGet returned error {0} restoring NuGet tools." -f $LASTEXITCODE)
}
}
else
{
$md5Hash | Out-File $PACKAGES_CONFIG_MD5 -Encoding "ASCII"
Write-Verbose -Message ($NuGetOutput | out-string)
}

Pop-Location
}
```

---

@ahoefling commented on [Sat Nov 04 2017](https://github.com/cake-build/cake/issues/1892#issuecomment-341875440)

I did some investigation on this and I came to the same conclusion, it appears that Cake is using NuGet version 4.3 and there have been changes to NuGet 4.4 that directly apply to the latest windows fall creators update and netstandard builds. If I run the command `nuget restore project.sln` with NuGet 4.3 I get errors but if I run the same command with NuGet 4.4 the restore runs without issue.

**Environment**

| Name | Version |
|--------------------------------------|--------------|
| Windows 10 Fall Creators Update | 16299 |
| Xamarin.Forms | 2.4.018342 |
| UWP | 10.0.16299 |
| Cake | 0.23.0 |

**Errors**
This error is displayed in the build log and as @thnk2wn mentioned in the shared link it is a problem with NuGet 4.3 which I also reproduced by running the following command

* `nuget restore project.sln`

```
Errors in C:\source\TabStrip\CarouselView\CarouselView.FormsPlugin.UWP\CarouselView.FormsPlugin.UWP.csproj
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299). Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-arm. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-arm-aot. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-x64. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-x64-aot. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-x86. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-x86-aot. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
Errors in C:\source\TabStrip\src\TabStrip.FormsPlugin.UWP\TabStrip.FormsPlugin.UWP.csproj
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299). Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-arm. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-arm-aot. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-x64. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-x64-aot. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-x86. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
NU1201: Project CarouselView.FormsPlugin.Abstractions is not compatible with uap10.0.16299 (UAP,Version=v10.0.16299) / win10-x86-aot. Project CarouselView.FormsPlugin.Abstractions supports: netstandard2.0 (.NETStandard,Version=v2.0)
```

**Attempted Solution**
I first attempted cloning the repository to see how hard it would be to update the cake to use netstandard 2.0 and quickly learned there are a lot of dependencies and it will require more than an hour of time.

**Work Around**
I tried updating the powershell script with the provided script above and that did not work because the nuget restore referenced from cake is still using NuGet 4.3. I ended up making modifications to the powershell script to use the following logic:

1. Check if `nuget` is a version < 4.4
2. if true download version 4.4
3. prior to running the cake build execute `nuget restore project.sln`
4. execute cake build

I understand this is a hard coded hack but it solves my problem for now and hopefully this is a suitable workaround for others that run into this problem while we wait for an updated version.

**Powershell Updates**
Variables:
```
$TOOLS_DIR = Join-Path $PSScriptRoot "tools"
$ADDINS_DIR = Join-Path $TOOLS_DIR "Addins"
$MODULES_DIR = Join-Path $TOOLS_DIR "Modules"
$NUGET_EXE = Join-Path $TOOLS_DIR "nuget.exe"
$CAKE_EXE = Join-Path $TOOLS_DIR "Cake/Cake.exe"
$NUGET_URL = "https://dist.nuget.org/win-x86-commandline/v4.4.0/nuget.exe"
$PACKAGES_CONFIG = Join-Path $TOOLS_DIR "packages.config"
$PACKAGES_CONFIG_MD5 = Join-Path $TOOLS_DIR "packages.config.md5sum"
$ADDINS_PACKAGES_CONFIG = Join-Path $ADDINS_DIR "packages.config"
$MODULES_PACKAGES_CONFIG = Join-Path $MODULES_DIR "packages.config"
```
Note the download path for nuget is changed

Nuget:
```
# Try download NuGet.exe if not exists
if(!(Test-Path $NUGET_EXE))
{
Write-Verbose -Message "Downloading NuGet.exe..."
try {
$wc = GetProxyEnabledWebClient
$wc.DownloadFile($NUGET_URL, $NUGET_EXE)
} catch {
Throw "Could not download NuGet.exe."
}
}
else
{
# Check if the current version of nuget is 4.4 if not download it
$version = (nuget.exe | Select-Object -First 1).Split(':')[1].Trim()
if(!($version.StartsWith("4.4")))
{
Write-Verbose -Message "Downloading NuGet.exe..."
try {
$wc = GetProxyEnabledWebClient
$wc.DownloadFile($NUGET_URL, $NUGET_EXE)
} catch {
Throw "Could not download NuGet.exe."
}
}
}
```

then at the bottom of the `build.ps1` script should look like this to force a `nuget restore`

Execute `NuGet` restore and `Cake` Build
```
# run nuget command
nuget restore Project.sln

# Start Cake
Write-Host "Running build script..."
&$CAKE_EXE $cakeArguments
exit $LASTEXITCODE
```

Just to reiterate - my solution is intended to get my projects by until cake supports NuGet 4.4, I understand it is not the most elegant way to solve this problem. If you choose to use the updates provided there may be side effects with your project.

---

@gep13 commented on [Sat Nov 04 2017](https://github.com/cake-build/cake/issues/1892#issuecomment-341886476)

@thnk2wn @ahoefling since these issues seems to be related to the bootstrapper that is provided with Cake, I am going to close this issue out, and move it to the resources repo, which is where the bootstrapper lives.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in build.ps1 at the NuGet tools restore around the reported line 173, and reproduce the second build with NuGet 4.4. Check how the script handles the exit code and captured NuGet output when packages are already installed; done means that case proceeds without hiding genuine restore failures and the reported diagnostic issues are addressed.

Written by the indexing model from the issue text.

Assessment

Tech stack
powershell
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.