Repository navigation
Description
Related: #1759
The Ignore error-action value is meant to be used only with the -ErrorAction common parameter, not with the $ErrorActionPreference preference variable.
An explicit check to prevent the latter was clearly implemented (as evidenced by error message The value Ignore is not supported for an ActionPreference variable. The provided value should be used only as a value for a preference parameter, and has been replaced by the default value.), but it doesn't / doesn't properly take effect.
-
On the command line,
$ErrorActionPreference = 'Ignore'is accepted at first. When the next error occurs, it is the error message about the invalid$ErrorActionPreferencevalue that surfaces instead of the actual error's, and only at that point is$ErrorActionPreferencereset toContinue. -
In a script,
$ErrorActionPreference = 'Ignore'is accepted and takes effect. A contributing factor is that preference variables in child scopes are not type-constrained, so I presume that no validation takes place at assignment time - see Explicit creation of preference variables in descendant scopes should be appropriately type-constrained, as in the global scope / according to the definition of the preference variable #3483
Steps to reproduce
- On the command line:
# On the command line: set to invalid value, reflect it, then trigger an error, then reflect it again.
> $ErrorActionPreference = 'Ignore'; $ErrorActionPreference; 1 / 0; $ErrorActionPreference- In a script (place code in a
*.ps1file):
$ErrorActionPreference = 'Ignore'; $ErrorActionPreference; 1/0; $ErrorActionPreferenceExpected behavior
- Both on the command line and in a script:
The value Ignore is not supported for an ActionPreference variable. The provided value should be used only as a
value for a preference parameter, and has been replaced by the default value.
...
Continue
Attempted to divide by zero.
...
Continue
Actual behavior
- On the command line:
Ignore
$ErrorActionPreference = 'Ignore'; $ErrorActionPreference; 1 / 0: The value Ignore is not supported for an ActionPreference variable. The provided value should be used
only as a value for a preference parameter, and has been replaced by the default value. For more information, see the Help topic, "about_Preference_Variables."
+ CategoryInfo : NotSpecified: (:) [], NotSupportedException
+ FullyQualifiedErrorId : System.NotSupportedException
Continue
The value was accepted at first and retained until the next error occurred. The next error's message is mistakenly replaced with the invalid-$ErrorActionPreference-value error message, and the value is reset to Continue at that point.
- In a script:
Ignore
Ignore
The value was accepted, stayed in effect, and suppressed the statement-terminating error.
Environment data
PowerShell Core v6.0.0-beta.4 on macOS 10.12.5
PowerShell Core v6.0.0-beta.4 on Ubuntu 16.04.2 LTS
PowerShell Core v6.0.0-beta.4 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.413 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Activity
KirkMunro commentedon Apr 16, 2019
Related to this, this issue drives me crazy:
The results of this command are as follows:
According to docs, that's a bug, correct? Because technically the function is setting
ErrorActionPreference, but from an end user's perspective, they're just using Ignore with-ErrorActionwhich should work just fine. I felt it was worth mentioning with this bug because this is a scenario where the prevention logic is being too strict, causing a bug.mklement0 commentedon Apr 16, 2019
Indeed, Kirk Munro (@KirkMunro). What you're describing is the subject of #1759, which also shows workarounds.
KirkMunro commentedon Apr 18, 2019
Just to highlight it here as well for visibility, the best solution to this issue seems to be:
Ignore.Set-Variable) as red, with appropriate documentation for that rule explaining why that's dangerous and what should be done instead.Also shared over here.
KirkMunro commentedon Apr 27, 2019
There's more to this. The fact that it works in a script is simply because the logic internally was written incorrectly, only preventing
$ErrorActionPreferencefrom having a value of[System.Management.Automation.ActionPreference]::Ignore(e.g. an enumerated value). Having a value that is of type string (which is what happens whenever you create a locally scoped$ErrorActionPreferencevalue -- it doesn't have to be in a script at all) allowsIgnoreto work just fine. You can also delete the top level$ErrorActionPreferencevariable, which is strongly typed as anActionPreference, and then recreate it as a string and again it works.For example:
That, combined with the example below, demonstrates how terrible a job PowerShell is doing in this case, when really this is a linting problem, not something that the parser/compiler/interpreter should care about at all.
Steve Lee (@SteveL-MSFT): Here's one for a committee meeting. Please give the green light to just remove the nonsensical code blocking Ignore from
$ErrorActionPreference(but only some of the time, as this illustrates), in favor of a PSSA rule to warn folks who don't know better instead.KirkMunro commentedon Apr 27, 2019
To add an additional thought for the committee:
ActionPreference.Suspendis not supported in PowerShell Core (it's only used for workflows), and today it just adds confusion to the code with checks to ensure that value isn't used where there doesn't need to be any (plus some of those checks suffer the same issues identified forActionPreference.Ignore, since they use the same logic). Do we need to keepActionPreference.Suspendaround at this point or can we just chuck it and the half-dozen checks related to it?joeyaiello commentedon May 8, 2019
Sorry for the ignorance here (and mostly this is a note to myself for a Committee discussion later today), but why is setting EAP to Ignore dangerous?
vexx32 commentedon May 8, 2019
Mainly because it effectively disables all possibility to handle non-terminating errors if you're not aware it's been set. For example, say you don't quite understand the gravity of it, and you set it in your profile.
Many commands will now simply cease to provide output, with no apparent reason as to why. No errors are generated or stored in $error, nothing is displayed. It's possible that for some poorly-designed commands or functions that this could be actually harmful, but in my mind it would mostly hurt the user experience for those less familiar with the language.
That said, it probably isn't that huge of a concern, really.
SteveL-MSFT commentedon May 8, 2019
@PowerShell/powershell-committee reviewed this and agrees that the check for
Ignoreshould be removed and allow the user to set what they want. Separately, we should have a PSScriptAnalyzer rule to detect and recommend against setting EAP.KirkMunro commentedon May 9, 2019
Steve Lee (@SteveL-MSFT) What about the question about
ActionPreference.Suspend? What was the decision there?SteveL-MSFT commentedon May 9, 2019
Kirk Munro (@KirkMunro) sorry we missed that. Can you open a new issue on that and cc me to mark for @PowerShell/powershell-committee review? Thanks.
KirkMunro commentedon May 9, 2019
FYI, I already included a fix for this as part of PR #8205.
KirkMunro commentedon Sep 25, 2019
The fix for this was to allow
ActionPreference.Ignoreto be assigned to any action preference variable, which allows it to be used in functions as well. This fix was merged and included in PowerShell 7 preview 4. I believe this issue can be closed.vexx32 commentedon Sep 25, 2019
/cc Ilya (@iSazonov) 🙂
daxian-dbw commentedon Sep 25, 2019
Fixed by #10317
iSazonov commentedon Sep 26, 2019
Kirk Munro (@KirkMunro) Thanks for fixing this!