Repository navigation
Improve error for multi-item Copy-Item to non-container destination - #28588
Jacob Jefferson Cochran (jacjcoch) wants to merge 1 commit into
Conversation
Detect ambiguous multi-source Copy-Item operations where the destination is not an existing container and at least one source is a container. Fail early with a clear error instead of partially copying and throwing "Container cannot be copied onto existing leaf item". Co-authored-by: Iceman Free
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
|
Jacob Jefferson Cochran (@jacjcoch) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
2 similar comments
|
Jacob Jefferson Cochran (@jacjcoch) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
|
Jacob Jefferson Cochran (@jacjcoch) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
There was a problem hiding this comment.
🟡 Changes recommended
A missing source now aborts the entire Copy-Item batch even with -ErrorAction Continue, leaving valid sources uncopied regardless of their position. Separately, copying from multiple drives reuses the last source’s drive context, causing -PassThru results to report the wrong PSDrive.
2 open issues
What changed in this PR
Addresses #27478 by detecting ambiguous local multi-source Copy-Item operations before copying, replacing partial results with a clearer error.
Changes:
- Batches explicit source paths and adds destination preflight validation.
- Adds
CopyItemDestinationIsNotContainerresource entries. - Adds six regression tests covering rejected operations and preserved behavior.
| File | Description |
|---|---|
| test/powershell/Modules/Microsoft.PowerShell.Management/Copy-Item.Tests.ps1 | Adds six regression tests. |
| src/System.Management.Automation/resources/zh-Hant/SessionStateStrings.zh-Hant.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/zh-Hans/SessionStateStrings.zh-Hans.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/tr/SessionStateStrings.tr.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/SessionStateStrings.resx | Defines the new diagnostic. |
| src/System.Management.Automation/resources/ru/SessionStateStrings.ru.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/pt-BR/SessionStateStrings.pt-BR.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/pl/SessionStateStrings.pl.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/ko/SessionStateStrings.ko.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/ja/SessionStateStrings.ja.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/it/SessionStateStrings.it.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/fr/SessionStateStrings.fr.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/es/SessionStateStrings.es.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/de/SessionStateStrings.de.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/resources/cs/SessionStateStrings.cs.resx | Adds English diagnostic entry. |
| src/System.Management.Automation/engine/SessionStateContainer.cs | Resolves sources before destination validation and copying. |
| src/System.Management.Automation/engine/ItemCmdletProviderInterfaces.cs | Adds internal array-based copy overload. |
| src/Microsoft.PowerShell.Commands.Management/commands/management/Navigation.cs | Submits source paths as one batch. |
Review effort: Balanced
| continue; | ||
| } | ||
| // Now do the copy | ||
| InvokeProvider.Item.Copy(_paths, Destination, Recurse, copyContainers, currentCommandContext); |
| } | ||
|
|
||
| bool destinationIsContainer = false; | ||
| resolvedPaths.Add((providerInstance, providerPaths)); |

Detect ambiguous multi-source Copy-Item operations where the destination is not an existing container and at least one source is a container. Fail early with a clear error instead of partially copying and throwing "Container cannot be copied onto existing leaf item".
Co-authored-by: Iceman Free
PR Summary
Fix
Copy-Itemhandling for the narrow ambiguous case in #27478. When multiple resolved source items target a destination that is not an existing container and at least one source is a container,Copy-Itemnow fails early with a clear error instead of copying the first container, then failing withContainer cannot be copied onto existing leaf item.PR Context
Fixes #27478.
The engine previously computed destination-container state once before iterating resolved source paths. For wildcard/multi-source container copies to a non-existing destination, the first container could create the destination path; subsequent container sources then hit a stale non-container check and produced a misleading error after a partial copy.
This change follows the WG direction for #27478: provide a clearer error and avoid a breaking semantic redesign. The fix is intentionally narrow:
CopyItemDestinationIsNotContainerbefore any copy.Implementation includes batching
CopyItemCommandpaths into one engine copy call so multi-explicit-path cases are covered by the same preflight, and adding the corresponding internalItemCmdletProviderIntrinsics.Copy(string[] ...)overload.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerNotes for reviewers
CopyItemDestinationIsNotContainer. Leaf-only multi-source copy to a non-existing destination remains unchanged.CopyItemDestinationIsNotContainer.test/powershell/Modules/Microsoft.PowerShell.Management/Copy-Item.Tests.ps1. Verified on Linux with builtpwsh+ Pester 4.10.1: 21 passed, 0 failed, 1 skipped (existing Windows-only local test). Windows-gated remoteCopy-Itemtests in the same file were not run on Linux.