PSResourceGet icon indicating copy to clipboard operation
PSResourceGet copied to clipboard

Update-ModuleManifest parameter surface should mirror New-ModuleManifest

Open IISResetMe opened this issue 1 year ago • 3 comments

Summary of the new feature / enhancement

The current implementation of Update-ModuleManifest does not accept empty arguments for a number of parameters, meaning it can't be used to clear array-like values from existing manifests:

Update-ModuleManifest .\Test\Test.psd1 -RequiredModules @()

Instead of updating the manifest with RequiredModules = @(), this actually throws a validation error because -RequiredModules doesn't accept empty arguments.

This issue was initially raised here.

Proposed technical implementation details (optional)

Update the cmdlet in src/code/UpdateModuleManifest.cs.

Parameters that should be decorated with [AllowEmptyCollection]:

  • NestedModules
  • TypesToProcess
  • FormatsToProcess
  • ScriptsToProcess
  • RequiredAssemblies
  • FileList
  • ModuleList
  • FunctionsToExport
  • AliasesToExport
  • VariablesToExport
  • CmdletsToExport
  • DscResourcesToExport
  • CompatiblePSEditions

There's a number of additional parameters for which [AllowNull]/[AllowEmptyString] might be appropriate too.

IISResetMe avatar Oct 29 '24 15:10 IISResetMe

I think that the concerned parameters should allow for both $Null and an Empty Collection

  • In case the value is an (empty) collection, the value should convert to an (empty array) expression: @()
  • In case the value is $Null, the setting should reset to its original state:
    • For e.g. RequiredModules this means completely removing the concerned statement and replacing it with a comment: # RequiredModules = @()
    • For e.g. VariablesToExport assigning a wildcard (*), meaning replacing the statement with: VariablesToExport = '*'
function Update-ModuleManifest {
    param(
        [AllowNull()]
        [Object[]]$RequiredModules
    )
    
    if ($Null -eq $RequiredModules) {
        Write-Host 'Reset the setting to its original state'
    }
    else {
        Write-Host 'Convert the value to an expression'
        if (-not $RequiredModules) {
            Write-Host 'Even it is an empty collection'
        }
    }
}

PS C:\> Update-ModuleManifest -RequiredModules $Null
Reset the setting to its original state
PS C:\> Update-ModuleManifest -RequiredModules 1,2,3
Convert the value to an expression
PS C:\> Update-ModuleManifest -RequiredModules @()
Convert the value to an expression
Even it is an empty collection

iRon7 avatar Oct 30 '24 08:10 iRon7

Thanks for this issue-- feel free to open a pr

SydneyhSmith avatar Oct 31 '24 18:10 SydneyhSmith

Actually, it appears that there is a newer Update-PSModuleManifest (1.0.6) that does work as expected (except from the suggested $Null/Defaulting value):

Install-Module -Name Microsoft.PowerShell.PSResourceGet -RequiredVersion 1.0.6
Update-PSModuleManifest .\Test\Test.psd1 -RequiredModules @())

iRon7 avatar Nov 01 '24 12:11 iRon7