RamblingCookieMonster / PSDepend

PowerShell Dependency Handler
MIT License
285 stars 76 forks source link

Dependency of type 'PSGalleryNuget' without specifying version throw error #69

Open csandfeld opened 6 years ago

csandfeld commented 6 years ago

During testing with PSDepend today I ran into an issue when trying to install a module from a MyGet feed using the 'PSGalleryNuget' dependency type. The module was not installed, and "Unable to find package 'psdepend'. Existing packages must be restored before performing an install or update." was output to the console.

The problem only showed when not specifying a version in the build.requirements.psd1.

Steps to reproduce (on my system atleast):

Result: Module is not installed/downloaded, and this output is shown (as standard output): Unable to find package 'psdepend'. Existing packages must be restored before performing an install or update.

By debugging I found the parameters nuget.exe is called with, and calling nuget.exe directly with those parameters produced the same result: D:\Some\Custom\Path\psdepend\nuget.exe install TestMod99 -Source "https://some.myget.feed/.../api/v2/" -ExcludeVersion -NonInteractive -OutputDirectory "D:\Some\Custom\Path"

However by trial and error I found that when removing the '-ExcludeVersion' parameter it succeeds: D:\Some\Custom\Path\psdepend\nuget.exe install TestMod99 -Source "https://some.myget.feed/.../api/v2/" -NonInteractive -OutputDirectory "D:\Some\Custom\Path"

I don't know much about NuGet and it's parameters, so can't really say if it would generally be safe to remove that parameter when no version is specified (or even entirely).

build.requirements.psd1:

@{
    'TestMod99' = @{
        DependencyType = 'PSGalleryNuget'
        Source         = 'https://some.myget.feed/.../api/v2/'
    }
}

NuGet Version:

NuGet Version: 4.5.1.4879

Windows Version:

ProductName           Version Build UBR
-----------           ------- ----- ---
Windows 10 Enterprise 1709    16299 214

PowerShell Version:

Name                           Value
----                           -----
PSVersion                      5.1.16299.98
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.16299.98
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Host:

Name             : Visual Studio Code Host
Version          : 1.5.1
InstanceId       : 30c5f87d-9b2a-4cc6-8d86-0cf9703f8320
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : da-DK
CurrentUICulture : en-US
PrivateData      :
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace
RamblingCookieMonster commented 6 years ago

Ahh that's a pain. IIRC did this to avoid odd folder names:

ExcludeVersion | Installs the package to a folder named with only the package name and not the version number.

Wonder if anything changed in nuget that broke this? Will poke around

edit: yeah, without that switch we end up with unexpected folder names like poshbot.0.10.0 in the target

RamblingCookieMonster commented 6 years ago

Hi again!

Would you mind running with -verbose and including sanitized output? Curious to see what path it's following. Wonder if there are API differences with MyGet

RamblingCookieMonster commented 6 years ago

My guess is MyGet doesn't understand -version latest. Seems to line up with the error, and would explain why it happens when not specifying a version (latest is used in that case).

csandfeld commented 6 years ago

What really puzzled me was that even when running nuget.exe manually, trying to get TestMod99 downloaded, it would still complain about psdepend in the message. I even tried moving nuget.exe to a different folder. Guess this is a NuGet thing.

I will run with -verbose and post output later today :)

csandfeld commented 6 years ago

build_psdepend.ps1:

$tempPath             = Join-Path -Path $PSScriptRoot -ChildPath 'temp'
$tempModulePath       = Join-Path -Path $tempPath -ChildPath 'modules'
$installPSDependPath  = Join-Path -Path (Join-Path -Path (Join-Path -Path $PSScriptRoot -ChildPath 'build') -ChildPath 'helpers') -ChildPath 'Install-PSDepend.ps1'

### Getting dependencies

# Get PSDepened
& "$installPSDependPath" "$tempModulePath" -Verbose

Remove-Module -Name 'PSDepend' -Force -ErrorAction SilentlyContinue
Import-Module ('{0}\temp\modules\psdepend\PSDepend.psd1' -f $PSScriptRoot) -Force -Verbose

Invoke-PSDepend -Path "$PSScriptRoot\build\build.requirements.psd1" -Target $tempModulePath -Force -Install -Verbose

build.requirements.psd1 (sanitized):

@{
    'TestMod99' = @{
        DependencyType = 'PSGalleryNuget'
        Source     = 'https://some.myget.feed/.../api/v2/'
    }
}

Verbose output (sanitized):

D:\Some\Custom\Path> D:\Some\Custom\Path\build_psdepend.ps1

VERBOSE: GET https://dist.nuget.org/win-x86-commandline/latest/nuget.exe with 0-byte payload
VERBOSE: received 5066856-byte response of content type application/x-msdownload
Feeds used:
  https://www.powershellgallery.com/api/v2/

Installing package 'PSDepend' to 'D:\Some\Custom\Path\temp\modules'.
  GET https://www.powershellgallery.com/api/v2/FindPackagesById()?id='PSDepend'&semVerLevel=2.0.0
  OK https://www.powershellgallery.com/api/v2/FindPackagesById()?id='PSDepend'&semVerLevel=2.0.0 150ms
  GET https://www.powershellgallery.com/api/v2/FindPackagesById?id='PSDepend'&$skiptoken='PSDepend','0.1.60'
  OK https://www.powershellgallery.com/api/v2/FindPackagesById?id='PSDepend'&$skiptoken='PSDepend','0.1.60' 118ms

Attempting to gather dependency information for package 'PSDepend.0.1.63' with respect to project 'D:\Some\Custom\Path\temp\modules', targeting 'Any,Version=v0.0'
Gathering dependency information took 42,4 ms
Attempting to resolve dependencies for package 'PSDepend.0.1.63' with DependencyBehavior 'Lowest'
Resolving dependency information took 0 ms
Resolving actions to install package 'PSDepend.0.1.63'
Resolved actions to install package 'PSDepend.0.1.63'
Retrieving package 'PSDepend 0.1.63' from 'https://www.powershellgallery.com/api/v2/'.
Adding package 'psdepend.0.1.63' to folder 'D:\Some\Custom\Path\temp\modules'
Added package 'psdepend.0.1.63' to folder 'D:\Some\Custom\Path\temp\modules'
Successfully installed 'PSDepend 0.1.63' to D:\Some\Custom\Path\temp\modules
Executing nuget actions took 260,1 ms
VERBOSE: Loading module from path 'D:\Some\Custom\Path\temp\modules\psdepend\PSDepend.psd1'.
VERBOSE: Loading 'FormatsToProcess' from path 'D:\Some\Custom\Path\temp\modules\psdepend\PSDepend.Format.ps1xml'.
VERBOSE: Loading module from path 'D:\Some\Custom\Path\temp\modules\psdepend\PSDepend.psm1'.
VERBOSE: Importing function 'Get-Dependency'.
VERBOSE: Importing function 'Get-PSDependScript'.
VERBOSE: Importing function 'Get-PSDependType'.
VERBOSE: Importing function 'Import-Dependency'.
VERBOSE: Importing function 'Install-Dependency'.
VERBOSE: Importing function 'Invoke-DependencyScript'.
VERBOSE: Importing function 'Invoke-PSDepend'.
VERBOSE: Importing function 'Test-Dependency'.
VERBOSE: Running Invoke-PSDepend with ParameterSetName 'installimport-file', PSDependAction Install, and params:
Key     Value
---     -----
Path    {D:\Some\Custom\Path\build\build.requirements.psd1}
Target  D:\Some\Custom\Path\temp\modules
Force   True
Install True
Verbose True

VERBOSE: Working with [1] dependency files from [D:\Some\Custom\Path\build\build.requirements.psd1]:
D:\Some\Custom\Path\build\build.requirements.psd1

VERBOSE: Overriding Dependency target [] with target parameter value [D:\Some\Custom\Path\temp\modules]
VERBOSE: Running Invoke-DependencyScript with ParameterSetName 'Map' and params:
Key              Value
---              -----
PSDependAction   {Install}
PSDependTypePath D:\Some\Custom\Path\temp\modules\psdepend\PSDependMap.psd1
Dependency       @{DependencyFile=D:\Some\Custom\Path\build\build.requirements.psd1; DependencyName=TestMod99; DependencyType=PSGalleryNuget; Name=; Version=; Parameters=; Source=h...

VERBOSE: Dependencies:

DependencyFile  : D:\Some\Custom\Path\build\build.requirements.psd1
DependencyName  : TestMod99
DependencyType  : PSGalleryNuget
Name            :
Version         :
Parameters      :
Source          : https://some.myget.feed/.../api/v2/
Target          : D:\Some\Custom\Path\temp\modules
AddToPath       :
Tags            :
DependsOn       :
PreScripts      :
PostScripts     :
PSDependOptions :
Raw             : {Source, DependencyType}

VERBOSE: Searching for D:\Some\Custom\Path\temp\modules\psdepend\PSDependScripts\PSGalleryNuget.ps1
VERBOSE: Searching for PSGalleryNuget.ps1
VERBOSE: Get-Parameters for D:\Some\Custom\Path\temp\modules\psdepend\PSDependScripts\PSGalleryNuget.ps1\PSGalleryNuget.ps1
VERBOSE: Found parameters [Dependency Force Import PSDependAction]
VERBOSE: Invoking 'D:\Some\Custom\Path\temp\modules\psdepend\PSDependScripts\PSGalleryNuget.ps1' with parameters
Name                           Value
----                           -----
Dependency                     @{DependencyFile=D:\Some\Custom\Path\build\build.requirements.psd1; DependencyName=TestMod99; DependencyType=PSGalleryNuget; Name=; Version=; Paramet...
PSDependAction                 {Install}

VERBOSE: Getting dependency [TestMod99] from Nuget source [https://some.myget.feed/.../api/v2/]
VERBOSE: Saving [TestMod99] with path [D:\Some\Custom\Path\temp\modules]
VERBOSE: Running nuget.exe with arguments install; TestMod99; -Source; https://some.myget.feed/.../api/v2/; -ExcludeVersion; -NonInteractive; -OutputDirectory; D:\Some\Custom\Path\temp\modules
Unable to find package 'psdepend'. Existing packages must be restored before performing an install or update.
VERBOSE: Feeds used:
  https://some.myget.feed/.../api/v2/

Installing package 'TestMod99' to 'D:\Some\Custom\Path\temp\modules'.
  GET https://some.myget.feed/.../api/v2/FindPackagesById()?id='TestMod99'&semVerLevel=2.0.0
  OK https://some.myget.feed/.../api/v2/FindPackagesById()?id='TestMod99'&semVerLevel=2.0.0 227ms

Attempting to gather dependency information for package 'TestMod99.0.0.3' with respect to project 'D:\Some\Custom\Path\temp\modules', targeting 'Any,Version=v0.0'
Gathering dependency information took 749,39 ms
Attempting to resolve dependencies for package 'TestMod99.0.0.3' with DependencyBehavior 'Lowest'

D:\Some\Custom\Path>