Skip to content

Commit 61a10ce

Browse files
KirkMunroTravisEz13
authored andcommitted
Transition ActionPreference.Suspend enumeration value into a non-supported, reserved state, and remove restriction on using ActionPreference.Ignore in preference variables (PowerShell#10317)
1 parent d144a19 commit 61a10ce

File tree

6 files changed

+194
-111
lines changed

6 files changed

+194
-111
lines changed

src/System.Management.Automation/engine/ArgumentTypeConverterAttribute.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,14 @@ internal object Transform(EngineIntrinsics engineIntrinsics, object inputData, b
147147
// Note - this is duplicated in ExecutionContext.cs as parameter binding for script cmdlets can avoid this code path.
148148
if ((!bindingScriptCmdlet) && (!bindingParameters))
149149
{
150-
// ActionPreference of Suspend is not supported as a preference variable. We can only block "Suspend"
151-
// during variable assignment (here) - "Ignore" is blocked during variable retrieval.
150+
// ActionPreference.Suspend is reserved for future use and is not supported as a preference variable.
152151
if (_convertTypes[i] == typeof(ActionPreference))
153152
{
154153
ActionPreference resultPreference = (ActionPreference)result;
155154

156155
if (resultPreference == ActionPreference.Suspend)
157156
{
158-
throw new PSInvalidCastException("InvalidActionPreference", null, ErrorPackage.UnsupportedPreferenceVariable, resultPreference);
157+
throw new PSInvalidCastException("InvalidActionPreference", null, ErrorPackage.ActionPreferenceReservedForFutureUseError, resultPreference);
159158
}
160159
}
161160
}

src/System.Management.Automation/engine/CommandBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ public enum ActionPreference
291291
/// <summary>Ignore the event completely (not even logging it to the target stream)</summary>
292292
Ignore = 4,
293293

294-
/// <summary>Suspend the command for further diagnosis. Supported only for workflows.</summary>
294+
/// <summary>Reserved for future use.</summary>
295295
Suspend = 5,
296296

297297
/// <summary>Enter the debugger.</summary>

src/System.Management.Automation/engine/ExecutionContext.cs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,9 @@ internal T GetEnumPreference<T>(VariablePath preferenceVariablePath, T defaultPr
573573
object val = EngineSessionState.GetVariableValue(preferenceVariablePath, out _, out _);
574574
if (val is T)
575575
{
576-
// We don't want to support "Ignore" as action preferences, as it leads to bad
577-
// scripting habits. They are only supported as cmdlet overrides.
578-
if (val is ActionPreference)
576+
if (val is ActionPreference actionPreferenceValue)
579577
{
580-
ActionPreference preference = (ActionPreference)val;
581-
if ((preference == ActionPreference.Ignore) || (preference == ActionPreference.Suspend))
582-
{
583-
// Reset the variable value
584-
EngineSessionState.SetVariableValue(preferenceVariablePath.UserPath, defaultPref);
585-
string message = StringUtil.Format(ErrorPackage.UnsupportedPreferenceError, preference);
586-
throw new NotSupportedException(message);
587-
}
578+
CheckActionPreference(preferenceVariablePath, actionPreferenceValue, defaultPref);
588579
}
589580

590581
T convertedResult = (T)val;
@@ -611,6 +602,11 @@ internal T GetEnumPreference<T>(VariablePath preferenceVariablePath, T defaultPr
611602
result = (T)PSObject.Base(val);
612603
defaultUsed = false;
613604
}
605+
606+
if (result is ActionPreference actionPreferenceValue)
607+
{
608+
CheckActionPreference(preferenceVariablePath, actionPreferenceValue, defaultPref);
609+
}
614610
}
615611
catch (InvalidCastException)
616612
{
@@ -625,6 +621,18 @@ internal T GetEnumPreference<T>(VariablePath preferenceVariablePath, T defaultPr
625621
return result;
626622
}
627623

624+
private void CheckActionPreference(VariablePath preferenceVariablePath, ActionPreference preference, object defaultValue)
625+
{
626+
if (preference == ActionPreference.Suspend)
627+
{
628+
// ActionPreference.Suspend is reserved for future use. When it is used, reset
629+
// the variable to its default.
630+
string message = StringUtil.Format(ErrorPackage.ReservedActionPreferenceReplacedError, preference, preferenceVariablePath.UserPath, defaultValue);
631+
EngineSessionState.SetVariable(preferenceVariablePath, defaultValue, true, CommandOrigin.Internal);
632+
throw new NotSupportedException(message);
633+
}
634+
}
635+
628636
/// <summary>
629637
/// Same as GetEnumPreference, but for boolean values.
630638
/// </summary>

src/System.Management.Automation/engine/MshCommandRuntime.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3009,6 +3009,11 @@ internal ActionPreference DebugPreference
30093009

30103010
set
30113011
{
3012+
if (value == ActionPreference.Suspend)
3013+
{
3014+
throw PSTraceSource.NewNotSupportedException(ErrorPackage.ActionPreferenceReservedForFutureUseError, value);
3015+
}
3016+
30123017
_debugPreference = value;
30133018
_isDebugPreferenceSet = true;
30143019
}
@@ -3098,7 +3103,7 @@ internal ActionPreference WarningPreference
30983103
{
30993104
if (value == ActionPreference.Suspend)
31003105
{
3101-
throw PSTraceSource.NewNotSupportedException(ErrorPackage.SuspendActionPreferenceErrorActionOnly);
3106+
throw PSTraceSource.NewNotSupportedException(ErrorPackage.ActionPreferenceReservedForFutureUseError, value);
31023107
}
31033108

31043109
_warningPreference = value;
@@ -3268,7 +3273,7 @@ internal ActionPreference ErrorAction
32683273
{
32693274
if (value == ActionPreference.Suspend)
32703275
{
3271-
throw PSTraceSource.NewNotSupportedException(ErrorPackage.SuspendActionPreferenceSupportedOnlyOnWorkflow);
3276+
throw PSTraceSource.NewNotSupportedException(ErrorPackage.ActionPreferenceReservedForFutureUseError, value);
32723277
}
32733278

32743279
_errorAction = value;
@@ -3301,6 +3306,11 @@ internal ActionPreference ProgressPreference
33013306

33023307
set
33033308
{
3309+
if (value == ActionPreference.Suspend)
3310+
{
3311+
throw PSTraceSource.NewNotSupportedException(ErrorPackage.ActionPreferenceReservedForFutureUseError, value);
3312+
}
3313+
33043314
_progressPreference = value;
33053315
_isProgressPreferenceSet = true;
33063316
}
@@ -3335,7 +3345,7 @@ internal ActionPreference InformationPreference
33353345
{
33363346
if (value == ActionPreference.Suspend)
33373347
{
3338-
throw PSTraceSource.NewNotSupportedException(ErrorPackage.SuspendActionPreferenceErrorActionOnly);
3348+
throw PSTraceSource.NewNotSupportedException(ErrorPackage.ActionPreferenceReservedForFutureUseError, value);
33393349
}
33403350

33413351
_informationPreference = value;

src/System.Management.Automation/resources/ErrorPackage.resx

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,13 @@
126126
<data name="RedirectedException" xml:space="preserve">
127127
<value>Object "{0}" is reported as an error.</value>
128128
</data>
129-
<data name="SuspendActionPreferenceErrorActionOnly" xml:space="preserve">
130-
<value>The action preference of "Suspend" is supported only for ErrorAction.</value>
131-
<comment>"Suspend" and ErrorAction should not be localized</comment>
132-
</data>
133-
<data name="SuspendActionPreferenceSupportedOnlyOnWorkflow" xml:space="preserve">
134-
<value>The error action preference of "Suspend" is supported only on workflows.</value>
135-
<comment>"Suspend" should not be localized - it is a literal.</comment>
136-
</data>
137129
<data name="UnsupportedPreferenceError" xml:space="preserve">
138130
<value>The value {0} 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."</value>
139131
</data>
140-
<data name="UnsupportedPreferenceVariable" xml:space="preserve">
141-
<value>The value {0} is not supported for an ActionPreference variable. The provided value should be used only as a value for a preference parameter. For more information, see the Help topic, "about_Preference_Variables."</value>
132+
<data name="ActionPreferenceReservedForFutureUseError" xml:space="preserve">
133+
<value>The {0} ActionPreference value is reserved for future use and is not supported at this time. For more information about preference variables, see the Help topic, "about_Preference_Variables."</value>
134+
</data>
135+
<data name="ReservedActionPreferenceReplacedError" xml:space="preserve">
136+
<value>The {0} ActionPreference value is reserved for future use and is not supported at this time. It has been replaced in your {1} variable by the default value of {2}. For more information about preference variables, see the Help topic, "about_Preference_Variables."</value>
142137
</data>
143138
</root>

0 commit comments

Comments
 (0)