Skip to content

Using custom attributes in PowerShell Class results in parsing error. #2642

Closed

Description

@lfshr

Using a custom attribute in a PowerShell class causes PowerShell to error with message "An error occurred while creating the pipeline"

Steps to reproduce

class CustomAttribute1 : System.Attribute
{
}

class CustomObject1
{
[CustomAttribute1()]
$Var1
}

Expected behavior

Creates class with attributed member

Actual behavior

PowerShell throws "An error occurred while creating the pipeline"

Environment data

PSVersion 5.0.10240.16384
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.42000
BuildVersion 10.0.10240.16384
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3

Activity

  1. changed the title [-]Using custom attributes in PowerShell Class results in meaningless error.[/-] [+]Using custom attributes in PowerShell Class results in parsing error.[/+] on Nov 8, 2016
  2. added
    Issue-BugIssue has been identified as a bug in the product
    WG-Languageparser, language semantics
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Nov 8, 2016
  3. lfshr commented on Nov 9, 2016

    @lfshr
    Author

    This appears to go deeper than custom Attributes. Custom classes defined in in dll files also do not work! Trying my best to come up with a solution.

  4. iSazonov commented on Feb 18, 2017

    @iSazonov
    Collaborator

    I tested the sample in Powershell Core (alpha 15) - it work without a parser error.

  5. lzybkr commented on Feb 18, 2017

    @lzybkr
    Contributor

    It's a runtime error because we can't resolve the type and don't yet support defining an attribute in PowerShell.

    There is no reason this couldn't work though - and that is the basic syntax you'd use (though we'd require an AttributeUsage attribute on the attribute class definition.

  6. iSazonov commented on Feb 18, 2017

    @iSazonov
    Collaborator

    Jason Shirk (@lzybkr) Please clarify what DefineCustomAttributes do in PSTypes.cs?

  7. lzybkr commented on Feb 18, 2017

    @lzybkr
    Contributor

    It constructs the metadata when an attribute is applied to something.

    A blob of metadata has a reference to the constructor (and implicitly the attribute type) and the positional and named arguments for the specific application of the attribute.

  8. iSazonov commented on Feb 19, 2017

    @iSazonov
    Collaborator

    Jason Shirk (@lzybkr) Thanks!
    I don't see the parser error in PS 5.1 and PS Core alfa.15
    I didn't realize that we need to fix 😕

    class TestAttribute : System.Attribute {
        [string]$var1 = "q1"
        TestAttribute ([string] $t) {
            write-host "qwerty"
            $this.var1 = $t
    }
     }
    
    [TestAttribute("q2")]
       class TestClass {
       [int] [TestAttribute("q3")] $i = 5
    }
    
    
    $a = New-Object TestClass
    
    PS C:\Program Files\PowerShell\6.0.0.15> $a.Gettype().GetCustomAttributes($false)
    qwerty
    
    var1 TypeId
    ---- ------
    q2   TestAttribute
    
    
    PS C:\Program Files\PowerShell\6.0.0.15> $a.gettype().Getproperties()
    
    
    MemberType       : Property
    PropertyType     : System.Int32
    Attributes       : None
    CanRead          : True
    CanWrite         : True
    GetMethod        : Int32 get_i()
    SetMethod        : Void set_i(Int32)
    IsSpecialName    : False
    Name             : i
    DeclaringType    : TestClass
    ReflectedType    : TestClass
    CustomAttributes : {[TestAttribute("q3")]}
    MetadataToken    : 385875969
    Module           : RefEmit_InMemoryManifestModule
    
    
    PS C:\Program Files\PowerShell\6.0.0.15> $a.gettype().Getproperty("i").GetCustomAttributes($true)
    qwerty
    
    var1 TypeId
    ---- ------
    q3   TestAttribute
    ``
  9. lzybkr commented on Feb 19, 2017

    @lzybkr
    Contributor

    Ilya (@iSazonov) - I think you're getting lucky - maybe tried this in the ISE, and the attribute was defined before you ran the full script using the attribute.

    I get an error with your example when run in fresh console.

  10. iSazonov commented on Feb 19, 2017

    @iSazonov
    Collaborator

    Reboot my notebook. (Win 10 build 14936)
    Run C:\Program Files\PowerShell\6.0.0.15\powershell.exe
    Run the test (copy by right click).
    No errors 😕

    I don't install any development tools, SDK... on my notebook.
    Powershell is installed from package downloaded from the repo.

  11. lzybkr commented on Feb 19, 2017

    @lzybkr
    Contributor

    Ah - right click - that's your problem. Put the repro in a file and run that.

    With right click - the attribution definition was seen as one complete statement and run before parsing the next class.

    If you pasted (Ctrl+v in Windows PowerShell), then the entire buffer would have been parsed like a single file and I think you'd have seen the problem.

  12. iSazonov commented on Feb 20, 2017

    @iSazonov
    Collaborator

    Jason Shirk (@lzybkr) I thought that it is discovered something new.
    But this is not a new problem, the direct duplication old #1762 by Steve Lee (@SteveL-MSFT) .

    I see Issues with the loading of modules. Maybe they have the same root and we should gather all of them under one Issue:
    New-Object doesn't work with classes in nested modules #2963
    Using module behavior on nested modules is inconsistent #2964
    ???

  13. lzybkr commented on Feb 20, 2017

    @lzybkr
    Contributor

    Agreed - this is a dupe of #1762.

    The other issues you referenced may feel similar, but I don't believe they have the same root cause.

  14. added and removed
    Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributors
    on Feb 20, 2017
  15. iSazonov commented on Feb 21, 2017

    @iSazonov
    Collaborator

    Jason Shirk (@lzybkr) Thanks! Clear.

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-BugIssue has been identified as a bug in the productResolution-DuplicateThe issue is a duplicate.WG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions