Skip to content

Add-Type should not fail when default referenced assemblies are specified with -ReferencedAssemblies #3779

Closed

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 $requiredAssembly

Expected 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

  1. daxian-dbw commented on May 13, 2017

    @daxian-dbw
    Member

    [Move discussion comments from MSTeam to Github]
    Add-Type can 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.

  2. daxian-dbw commented on May 13, 2017

    @daxian-dbw
    Member

    The problem we need to solve (or at least mitigate) is to avoid requiring conditional logic about how to specify -ReferenceAssembly parameter when using Add-Type in a script that would run on Windows PowerShell and PowerShell Core. Ideally, a single set of arguments for -ReferenceAssembly should work on both Windows PowerShell and PowerShell Core.

  3. added
    WG-Enginecore PowerShell engine, interpreter, and runtime
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    on May 13, 2017
  4. iSazonov commented on Nov 20, 2017

    @iSazonov
    Collaborator

    Dongbo Wang (@daxian-dbw) It seems you already fixed the Issue.

  5. daxian-dbw commented on Nov 20, 2017

    @daxian-dbw
    Member

    Ilya (@iSazonov) If I remember it correctly, running Add-Assembly with the same script may require different reference assemblies on Wndows PowerShell and PowerShell Core, so a conditional logic is needed if Add-Type is needed for a module targeting both Windows PowerShell and PowerShell Core. This issue is for solving or mitigating that problem.

  6. iSazonov commented on Nov 21, 2017

    @iSazonov
    Collaborator

    Dongbo Wang (@daxian-dbw) Thanks for clarify! The repo and workaround from the Issue don't work now. Have you new repo?

  7. daxian-dbw commented on Nov 29, 2017

    @daxian-dbw
    Member

    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.

  8. iSazonov commented on Nov 29, 2017

    @iSazonov
    Collaborator

    Dongbo Wang (@daxian-dbw) Thanks for the update!
    If we remove -ReferencedAssemblies the 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 replace s_autoReferencedAssemblies.Value with s_defaultAssemblies.Value in 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.

  9. daxian-dbw commented on Nov 29, 2017

    @daxian-dbw
    Member

    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 -ReferenceAssemblies and the default references we use. The default ones are pointing to the reference assemblies in the /refs folder. If the user specify a dll path points to the runtime dll, you may get ambiguous type identity or 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 use Json.net.dll).

    Currently, we include the default references (all .NET Core 2.0 ref assemblies) only if -ReferenceAssemblies is not specified. It will be tricky if you want to always include them.

  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. 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.

  13. 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-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issuesWG-Enginecore PowerShell engine, interpreter, and runtime

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions