Skip to content

Show warning when writing to the output stream in a function with a return value. #4924

@NickSt

Description

@NickSt

Prerequisites

  • I have written a descriptive issue title.
  • I have searched all issues to ensure it has not already been reported.

Summary

In a powershell function with a return value, any time the standard output stream is written to, it appends the those results to the output by transforming the output into an array. For Example

 function demonstration {
    echo "Starting demonstration"
    $returnHashMap = @{}
    Write-Output "building hashmap"
    $returnHashMap["key1"] = "value1"
    $returnHashMap["key2"] = "value2"
    Write-Output "hashmap built"

    return $returnHashMap
}
$x = demonstration
echo $x | ConvertTo-Json

This produces the following output :

[
  "Starting demonstration",
  "building hashmap",
  "hashmap built",
  {
    "key2": "value2",
    "key1": "value1"
  }
]

note the actual return value is a submember of an array.
if you wanted to access the hashmap at this point you have to first access its index in the array so for instance:
echo $x[3]['key1']

This behavior is almost always undesirable. I'd request that the extension provide a warning when doing this.

Proposed Design

I'd propose that the extension underline in yellow (warning) any time echo or write-output is used in a function that has a return command. If logging is desired write-information or (debug, verbose etc) can be used instead without the same effects.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-EnhancementA feature request (enhancement).Needs: TriageMaintainer attention needed!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions