Repository navigation
Description
Summary of the new feature / enhancement
Write-Output description excerpt:
In PowerShell, it is generally not necessary to use the cmdlet in instances where the output is displayed by default.
This is a very nice feature but at the other hand could cause quite some confusion for a novice PowerShell scripter or with complex (recursive) functions as it implies that unwanted objects (including $null) could be accidently dropped into the pipeline by an included command (function, method or cmdlet) without notice.
Besides, if it known that a custom function leaks objects into the pipeline, it might still be a hassle to actually track them down.
See StackOverflow issues along with:
- PowerShell Pipeline Pollution
- Powershell ArrayList magically inserts a zero
- Why does Range.BorderAround emit “True” to the console?
Proposed technical implementation details (optional)
Despite the reasons to avoid the use of the Write-Output cmdlet, it would be nice to have a Set-StrictMode version that enforces the use of Write-Output or Return. Meaning, if anything is placed on the pipeline that doesn't come from Write-Output or Return, a "strict" error should be raised pushing for a fix either way:
- If it was the intention to add something to the pipeline, use the
Write-Outputcmdlet. - if it was not the intention to add anything to the pipeline, waste the results by using the
[Void]initiator or assigning it
(e.g.$Null = ...)
Caveats
- I have no clue how difficult it is to determine whether a function or method always returns nothing or for a cmdlet (like
Export-Csv) always returns aSystem.Management.Automation.Internal.AutomationNull. - I suspect that that external commands should always be voided (
[void](NotePad)) or assigned ($Null = NotePad) - As
Set-StrictModeis also applied to the child scopes, I presume that custom child functions can just be checked for the existence ofWrite-Hostorreturnstatement to determine whether something is returned.
Actually, at second thought, the above caveats ar probably not relevant as the strict check (whether Write-Output or Return is used when something is added to the pipeline) should take place at runtime.
Set-StrictModecan be set from within a function but could also be set (temporary for trouble shooting) at the prompt level (or even at a global scope withSet-PSDebug -Strictcommand). The indiscriminate implication of this request would be that every expression (including a simple expression-mode calculation as2 * 3or.\MyTestFunction.ps1invocation) would require aWrite-Output(as:2 * 3 |Write-Outputor.\MyTestFunction.ps1 |Write-Output) unless exceptions are made at the top/shell-level.
Activity
mklement0 commentedon Nov 1, 2021
The pitfall of accidental output is real, but I wonder if it is better addressed with a debugging feature rather than a new strict mode, which would in essence take away a fundamental PowerShell feature (implicit output). Also, as a fundamental problem, the lack of lexical scoping makes any strict mode liable to break unrelated code.
Therefore, I wonder if enhancing, say,
Set-PSDebugis a better way forward, allowing users to locate all lines in their script that produce output in a given call.That is, the above indicates that the statement on line
8produced output.As a stopgap you can use the following technique to print the locations and source code of output-producing statements:
iRon7 commentedon Nov 7, 2021
Michael Klement (@mklement0), thanks for the comments, I indeed also like the
Set-PSDebugidea.I agree, the idea is quite controversy. Yet avoiding accidental pitfalls is exactly why someone would prefer strict programming (modes). Besides it is an optional choice. By default, PowerShell is still a loosely programming language.
mklement0 commentedon Nov 9, 2021
Re
Set-PSDebug: Glad to hear it, iRon7.Re introducing new strict modes in general: I am wary:
As for the specific new mode proposed:
Therefore, my personal preference would be to help prevent this pitfall via debugging and linting features (PSSA, also as an opt-in).
mklement0 commentedon Dec 7, 2021
iRon7, I assume you meant to post this comment here instead:
That would certainly fix the scoping issue (it could even be a property of the existing
CmdletBindingattribute) and make code easier to reason about.However, requiring the use of an attribute - undoubtedly an advanced technique - could prove too high of a hurdle for the intended target audience, novice scripters. (For those comfortable with implicit output who are merely looking to troubleshoot code, a
Set-PSDebugsolution should suffice.)daxian-dbw commentedon Jan 21, 2022
The Engine WG reviewed this issue today. We don't think introducing a new strict mode is the right way to help prevent this issue because that could easily break other scripts. If a script uses strict mode, then most likely it's using
Set-StrictMode latest, which means new strict mode like the proposed one will be applied to it, and thus potentially break the script.We believe PowerShell Script Analyzer should be the right tool to help, by emitting warnings for expressions that generate output directly to pipeline. Feel free to open an issue in that repo.
iRon7 commentedon Jan 24, 2022
Sorry, I was on holidays and couldn't immediately respond.
This argument is more related to the implementation of
Set-StrictModeand how the scope is linked to a version rather than a lexical scoping using a different parameter or cmdlet annotations (using the System.Diagnostics.CodeAnalysis Namespace?) instead. See also:#16388-Modeparameter forSet-StrictMode.Afaik, PowerShell Script Analyzer is not able to catch runtime errors/situation knowing that a (custom) function/cmdlet could potentially return an object but effectively never does during runtime...
Anyways, I am happy to give it a try if you think otherwise.
iRon7 commentedon Apr 11, 2022
Related:
#3669Allow advanced functions to only return objects to the pipeline explicitly