PoshCode / Configuration

A module to help other modules have settings
MIT License
176 stars 27 forks source link

Add-MetadataConverter is always called, even with no converters supplied #49

Closed fsackur closed 2 years ago

fsackur commented 2 years ago

This code block from #46 doesn't have the expected effect (https://github.com/PoshCode/Configuration/blob/main/Source/Header/param.ps1):

param(
    $Converters = @{},
    $EnterpriseData,
    $UserData,
    $MachineData
)

if ($Converters) {
    Add-MetadataConverter $Converters
}

Even empty hashtables are truthy, so the if branch is always followed.

I'm trying to use these modules without exporting to the global scope, and this call causes the import of the Metadata module in the global scope. Without this call, I can import the modules within my own module and not leak to the global scope.

Workaround is to import with ArgumentList:

Import-Module Configuration -ArgumentList @($null)
fsackur commented 2 years ago

For more context, this is how I'm importing Configuration in my own module. I don't expect this to be a supported scenario, of course!

# MyModule.psm1

#region Import 3rd-party dependencies
$DepRoot = Join-Path $PSScriptRoot Dependency
$ImportParams = @{
    PassThru    = $true
    ErrorAction = 'Stop'
}

$MetadataModule = Import-Module @ImportParams $DepRoot/Metadata/1.5.7/Metadata.psd1

# Pass ArgumentList to prevent call to Metadata function, which triggers global import
# Bug: https://github.com/PoshCode/Configuration/issues/49
# Fix: https://github.com/PoshCode/Configuration/issues/50
$ConfigurationModule = Import-Module @ImportParams $DepRoot/Configuration/1.5.1/Configuration.psd1 -ArgumentList @($null)

# Although both modules are imported into this scope, they can't see each other :-(
# Import Metadata inside Configuration's scope
# Without this, if Metadata is not in the PSModulePath, calls will fail
& $ConfigurationModule {Import-Module $args[0]} $MetadataModule
#endregion Import 3rd-party dependencies