Repository navigation
Description
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_bExpected 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
iSazonov commentedon Nov 20, 2017
Add-Type uses a default namespace. I think
Add-Type -Namespaceresolves the issue.I believe we could make th error message more informative.
iSazonov commentedon Nov 20, 2017
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.
daxian-dbw commentedon Nov 21, 2017
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
SyntaxTreereturned fromCSharpSyntaxTree.ParseTextto get that information.daxian-dbw commentedon Nov 21, 2017
Ilya (@iSazonov) I found an interesting issue when playing with
Add-Typeand opened #5515iSazonov commentedon Nov 21, 2017
I discover that
CheckTypesForDuplicateshas 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.
alx9r commentedon Nov 29, 2017
Ilya (@iSazonov) Are you suggesting that, to avert this problem, the types used by the
libaandlibbmodules should be namespaces named uniquely for each minor version oflibutil? So accessing the type from, say version 1.3.2 of modulelibawould look like[libutil.v1_0_0.E], for example. And accessing the type from version 1.3.3 of modulelibawould look like[libutil.v_1_0_1.E].iSazonov commentedon Dec 1, 2017
alx9r You can use this as workaround for this certain case.
iSazonov commentedon May 8, 2018
Bugs in
Add-Typefixed. Can we close the Issue? Currently we have a workaround. General solution requires a significant alteration in the type resolution.microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 16, 2023
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.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.