Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I define RequiredModules in a PowerShell module manifest (psd1)?

I'm trying to create a simple dependency between two PowerShell modules, but I'm getting the syntax or something wrong.

Module1.psd1:

@{
    RootModule        = 'Module1.psm1'
    ModuleVersion     = '1.0'
    GUID              = '11111111-1111-1111-1111-111111111111'
    Author            = 'uw'
    FunctionsToExport = @()
    CmdletsToExport   = @()
    VariablesToExport = '*'
    AliasesToExport   = @()
}

Module2.psd1:

@{
    RootModule        = 'Module2.psm1'
    ModuleVersion     = '1.0'
    GUID              = '22222222-2222-2222-2222-222222222222'
    Author            = 'uw'
    FunctionsToExport = @()
    CmdletsToExport   = @()
    VariablesToExport = '*'
    AliasesToExport   = @()
    RequiredModules   = @(
                          @{
                            ModuleName = "Module1"; 
                            ModuleVersion = "1.0"; 
                            Guid = "11111111-1111-1111-1111-111111111111"
                           }
                         )
}

The module manifest for Module2 defines that Module2 depends on Module1.

When running Test-ModuleManifest Module2.psd1, I get the following error:

Test-ModuleManifest : The specified RequiredModules entry 'Module1' in the module manifest 'Module2.psd1' is invalid. 
Try again after updating this entry with valid values.
like image 661
Uli Avatar asked Sep 14 '17 09:09

Uli


Video Answer


2 Answers

The issue turned out to be that Test-ModuleManifest expects all required modules to be installed on the local system.

So the fix is to install Module1, then validate the manifest of Module2.

See https://github.com/PowerShell/PowerShellGet/blob/90c5a3d4c8a2e698d38cfb5ef4b1c44d79180d66/Tests/PSGetPublishModule.Tests.ps1#L1470.

like image 169
Uli Avatar answered Nov 08 '22 21:11

Uli


Your question inspired this GitHub issue, which proposes introducing an option to restrict checking to whether a module is (syntactically) well-formed, as opposed to whether all modules referenced can be found and loaded.

The linked issue is primarily about a related bug: Test-ModuleManifest currently ignores dependencies on a specific version of a required module - any locally available version passes the test.

As an alternative to your own workaround (installing all required modules locally first), the following approach is a simpler stopgap:

# Run Test-ModuleManifest and collect any errors in variable $errs while
# suppressing immediate error output.
Test-ModuleManifest ./Module1.psd1 -ErrorVariable errs 2>$null

# Remove the errors relating to the 'RequiredModules' key, which we want to ignore.
$errs = $errs | ? { $_.ToString() -notmatch '\bRequiredModules\b' }

# Output any remaining errors.
$errs | % { Write-Error -ErrorRecord $_ }

# Determine success:
# Testing the manifest succeeded, if no errors other than the anticipated
# one relating to 'RequiredModules' occurred.
$ok = $errs.Count -eq 0
like image 23
mklement0 Avatar answered Nov 08 '22 19:11

mklement0