Repository navigation
Description
This regression got introduced with #3556. This impacted the functionality of PowerShellGet module in PowerShellCore beta1 release (https://github.com/PowerShell/PowerShellGet/issues/125).
I think, Add-Type should ignore -ReferencedAssemblies if that is not applicable on PowerShellCore.
Add-Type should still support -ReferencedAssemblies for assemblies which are not referenced by default in PowerShellCore.
This impacts all PowerShell Script modules which uses Add-Type with -ReferencedAssemblies. Please ensure that this is not another SemanticVersion related issue in PowerShellCore for 2 part version comparison with $PSVersionTable.PSVersion .
Repro
$requiredAssembly = @([System.Management.Automation.PSCmdlet].Assembly.FullName)
$source = @"
using System.Management.Automation;
namespace Microsoft.PowerShell.Commands.PowerShellGet
{
[Cmdlet("Get", "Test")]
public class Telemetry : PSCmdlet
{
protected override void ProcessRecord()
{
WriteObject(new { ArtifactsNotFound = "abc" });
}
}
}
"@
Add-Type -TypeDefinition $source -Language CSharp -Verbose -PassThru -ReferencedAssemblies $requiredAssemblyExpected Behavior
Add-Type runs successfully, link on Windows PowerShell v 5.1
Actual Behavior
Add-Type : (1) : Missing compiler required member 'System.Collections.Generic.EqualityComparer`1.Equals'
(1) : >>> using System.Management.Automation;
(2) : namespace Microsoft.PowerShell.Commands.PowerShellGet
At line:1 char:1
+ Add-Type -TypeDefinition $source -Language CSharp -Verbose -PassThru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : (1) : Missing compiler required member 'System.Collections.Generic.EqualityComparer`1.GetHashCode'
(1) : >>> using System.Management.Automation;
(2) : namespace Microsoft.PowerShell.Commands.PowerShellGet
At line:1 char:1
+ Add-Type -TypeDefinition $source -Language CSharp -Verbose -PassThru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : (1) : Missing compiler required member 'System.Collections.Generic.EqualityComparer`1.get_Default'
(1) : >>> using System.Management.Automation;
(2) : namespace Microsoft.PowerShell.Commands.PowerShellGet
At line:1 char:1
+ Add-Type -TypeDefinition $source -Language CSharp -Verbose -PassThru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCompilerError) [Add-Type], Exception
+ FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCommand
Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
+ Add-Type -TypeDefinition $source -Language CSharp -Verbose -PassThru ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Type], InvalidOperationException
+ FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeCommand
Environment
Name Value
---- -----
PSVersion 6.0.0-rc
PSEdition Core
GitCommitId v6.0.0-rc
OS Microsoft Windows 10.0.16299
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Solution/Workaround
Add-Type now implicitly include all netcoreapp2.0 reference assemblies and System.Management.Automation by default with -ReferenceAssembly is not specified. So running Add-Type without -ReferenceAssembly will work.
Activity
daxian-dbw commentedon May 13, 2017
[Move discussion comments from MSTeam to Github]
Add-Typecan be made smarter to do better choice about what explicitly given reference assemblies to ignore and what to included implicitly. The user probably should be notified that the ignored ref assemblies are not necessary, probably through a warning message.daxian-dbw commentedon May 13, 2017
The problem we need to solve (or at least mitigate) is to avoid requiring conditional logic about how to specify
-ReferenceAssemblyparameter when usingAdd-Typein a script that would run on Windows PowerShell and PowerShell Core. Ideally, a single set of arguments for-ReferenceAssemblyshould work on both Windows PowerShell and PowerShell Core.iSazonov commentedon Nov 20, 2017
Dongbo Wang (@daxian-dbw) It seems you already fixed the Issue.
daxian-dbw commentedon Nov 20, 2017
Ilya (@iSazonov) If I remember it correctly, running
Add-Assemblywith the same script may require different reference assemblies on Wndows PowerShell and PowerShell Core, so a conditional logic is needed ifAdd-Typeis needed for a module targeting both Windows PowerShell and PowerShell Core. This issue is for solving or mitigating that problem.iSazonov commentedon Nov 21, 2017
Dongbo Wang (@daxian-dbw) Thanks for clarify! The repo and workaround from the Issue don't work now. Have you new repo?
daxian-dbw commentedon Nov 29, 2017
Ilya (@iSazonov) I have updated the repro steps in the issue description. It should demonstrate that the same script runs on Windows PowerShell but fails on PowerShell Core.
iSazonov commentedon Nov 29, 2017
Dongbo Wang (@daxian-dbw) Thanks for the update!
If we remove
-ReferencedAssembliesthe code works on both versions - so the reference on the assembly is not mandatory for Windows PowerShell. The error is because required assemblies is missing from this list-ReferencedAssemblies. If replaces_autoReferencedAssemblies.Valuewiths_defaultAssemblies.Valuein the line the Issue is resolved. If it is a right fix I can add it to my fork where I work on SyntaxTree integration.daxian-dbw commentedon Nov 29, 2017
Ilya (@iSazonov) The repro is just a demonstration, there could be other examples which you can come up with yourself once leaned the code that causes the issue.
If I remember correctly, you will also need to resolve the conflicts between what are specified by the user with
-ReferenceAssembliesand the default references we use. The default ones are pointing to the reference assemblies in the/refsfolder. If the user specify a dll path points to the runtime dll, you may getambiguous type identityor other issues during the compilation. But sometimes it's OK to use a runtime dll path so that you can use types that are not included in the reference dlls that we include (we only include the standard .NET Core 2.0 reference assemblies, but say you want to useJson.net.dll).Currently, we include the default references (all .NET Core 2.0 ref assemblies) only if
-ReferenceAssembliesis not specified. It will be tricky if you want to always include them.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.