-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Respect $VerbosePreference in ShouldProcess #26511
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Respect $VerbosePreference in ShouldProcess #26511
Conversation
ShouldProcess was only emitting verbose output when the -Verbose parameter was explicitly specified, ignoring the $VerbosePreference variable setting. Changed to use IsWriteVerboseEnabled() instead of checking the Verbose flag directly in both DoShouldProcess() and CalculatePossibleShouldProcessOptimization(). This method properly respects both the -Verbose parameter and $VerbosePreference variable.
|
From #12148 (comment)
@yotsuda Could you please add test for |
|
|
||
| Context "VerbosePreference variable" { | ||
| It "Emits verbose output when VerbosePreference is Continue" { | ||
| $VerbosePreference = 'Continue' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please save and then restore the value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review! Fixed in 5d8a7d1. All tests now save and restore $VerbosePreference using try-finally blocks.
- Add test case for -Verbose:$false taking precedence over $VerbosePreference - Save and restore $VerbosePreference in all test cases using try-finally blocks - Clean up whitespace
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR fixes a bug where ShouldProcess was only emitting verbose output when the -Verbose parameter was explicitly specified, completely ignoring the $VerbosePreference variable. This makes the behavior consistent with how other cmdlets handle verbose output by properly checking both the parameter and the preference variable.
Key Changes:
- Changed two locations in
DoShouldProcess()andCalculatePossibleShouldProcessOptimization()from checkingthis.Verboseto callingIsWriteVerboseEnabled() - Added comprehensive test coverage for both
$VerbosePreferencevariable and-Verboseparameter scenarios
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/System.Management.Automation/engine/MshCommandRuntime.cs |
Fixed two method calls to use IsWriteVerboseEnabled() instead of this.Verbose to properly respect both -Verbose parameter and $VerbosePreference variable |
test/powershell/Language/Scripting/ShouldProcess.Tests.ps1 |
Added new test file with comprehensive coverage for ShouldProcess verbose output behavior with both preference variable and parameter scenarios |
Comments suppressed due to low confidence (1)
src/System.Management.Automation/engine/MshCommandRuntime.cs:1649
- Both branches of this 'if' statement return - consider using '?' to express intent better.
if (IsWriteVerboseEnabled())
{
return ShouldProcessPossibleOptimization.AutoYes_CanCallShouldProcessAsynchronously;
}
else
{
return ShouldProcessPossibleOptimization.AutoYes_CanSkipShouldProcessCall;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@yotsuda Please look test errors. |
- Add ShouldEmitVerboseFromShouldProcess() method that checks only -Verbose parameter and $VerbosePreference variable - Excludes -Debug parameter effect to avoid unintended side effects - Update History.Tests.ps1 expected count from 12 to 13 to reflect the correct behavior when $VerbosePreference is 'Continue'
Fixed in ce12d77. The original implementation using Created a dedicated method
Also updated |
Hmm, |
@iSazonov You're right that The WG decision specifies only |
|
Thanks for clarify! I don't see that WG discussed the influence of |
|
Cmdlet-WG already reviewed and approved the issue, so this is good from a behavior perspective |
|
@SteveL-MSFT It is not clear should we take into account the influence of -Debug switch presence or ignore it as implemented in the PR? |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
PR Summary
Fix
ShouldProcessto respect$VerbosePreferencevariable in addition to-Verboseparameter.PR Context
Fixes #12148
$PSCmdlet.ShouldProcess()was only emitting verbose output when the-Verboseparameter was explicitly specified, completely ignoring the$VerbosePreferencevariable setting. This is inconsistent with how other cmdlets handle verbose output.The issue occurred because
DoShouldProcess()andCalculatePossibleShouldProcessOptimization()were checkingthis.Verbose(which only tracks the-Verboseparameter flag) instead of callingIsWriteVerboseEnabled()(which properly checks both-Verboseparameter and$VerbosePreferencevariable).Changes:
MshCommandRuntime.csto useIsWriteVerboseEnabled()instead ofthis.Verbosein two locationsBefore:
After:
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header