Skip to content

'#Requires -Assembly' does not have any effect #11098

Closed

Description

@Mettcon

Nothing get's loaded and it throws no error. No matter if it's valid input or not.
With Add-Type -Assembly 'Name.For.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=123' could be loaded.
Requires for Administrator and Modules work like expected.

Steps to reproduce

@"
#Requires -RunAsAdministrator
#Requires -Assembly 'Name.For.Assembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=123'
Write-Host 'bla'
"@ > RequiresTest.ps1

Expected behavior

Expected it to work like Requires -Module ModuleName

Actual behavior

Like mentioned above, nothing happens.
The following showed no difference before and after execution

[System.AppDomain]::CurrentDomain.GetAssemblies() | select Fullname

Environment data

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.5
PSEdition                      Core
GitCommitId                    7.0.0-preview.5
OS                             Microsoft Windows 10.0.14393
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0
Name                           Value
----                           -----
PSVersion                      5.1.14393.206
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14393.206
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

Activity

  1. added
    Issue-Question
    on Nov 18, 2019
  2. iSazonov commented on Nov 18, 2019

    @iSazonov
    Collaborator

    Mettcon (@Mettcon) Please look #5022 and referenced there RFC.

  3. vexx32 commented on Nov 18, 2019

    @vexx32
    Collaborator

    Ilya (@iSazonov) does the using assembly alternative permit a fully qualified assembly name?

  4. iSazonov commented on Nov 18, 2019

    @iSazonov
    Collaborator

    Yes, #5022 and PowerShell/PowerShell-RFC#122 reject #Requires -Assembly
    Tracking issues #9929 #6653

  5. ghost closed this as completedon Nov 19, 2019
  6. xtqqczze commented on May 5, 2020

    @xtqqczze
    Contributor

    Ilya (@iSazonov) It is unclear from these issues whether or not #Requires -Assembly should be supported. Documentation suggests there is support, but this issue states the statement has no effect.

    If #Requires -Assembly is not supported, than we should throw an error instead of failing silently.

  7. iSazonov commented on May 5, 2020

    @iSazonov
    Collaborator

    xtqqczze What is a behavior on Windows PowerShell 5?

  8. xtqqczze commented on May 6, 2020

    @xtqqczze
    Contributor

    Ilya (@iSazonov) According to MicrosoftDocs/PowerShell-Docs#4518, the #Requires -Assembly feature was present in Windows PowerShell 5.1 (and likely earlier).

    However, my tests in Windows PowerShell 5.1 show the behaviour is the same, i.e. nothing is loaded and no error is thrown.

  9. iSazonov commented on May 6, 2020

    @iSazonov
    Collaborator

    Perhaps Rob Holt (@rjmholt) could comment - should we remove the parameter?

  10. rjmholt commented on May 6, 2020

    @rjmholt
    Collaborator

    I suspect it remains for compatibility with PS 5.1 -- my guess is that's been discussed elsewhere, but can't remember.

    Basically:

    • using assembly is preferred to #require -Assembly because it should try to load something rather than just fail if it's not loaded already. #require has a tradition of being an assertion rather than performing an action, and also presents as a comment pragma and isn't stored in an AST, meaning it's not as good a fit.
    • But because acceptance for the syntax (even if it does nothing) was added in PS 5.1, removing it would cause parse errors for scripts that would otherwise "just work" in migration from 5.1 to 7. So given the choice between a wart and a hard failure, the wart was chosen.

    I think the question remains open for whether it should be removed for PS vNext

  11. iSazonov commented on May 6, 2020

    @iSazonov
    Collaborator

    my guess is that's been discussed elsewhere, but can't remember.

    It is your RFC :-)

    I think we should update docs with "that currently it is not work on 5 and 7 versions and will be probably removed in future."

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions