Visitar URL original
add experimental provider dynamic parameter errors by huong8373tt-beep · Pull Request #28165 · PowerShell/PowerShell · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions experimental-feature-linux.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"PSLoadAssemblyFromNativeCode",
"PSNativeWindowsTildeExpansion",
"PSProfileDSCResource",
"PSProviderDynamicParameterBindingErrors",
"PSSerializeJSONLongEnumAsNumber",
"PSRedirectToVariable",
"PSSubsystemPluginModel"
Expand Down
1 change: 1 addition & 0 deletions experimental-feature-windows.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"PSLoadAssemblyFromNativeCode",
"PSNativeWindowsTildeExpansion",
"PSProfileDSCResource",
"PSProviderDynamicParameterBindingErrors",
"PSSerializeJSONLongEnumAsNumber",
"PSRedirectToVariable",
"PSSubsystemPluginModel"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Microsoft.PowerShell.Commands
/// <remarks>
/// </remarks>
[Cmdlet(VerbsCommon.Get, "ChildItem", DefaultParameterSetName = "Items", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096492")]
public class GetChildItemCommand : CoreCommandBase
public class GetChildItemCommand : CoreCommandBase, IReportDynamicParameterBindingErrors
{
/// <summary>
/// The string declaration for the Items parameter set in this command.
Expand Down Expand Up @@ -200,6 +200,18 @@ public SwitchParameter Name
}
}

/// <summary>
/// Gets a value indicating whether provider-resolution failures should be reported while binding dynamic parameters.
/// </summary>
bool IReportDynamicParameterBindingErrors.ShouldReportDynamicParameterBindingErrors =>
ShouldReportDynamicParameterBindingErrors;

/// <summary>
/// Gets a value indicating whether provider-resolution failures should be reported while binding dynamic parameters.
/// </summary>
internal override bool ShouldReportDynamicParameterBindingErrors =>
ExperimentalFeature.IsEnabled(ExperimentalFeature.PSProviderDynamicParameterBindingErrors);

/// <summary>
/// A virtual method for retrieving the dynamic parameters for a cmdlet. Derived cmdlets
/// that require dynamic parameters should override this method and return the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ internal virtual SwitchParameter SuppressWildcardExpansion

private bool _suppressWildcardExpansion;

/// <summary>
/// Gets a value indicating whether dynamic parameter binding errors should be reported.
/// </summary>
internal virtual bool ShouldReportDynamicParameterBindingErrors => false;

/// <summary>
/// A virtual method for retrieving the dynamic parameters for a cmdlet. Derived cmdlets
/// that require dynamic parameters should override this method and return the
Expand Down Expand Up @@ -243,15 +248,15 @@ public object GetDynamicParameters()
{
_dynamicParameters = GetDynamicParameters(context);
}
catch (ItemNotFoundException)
catch (ItemNotFoundException) when (!ShouldReportDynamicParameterBindingErrors)
{
_dynamicParameters = null;
}
catch (ProviderNotFoundException)
catch (ProviderNotFoundException) when (!ShouldReportDynamicParameterBindingErrors)
{
_dynamicParameters = null;
}
catch (DriveNotFoundException)
catch (DriveNotFoundException) when (!ShouldReportDynamicParameterBindingErrors)
{
_dynamicParameters = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1620,7 +1620,10 @@ private void HandleCommandLineDynamicParameters(out ParameterBindingException ou
}
catch (Exception e) // Catch-all OK, this is a third-party callout
{
if (e is ProviderInvocationException)
if (e is ProviderInvocationException ||
(dynamicParameterCmdlet is IReportDynamicParameterBindingErrors reportErrors &&
reportErrors.ShouldReportDynamicParameterBindingErrors &&
e is ItemNotFoundException or ProviderNotFoundException or DriveNotFoundException))
{
throw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public class ExperimentalFeature

internal const string EngineSource = "PSEngine";
internal const string PSSerializeJSONLongEnumAsNumber = nameof(PSSerializeJSONLongEnumAsNumber);
internal const string PSProviderDynamicParameterBindingErrors = nameof(PSProviderDynamicParameterBindingErrors);
internal const string PSProfileDSCResource = "PSProfileDSCResource";

#endregion
Expand Down Expand Up @@ -111,6 +112,10 @@ static ExperimentalFeature()
name: PSSerializeJSONLongEnumAsNumber,
description: "Serialize enums based on long or ulong as an numeric value rather than the string representation when using ConvertTo-Json."
),
new ExperimentalFeature(
name: PSProviderDynamicParameterBindingErrors,
description: "Report errors raised while resolving provider dynamic parameters instead of suppressing them."
),
new ExperimentalFeature(
name: PSProfileDSCResource,
description: "DSC v3 resources for managing PowerShell profile."
Expand Down
11 changes: 11 additions & 0 deletions src/System.Management.Automation/engine/MshCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,17 @@ public interface IDynamicParameters
/// </returns>
object? GetDynamicParameters();
}

/// <summary>
/// Exposes whether exceptions raised while binding dynamic parameters should be reported.
/// </summary>
internal interface IReportDynamicParameterBindingErrors
{
/// <summary>
/// Gets a value indicating whether dynamic parameter binding errors should be reported.
/// </summary>
bool ShouldReportDynamicParameterBindingErrors { get; }
}
#nullable restore

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.

Describe 'Get-ChildItem with PSProviderDynamicParameterBindingErrors' -Tags 'CI' {
BeforeAll {
$isFeatureEnabled = [ExperimentalFeature]::IsEnabled('PSProviderDynamicParameterBindingErrors')
}

It 'reports a missing drive before binding file system dynamic parameters' -Skip:$(-not $isFeatureEnabled) {
{
Get-ChildItem -LiteralPath 'PSDynamicParameterBindingMissingDrive:\path' -File -ErrorAction Stop
} | Should -Throw -ErrorId 'DriveNotFound'
}

It 'reports a missing drive before binding directory dynamic parameters' -Skip:$(-not $isFeatureEnabled) {
{
Get-ChildItem -LiteralPath 'PSDynamicParameterBindingMissingDrive:\path' -Directory -ErrorAction Stop
} | Should -Throw -ErrorId 'DriveNotFound'
}

It 'reports a missing provider before binding file system dynamic parameters' -Skip:$(-not $isFeatureEnabled) {
{
Get-ChildItem -LiteralPath 'PSDynamicParameterBindingMissingProvider::path' -File -ErrorAction Stop
} | Should -Throw -ErrorId 'ProviderNotFound'
}

It 'continues to report file system dynamic parameters as unavailable for registry paths' -Skip:$(-not ($IsWindows -and $isFeatureEnabled)) {
{
Get-ChildItem -LiteralPath 'HKCU:\Software' -File -ErrorAction Stop
} | Should -Throw -ErrorId 'NamedParameterNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand'
}
}
1 change: 1 addition & 0 deletions test/tools/TestMetadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ExperimentalFeatures": {
"ExpTest.FeatureOne": [ "test/powershell/engine/ExperimentalFeature/ExperimentalFeature.Basic.Tests.ps1" ],
"PSProviderDynamicParameterBindingErrors": [ "test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.PSProviderDynamicParameterBindingErrors.Tests.ps1" ],
"PSSerializeJSONLongEnumAsNumber": [ "test/powershell/Modules/Microsoft.PowerShell.Utility/ConvertTo-Json.PSSerializeJSONLongEnumAsNumber.Tests.ps1" ]
}
}