Skip to content

-Verbose Changes (downgrades) the Error Behaviour #2247

Closed

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

Activity

  1. added
    WG-Engine
    on Sep 13, 2016
  2. added this to the milestone on Nov 2, 2016
  3. added
    Breaking-Change
    Review - Committee
    on May 25, 2017
  4. joeyaiello commented on May 25, 2017

    @joeyaiello
    Contributor

    This is a breaking change that we'd need to review, but if we're going to take it, there's the code, and we should do it sooner rather than later.

  5. SteveL-MSFT commented on May 31, 2017

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee it appears -Debug also has the same issue. Committee agrees that we should fix this so that -ErrorAction works correctly and not overridden by -Debug or -Verbose, but Jason Shirk (@lzybkr) would like to think about the implications of this change

  6. mintsoft commented on Jun 2, 2017

    @mintsoft

    We just ran into this bug too; +1 for changing it to respect the $ErrorActionPreference

  7. added
    Committee-Reviewed
    and removed
    Review - Committee
    on Jul 5, 2017
  8. modified the milestones: , on Jul 5, 2017
  9. joeyaiello commented on Oct 10, 2017

    @joeyaiello
    Contributor

    Jason Shirk (@lzybkr) couldn't come up with anything per our last conversation on this one back in July-ish. We should implement the fix.

    Honestly, though, this is a small enough one that I might feel okay pushing it out if we have to.

  10. WernerMairl commented on Dec 7, 2017

    @WernerMairl

    👍👍
    a lot of unexpected behavior (without finding the reason) that i found in the last year should be solved with this, THANK YOU!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Breaking-ChangeCommittee-ReviewedResolution-FixedWG-Engine

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions