Skip to content

Conversation

@mfloyd16
Copy link

@mfloyd16 mfloyd16 commented Dec 1, 2025

…(ExecutionContext, CommandOrigin.Internal); add WhatIf test to assert single message (fixes #24434)

PR Summary

Fixes: double confirmation prompts for Set-Content when -Confirm is used (issue #24434).
Cause: Provider-level ClearContent calls ShouldProcess while cmdlet-level Set-Content also calls ShouldProcess, and the provider context was inheriting the current cmdlet reference.
Result: Only one confirmation prompt is shown for Set-Content.

PR Context

This PR helps by making it more efficient for users running "Set-Content -Path file.txt -Value "content" -Confirm", previously you would have to confirm twice due to what seemed like a bug, now users only have to confirm once.

PR Checklist

Copilot AI review requested due to automatic review settings December 1, 2025 03:22
@mfloyd16
Copy link
Author

mfloyd16 commented Dec 1, 2025

@microsoft-github-policy-service agree

@mfloyd16 please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

Copy link
Contributor

Copilot AI 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 where Set-Content -Confirm prompted users twice for confirmation (issue #24434). The root cause was that both the cmdlet-level Set-Content and the provider-level ClearContent method called ShouldProcess, and the provider context inherited the cmdlet reference, causing double prompts.

The fix creates a new CmdletProviderContext using CommandOrigin.Internal instead of copying the existing context, which prevents the provider's ShouldProcess from prompting users while preserving essential properties like Filter and SuppressWildcardExpansion.

  • Modifies BeforeOpenStreams to create a new context without the cmdlet reference
  • Adds a WhatIf test using transcript capture to verify single confirmation message

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Microsoft.PowerShell.Commands.Management/commands/management/SetContentCommand.cs Creates a new CmdletProviderContext with CommandOrigin.Internal to avoid double ShouldProcess prompts when clearing content, while preserving essential context properties
test/powershell/Modules/Microsoft.PowerShell.Management/Set-Content.Tests.ps1 Adds a test using Start-Transcript to verify Set-Content -WhatIf produces only one confirmation message

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +38 to +39
Filter = currentContext.Filter,
};
Copy link

Copilot AI Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new context is missing several properties that may affect the provider's behavior during ClearContent:

  1. Force: Required for clearing content from hidden/readonly files (see FileSystemProvider.ClearContent line 7063)
  2. Include/Exclude: Filter patterns that may be needed by some providers
  3. DynamicParameters: Provider-specific parameters (e.g., -Stream for alternate data streams)
  4. Drive: The current drive context
  5. Credential: Authentication credentials if specified

These properties are set in the base class's CmdletProviderContext getter and should be copied to maintain consistent behavior. Consider copying these properties similar to how the CmdletProviderContext copy constructor does (CoreCommandContext.cs lines 265-275):

CmdletProviderContext context = new(currentContext.ExecutionContext, CommandOrigin.Internal)
{
    SuppressWildcardExpansion = currentContext.SuppressWildcardExpansion,
    Filter = currentContext.Filter,
    Force = currentContext.Force,
    DynamicParameters = currentContext.DynamicParameters,
    Drive = currentContext.Drive
};
context.SetFilters(currentContext.Include, currentContext.Exclude, currentContext.Filter);
Suggested change
Filter = currentContext.Filter,
};
Filter = currentContext.Filter,
Force = currentContext.Force,
DynamicParameters = currentContext.DynamicParameters,
Drive = currentContext.Drive,
Credential = currentContext.Credential
};
context.SetFilters(currentContext.Include, currentContext.Exclude, currentContext.Filter);

Copilot uses AI. Check for mistakes.
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Dec 3, 2025
@iSazonov
Copy link
Collaborator

iSazonov commented Dec 3, 2025

@mfloyd16 Please look failed tests.

@microsoft-github-policy-service microsoft-github-policy-service bot added the Review - Needed The PR is being reviewed label Dec 12, 2025
@microsoft-github-policy-service
Copy link
Contributor

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Maintainer, please provide feedback and/or mark it as Waiting on Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Set-Content -Confirm confirms twice

2 participants