Skip to content

ToString has invalid/no console output after any property, method or function is accessed #20694

Open

Description

@agowa

Prerequisites

Steps to reproduce

Create a powershell class with an override for ToString(). Call any property, method or function on it. And finally output it to the console again. See below for an example script including its console output as comments.

Expected behavior

It always calling the ToString() and always correctly forming console output regardless of what function, method, or property was accessed previously.

Actual behavior

class TestClass {
  hidden [bool]$_foo = $false

  [void]Enable() {
    $this._foo = $true
  }

  [void]Disable() {
    $this._foo  = $false
  }

  [string]ToString() {
    return ('Value is: {0}' -f $this._foo.ToString())
  }
}

#1
[Collections.Generic.Dictionary[int, TestClass]]$testClassDict = @{}
1..4 | ForEach-Object { $testClassDict[$_] = [TestClass]::new() }
$testClassDict
<#
Key Value
--- -----
  1 Value is: False
  2 Value is: False
  3 Value is: False
  4 Value is: False
#>
$testClassDict[1]
# Value is: False
$testClassDict[1].ToString()
# Value is: False
$testClassDict[1]
#
#
$testClassDict
<#
Key Value
--- -----
  1 Value is: False
  2 Value is: False
  3 Value is: False
  4 Value is: False
#>



#2
[TestClass]$_testClass = [TestClass]::new()
$_testClass
# Value is: False
$_testClass | Write-Host
# Value is: False
$_testClass.Enable()
$_testClass | Write-Host
# Value is: False
$_testClass
#
#
$_testClass.ToString()
# Value is: False

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.9
PSEdition                      Core
GitCommitId                    7.3.9
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

image

Activity

  1. added
    Needs-Triage
    on Nov 16, 2023
  2. mklement0 commented on Nov 16, 2023

    @mklement0
    Contributor

    Update: The bug appears to be related to the CompletionPredictor plug-in module of PSReadLine. For me, the bug only surfaces with both modules loaded. I've submitted PowerShell/CompletionPredictor#35, which points here.

    Using the following minimal repro, I see it only :

    • with custom class definitions that have no (non-hidden) properties,
    • if the following commands are submitted interactively, one by one (which pasting the block of lines does, but on Windows only via RIGHT-CLICKING; at least on macOS, line-by-line pasting happens even with Ctrl-V)
    • and if both PSReadLine and CompletionPredictor are loaded at the time:
    # Paste this block of lines *at the prompt*, *via RIGHT-CLICKING on Windows*.
    # You should see TWO "Foo" output lines, but the second implicit output of $o 
    # prints just an empty line - as a result of the attempted property access with $o.no
    class Foo {  }; $o = [Foo]::new()
    $o
    $o.no
    $o
    • It is the (attempted) member access $o.no that triggers the problem (it doesn't matter whether the member exists).
    • If I execute Remove-Module PSReadLine first, the problem does not surface.
    • If do it after, the symptom persists with the original $o object (but the problem goes away when re-running the commands); conversely, an unaffected $o instance created while PSReadLine was not loaded continues to be unaffected after reloading PSReadLine.
    • I never see the problem if the code is executed via a script file (whether with dot-sourcing or not).
    • On a general note:
  3. B-Art commented on Nov 18, 2023

    @B-Art

    Looks like your are running it in vscode. That is a different ballgame.
    It looks like it is working the Pwsh itself.

    PowerShell 7.4.0
    PS C:\Users\<username>> $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      7.4.0
    PSEdition                      Core
    GitCommitId                    7.4.0
    OS                             Microsoft Windows 10.0.22631
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0
    
    PS C:\Users\<username>> class TestClass {
    >>   hidden [bool]$_foo = $false
    >>
    >>   [void]Enable() {
    >>     $this._foo = $true
    >>   }
    >>
    >>   [void]Disable() {
    >>     $this._foo  = $false
    >>   }
    >>
    >>   [string]ToString() {
    >>     return ('Value is: {0}' -f $this._foo.ToString())
    >>   }
    >> }
    PS C:\Users\<username>>
    PS C:\Users\<username>> #1
    PS C:\Users\<username>> [Collections.Generic.Dictionary[int, TestClass]]$testClassDict = @{}
    PS C:\Users\<username>> 1..4 | ForEach-Object { $testClassDict[$_] = [TestClass]::new() }
    PS C:\Users\<username>> $testClassDict
    
    Key Value
    --- -----
      1 Value is: False
      2 Value is: False
      3 Value is: False
      4 Value is: False
    
    PS C:\Users\<username>> <#
    >> Key Value
    >> --- -----
    >>   1 Value is: False
    >>   2 Value is: False
    >>   3 Value is: False
    >>   4 Value is: False
    >> #>
    PS C:\Users\<username>> $testClassDict[1]
    Value is: False
    PS C:\Users\<username>> # Value is: False
    PS C:\Users\<username>> $testClassDict[1].ToString()
    Value is: False
    PS C:\Users\<username>> # Value is: False
    PS C:\Users\<username>> $testClassDict[1]
    Value is: False
    PS C:\Users\<username>> #
    PS C:\Users\<username>> #
    PS C:\Users\<username>> $testClassDict
    
    Key Value
    --- -----
      1 Value is: False
      2 Value is: False
      3 Value is: False
      4 Value is: False
    
    PS C:\Users\<username>> <#
    >> Key Value
    >> --- -----
    >>   1 Value is: False
    >>   2 Value is: False
    >>   3 Value is: False
    >>   4 Value is: False
    >> #>
    PS C:\Users\<username>>
    PS C:\Users\<username>>
    PS C:\Users\<username>>
    PS C:\Users\<username>> #2
    PS C:\Users\<username>> [TestClass]$_testClass = [TestClass]::new()
    PS C:\Users\<username>> $_testClass
    Value is: False
    PS C:\Users\<username>> # Value is: False
    PS C:\Users\<username>> $_testClass | Write-Host
    Value is: False
    PS C:\Users\<username>> # Value is: False
    PS C:\Users\<username>> $_testClass.Enable()
    PS C:\Users\<username>> $_testClass | Write-Host
    Value is: True
    PS C:\Users\<username>> # Value is: False
    PS C:\Users\<username>> $_testClass
    Value is: True
    PS C:\Users\<username>> #
    PS C:\Users\<username>> #
    PS C:\Users\<username>> $_testClass.ToString()
    Value is: True
    PS C:\Users\<username>> # Value is: False
    PS C:\Users\<username>> $_testClass.Disable()
    PS C:\Users\<username>> $_testClass.ToString()
    Value is: False
    PS C:\Users\<username>> $testClass.ToString()
    InvalidOperation: You cannot call a method on a null-valued expression.
    PS C:\Users\<username>> $testClass
    PS C:\Users\<username>> $testClassDict
    
    Key Value
    --- -----
      1 Value is: False
      2 Value is: False
      3 Value is: False
      4 Value is: False
    
    PS C:\Users\<username>> $testClassDict.ToString()
    System.Collections.Generic.Dictionary`2[System.Int32,TestClass]
    PS C:\Users\<username>> $testClassDict.Foreach{$_.ToString()}
    System.Collections.Generic.Dictionary`2[System.Int32,TestClass]
    PS C:\Users\<username>> $testClassDict[0].Foreach{$_.ToString()}
    PS C:\Users\<username>> $testClassDict[0].ToString()
    InvalidOperation: You cannot call a method on a null-valued expression.
    PS C:\Users\<username>> $testClassDict[1]
    Value is: False
    PS C:\Users\<username>> $testClassDict[4]
    Value is: False
    PS C:\Users\<username>> (1..4).foreach{$testClassDict[$_].ToString()}
    Value is: False
    Value is: False
    Value is: False
    Value is: False
    PS C:\Users\<username>> $testClassDict[1].Disable()
    PS C:\Users\<username>> (1..4).foreach{$testClassDict[$_].ToString()}
    Value is: False
    Value is: False
    Value is: False
    Value is: False
    PS C:\Users\<username>> $testClassDict[3].Enable()
    PS C:\Users\<username>> (1..4).foreach{$testClassDict[$_].ToString()}
    Value is: False
    Value is: False
    Value is: True
    Value is: False
    PS C:\Users\<username>> $testClassDict[3].Disable()
    PS C:\Users\<username>> (1..4).foreach{$testClassDict[$_].ToString()}
    Value is: False
    Value is: False
    Value is: False
    Value is: False
    PS C:\Users\<username>> (1..4).foreach{$testClassDict[$_]._foo.ToString()}
    False
    False
    False
    False
    PS C:\Users\<username>> (1..4).foreach{$testClassDict[$_]._foo}
    False
    False
    False
    False
    PS C:\Users\<username>> $testClassDict[3].Enable()
    PS C:\Users\<username>> (1..4).foreach{$testClassDict[$_]._foo}
    False
    False
    True
    False
    PS C:\Users\<username>> $_testClass._foo
    False
    PS C:\Users\<username>> $_testClass.Enable()
    PS C:\Users\<username>> $_testClass._foo
    True
    
  4. mklement0 commented on Nov 18, 2023

    @mklement0
    Contributor

    No, I was running it in regular console windows, and I see it everywhere, including in Windows Terminal, Visual Studio Code, and in Terminal.app and iTerm2.app on macOS.

    However:

    • My instructions were flawed: I neglected to mention that on Windows you have to paste via right-clicking, as only then are the commands submitted one by one.
    • More importantly: The problem seems connected to my specific PSReadLine configuration - I have yet to discover what, specifically, triggers the problem. Having PSReadLine loaded alone does not trigger the problem.

    I've updated my previous comment.

  5. mklement0 commented on Nov 19, 2023

    @mklement0
    Contributor

    Bart van den Donk (@B-Art), I missed that you're not the OP (and neither am I).

    • Yes, the screenshot implies use of Visual Studio Code's integrated terminal.
    • However, I see no difference in behavior depending on the console/terminal environment.
    • I do see that it works by default, but - for reasons still unknown to me - doesn't work with my custom PSReadLine configuration, regardless of environment.
  6. B-Art commented on Nov 19, 2023

    @B-Art

    Than maybe (to investigate further) you must share the way you changed your psreadline settings?

    Otherwise it will be hard to reproduce.

    Customization of psreadline isn't always performing the best way when pasting via ctrl-v.

    For instance if you program it to create a second " when you type the first, that messes things up when (trying to) pasting inbetween.

  7. mklement0 commented on Nov 20, 2023

    @mklement0
    Contributor

    Bart van den Donk (@B-Art), the pasting part is incidental to the problem: what matters is whether the commands are submitted one by one.

    I've narrowed down the problem, and it is only indirectly related to PSReadLine: it is its CompletionPredictor plugin module whose import causes the problem. I've updated my original comment.

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

    Needs-Triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions