Skip to content

-Verbose Changes (downgrades) the Error Behaviour #2247

@timker

Description

@timker

-Verbose changes (downgrades) the Error Action from a Stop to Continue. This is undocumented and confusing.

Steps to reproduce

Run this script

$ErrorActionPreference = "Stop"
$VerbosePreference = "continue"

#safely creating a file for first run
if(-Not (Test-Path test.txt))
{
    new-item test.txt  -Verbose 
}

#create the same file
new-item test.txt  -Verbose
Write-output 'This should not output because ErrorActionPreference is set to STOP'


new-item test.txt 
Write-output 'This should never output because of the error above, and it doesn't'

Expected behavior

'This should not output because ErrorActionPreference is set to STOP', should not output

Actual behavior

'This should not output because ErrorActionPreference is set to STOP' is output

Bug Location

MshCommandRuntime.cs
internal ActionPreference ErrorAction
if (Verbose)
return ActionPreference.Continue;
doesn't will downgrade a Stop

Proposed Solution

                if (!_isErrorActionPreferenceCached)
                {
                    bool defaultUsed = false;
                    _errorAction = Context.GetEnumPreference<ActionPreference>(SpecialVariables.ErrorActionPreferenceVarPath, _errorAction, out defaultUsed);

                    //Verbose should upgrade communication preferences, but not downgrade ErrorAction behavioural preferences    
                    if (Verbose)
                    {
                        // don't downgrade preferences 
                        if (!(_errorAction == ActionPreference.Stop || _errorAction == ActionPreference.Suspend))
                        {
                            _errorAction = ActionPreference.Continue;
                        }
                    }

                    _isErrorActionPreferenceCached = true;
                }

Environment data

Major Minor Patch Label
----- ----- ----- -----
   6     0     0 alpha

Metadata

Metadata

Assignees

Labels

Breaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionResolution-FixedThe issue is fixed.WG-Enginecore PowerShell engine, interpreter, and runtime

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions