Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
yotsuda wants to merge3 commits intoPowerShell:master
base:master
Choose a base branch
Loading
fromyotsuda:fix/issue-12148-shouldprocess-verbosepreference

Conversation

@yotsuda
Copy link
Contributor

PR Summary

FixShouldProcess to respect$VerbosePreference variable in addition to-Verbose parameter.

PR Context

Fixes#12148

$PSCmdlet.ShouldProcess() was only emitting verbose output when the-Verbose parameter was explicitly specified, completely ignoring the$VerbosePreference variable setting. This is inconsistent with how other cmdlets handle verbose output.

The issue occurred becauseDoShouldProcess() andCalculatePossibleShouldProcessOptimization() were checkingthis.Verbose (which only tracks the-Verbose parameter flag) instead of callingIsWriteVerboseEnabled() (which properly checks both-Verbose parameter and$VerbosePreference variable).

Changes:

  • ModifiedMshCommandRuntime.cs to useIsWriteVerboseEnabled() instead ofthis.Verbose in two locations
  • Added comprehensive tests to verify the fix

Before:

functionTest-SP {    [CmdletBinding(SupportsShouldProcess)]param($Target="TestTarget",$Action="TestAction")if ($PSCmdlet.ShouldProcess($Target,$Action)) {"OK" }}$VerbosePreference='Continue'Test-SP# No verbose output (bug)

After:

functionTest-SP {    [CmdletBinding(SupportsShouldProcess)]param($Target="TestTarget",$Action="TestAction")if ($PSCmdlet.ShouldProcess($Target,$Action)) {"OK" }}$VerbosePreference='Continue'Test-SP# VERBOSE: Performing the operation "TestAction" on target "TestTarget". (fixed!)

PR Checklist

ShouldProcess was only emitting verbose output when the -Verboseparameter was explicitly specified, ignoring the $VerbosePreferencevariable setting.Changed to use IsWriteVerboseEnabled() instead of checking theVerbose flag directly in both DoShouldProcess() andCalculatePossibleShouldProcessOptimization(). This method properlyrespects both the -Verbose parameter and $VerbosePreference variable.
@iSazonoviSazonov added the CL-GeneralIndicates that a PR should be marked as a general cmdlet change in the Change Log labelNov 22, 2025
@iSazonov
Copy link
Collaborator

From#12148 (comment)

ShouldProgress needs to respect $ProgressPreference = 'Continue', but if -Verbose:$false, then that should take precedent.

@yotsuda Could you please add test for-Verbose:$false?

yotsuda reacted with thumbs up emoji


Context"VerbosePreference variable" {
It"Emits verbose output when VerbosePreference is Continue" {
$VerbosePreference='Continue'
Copy link
Collaborator

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.

Copy link
ContributorAuthor

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 in5d8a7d1. 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
Copy link
Contributor

CopilotAI left a 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 whereShouldProcess 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 inDoShouldProcess() andCalculatePossibleShouldProcessOptimization() from checkingthis.Verbose to callingIsWriteVerboseEnabled()
  • Added comprehensive test coverage for both$VerbosePreference variable and-Verbose parameter scenarios

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

FileDescription
src/System.Management.Automation/engine/MshCommandRuntime.csFixed two method calls to useIsWriteVerboseEnabled() instead ofthis.Verbose to properly respect both-Verbose parameter and$VerbosePreference variable
test/powershell/Language/Scripting/ShouldProcess.Tests.ps1Added new test file with comprehensive coverage forShouldProcess 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.

@iSazonov
Copy link
Collaborator

@yotsuda Please look test errors.

@yotsudayotsuda marked this pull request as draftNovember 24, 2025 22:44
- 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'
@yotsudayotsuda marked this pull request as ready for reviewNovember 26, 2025 04:28
@yotsuda
Copy link
ContributorAuthor

@yotsuda Please look test errors.

Fixed ince12d77. The original implementation usingIsWriteVerboseEnabled() was affected by-Debug parameter, causing unintended behavior.

Created a dedicated methodShouldEmitVerboseFromShouldProcess() that checks only:

  • -Verbose parameter (if explicitly set)
  • $VerbosePreference variable

Also updatedHistory.Tests.ps1 expected count (12 → 13) as it now correctly emits verbose output.
All CI checks passing.

@iSazonov
Copy link
Collaborator

The original implementation usingIsWriteVerboseEnabled() was affected by-Debug parameter, causing unintended behavior.

Hmm,IsWriteVerboseEnabled is not directly check Debug parameter. So it is not right fix.

@yotsuda
Copy link
ContributorAuthor

Hmm,IsWriteVerboseEnabled is not directly check Debug parameter. So it is not right fix.

@iSazonov You're right thatIsWriteVerboseEnabled() doesn't directly check-Debug. However, it uses theVerbosePreference property getter, which returnsContinue/Inquire when-Debug is set (line 3116-3128), before checking$VerbosePreference.

TheWG decision specifies only-Verbose and$VerbosePreference - not-Debug. So I createdShouldEmitVerboseFromShouldProcess() to check these two factors directly, bypassing the-Debug logic in the property getter.

@iSazonov
Copy link
Collaborator

Thanks for clarify!

I don't see that WG discussed the influence of-Debug switch presence on the issue. I believe that this parameter is taken into account in all preferences for a reason. We should probably ask for confirmation from the WG in original issue.

yotsuda reacted with thumbs up emoji

@iSazonoviSazonov added WG-Cmdletsgeneral cmdlet issues WG-NeedsReviewNeeds a review by the labeled Working Group labelsDec 5, 2025
@microsoft-github-policy-servicemicrosoft-github-policy-servicebot added the Review - NeededThe PR is being reviewed labelDec 12, 2025
@SteveL-MSFTSteveL-MSFT moved this toIn-Progress-PullRequests inCmdlets Working GroupDec 17, 2025
@SteveL-MSFTSteveL-MSFT removed the WG-Cmdletsgeneral cmdlet issues labelDec 17, 2025
@microsoft-github-policy-servicemicrosoft-github-policy-servicebot removed the Review - NeededThe PR is being reviewed labelDec 17, 2025
@SteveL-MSFT
Copy link
Member

SteveL-MSFT commentedDec 17, 2025
edited
Loading

Cmdlet-WG already reviewed and approved the issue, so this is good from a behavior perspective

@SteveL-MSFTSteveL-MSFT added Review - NeededThe PR is being reviewed and removed WG-NeedsReviewNeeds a review by the labeled Working Group labelsDec 17, 2025
@microsoft-github-policy-servicemicrosoft-github-policy-servicebot removed the Review - NeededThe PR is being reviewed labelDec 17, 2025
@SteveL-MSFTSteveL-MSFT added WG-Cmdletsgeneral cmdlet issues Review - NeededThe PR is being reviewed labelsDec 17, 2025
@microsoft-github-policy-servicemicrosoft-github-policy-servicebot removed the Review - NeededThe PR is being reviewed labelDec 17, 2025
@SteveL-MSFTSteveL-MSFT added Review - NeededThe PR is being reviewed WG-ReviewedA Working Group has reviewed this and made a recommendation labelsDec 17, 2025
@SteveL-MSFTSteveL-MSFT moved this fromIn-Progress-PullRequests toReviewed inCmdlets Working GroupDec 17, 2025
@iSazonov
Copy link
Collaborator

@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?

@microsoft-github-policy-servicemicrosoft-github-policy-servicebot removed the Review - NeededThe PR is being reviewed labelDec 17, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@iSazonoviSazonoviSazonov left review comments

Copilot code reviewCopilotCopilot left review comments

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

CL-GeneralIndicates that a PR should be marked as a general cmdlet change in the Change LogWG-Cmdletsgeneral cmdlet issuesWG-ReviewedA Working Group has reviewed this and made a recommendation

Projects

Status: Reviewed

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

ShouldProcess does not emit Verbose Output even if $VerbosePreference is set

3 participants

@yotsuda@iSazonov@SteveL-MSFT

[8]ページ先頭

©2009-2025 Movatter.jp