Skip to content

Is there a way to avert the diamond dependency problem for modules that use .Net types? #5504

Closed

Description

@alx9r

It seems like certain things can only be defined once per PowerShell session. Public enums named in calls to Add-Type are one such example. In the repro below, a .Net enum of a particular name could only be defined once even though the two definitions are in separate modules. Working around such naming collisions amongst unrelated modules is usually a one-time event. However, the same problem manifests when more than one version of the same module is required to exist in a single session. This occurs every time a .Net type in a low-level module changes and the mid-level modules aren't updated simultaneously. I've been running into this problem in PowerShell with increasing frequency as I incorporate more third-party libraries and as my code has become more modular.

As far as I've read this problem is called "the diamond dependency problem" (video). The repro below demonstrates the canonical scenario described in the video in PowerShell.

There seems to be some signs that C# projects can load multiple versions of the same assembly thereby averting the diamond dependency problem (e.g. stackoverflow question). Is there a similar strategy that works in PowerShell?

Steps to reproduce

New-Module libutil_v1 {
    Add-Type -TypeDefinition 'public enum E {a,b,c}'
    function f {
        param
        (            
            [e]
            $E
        )
    }
} | Import-Module

New-Module libutil_v2 {
    Add-Type -TypeDefinition 'public enum E {a,b,c,d}'
    function f {
        param
        (            
            [e]
            $E
        )
    }
} | Import-Module

New-Module liba {
    Get-Module libutil_v1 | Import-Module

    function g_a {
        f -E c
    }
} | Import-Module

New-Module libb {
    Get-Module libutil_v2 | Import-Module

    function g_b {
        f -E d
    }
} | Import-Module


g_a
g_b

Expected behavior

No exception thrown.

Actual behavior

Add-Type : Cannot add type. The type name 'E' already exists.
At C:\users\un1\Desktop\test.ps1:13 char:5
+     Add-Type -TypeDefinition 'public enum E {a,b,c,d}'
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (E:String) [Add-Type], Excepti
   on
    + FullyQualifiedErrorId : TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Command
   s.AddTypeCommand

f : Cannot process argument transformation on parameter 'E'. Cannot convert
value "d" to type "E". Error: "Unable to match the identifier name d to a
valid enumerator name. Specify one of the following enumerator names and try
again:
a, b, c"
At C:\users\un1\Desktop\test.ps1:35 char:14
+         f -E d
+              ~
    + CategoryInfo          : InvalidData: (:) [f], ParameterBindingArgumentTr
   ansformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,f

Environment data

> $PSVersionTable

Name                           Value                                           
----                           -----                                           
PSVersion                      6.0.0-beta.8                                    
PSEdition                      Core                                            
GitCommitId                    v6.0.0-beta.8                                   
OS                             Microsoft Windows 6.3.9600                      
Platform                       Win32NT                                         
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                         
PSRemotingProtocolVersion      2.3                                             
SerializationVersion           1.1.0.1                                         
WSManStackVersion              3.0                                             

Activity

  1. iSazonov commented on Nov 20, 2017

    @iSazonov
    Collaborator

    Add-Type uses a default namespace. I think Add-Type -Namespace resolves the issue.

    I believe we could make th error message more informative.

  2. added
    Issue-Question
    on Nov 20, 2017
  3. iSazonov commented on Nov 20, 2017

    @iSazonov
    Collaborator

    Dongbo Wang (@daxian-dbw) Could you please clarify why we first load assembly and only then check duplicate types? I'd expect that terminating error should prevent loading the assembly with duplicate types.

  4. daxian-dbw commented on Nov 21, 2017

    @daxian-dbw
    Member

    Ilya (@iSazonov) I agree with you that the duplicate type check should happen before actually loading the assembly. With the current behavior, 2 assemblies containing 2 different implementations of the same type name would be loaded in the AppDomain, and either one could be used when the user references the type name depending on the randomly generated assembly names by Add-Type.

    I think we check duplicates after loading now because it's easier. Before loading, it might be harder to know what types are in the assemblies. But I guess we can use the SyntaxTree returned from CSharpSyntaxTree.ParseText to get that information.

  5. daxian-dbw commented on Nov 21, 2017

    @daxian-dbw
    Member

    Ilya (@iSazonov) I found an interesting issue when playing with Add-Type and opened #5515

  6. iSazonov commented on Nov 21, 2017

    @iSazonov
    Collaborator

    I discover that CheckTypesForDuplicates has a wonder logic - it can add new non-duplicate types in a loaded type cache and then throw on duplicate type - and we get full mess with types.

    I think our logic should be simple - don't load an assembly if the one contains duplicate types in relation to loaded ones.

  7. alx9r commented on Nov 29, 2017

    @alx9r
    Author

    Add-Type uses a default namespace. I think Add-Type -Namespace resolves the issue.

    Ilya (@iSazonov) Are you suggesting that, to avert this problem, the types used by the liba and libb modules should be namespaces named uniquely for each minor version of libutil? So accessing the type from, say version 1.3.2 of module liba would look like [libutil.v1_0_0.E], for example. And accessing the type from version 1.3.3 of module liba would look like [libutil.v_1_0_1.E].

  8. iSazonov commented on Dec 1, 2017

    @iSazonov
    Collaborator

    alx9r You can use this as workaround for this certain case.

  9. iSazonov commented on May 8, 2018

    @iSazonov
    Collaborator

    Bugs in Add-Type fixed. Can we close the Issue? Currently we have a workaround. General solution requires a significant alteration in the type resolution.

  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  12. added
    Resolution-No Activity
    on Nov 16, 2023
  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  14. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

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

    Issue-QuestionResolution-No Activity

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions