Skip to content

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

Closed

Description

@fsackur

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)

Activity

  1. fsackur commented on Aug 29, 2022

    @fsackur
    ContributorAuthor

    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
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions