FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix for Move-Item with wildcard source and a non-existent destination directory implodes first directory by alexprabhat99 · Pull Request #26603 · PowerShell/PowerShell · GitHub

Fix for Move-Item with wildcard source and a non-existent destination directory implodes first directory - #26603

Open
Alex Prabhat Bara (alexprabhat99) wants to merge 3 commits into
PowerShell:masterfrom
alexprabhat99:bugfix-24837
Open

Fix for Move-Item with wildcard source and a non-existent destination directory implodes first directory#26603
Alex Prabhat Bara (alexprabhat99) wants to merge 3 commits into
PowerShell:masterfrom
alexprabhat99:bugfix-24837

Conversation

Alex Prabhat Bara (alexprabhat99) commented Dec 11, 2025
edited
Loading

Copy link
Copy Markdown

PR Summary

Fixes: #24837

PR Context

This PR fixes the follwing scenario:
When you call Move-Item with a wildcard into a directory that doesn't exist yet, it seems like the first move of a directory acts as a directory creation, which ends up moving the files into the new directory, instead of creating a new sub-directory, and I don't understand why / find this a rather odd behavior.

PR Checklist

Copy link
Copy Markdown
Author

Dongbo Wang (@daxian-dbw) Could you please review this PR as potential fix for the issue: #24837? Thank you.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Pull request overview

This PR fixes a bug where Move-Item with wildcard source paths to a non-existent destination directory would flatten the directory structure. The issue occurred because the first directory in the wildcard expansion was renamed to the destination path instead of being moved into it as a subdirectory, causing subsequent items to be moved into that first directory incorrectly.

Key changes:

  • Adds logic to pre-create the destination directory when it doesn't exist but its parent does
  • Adds a comprehensive test case to verify directory structure is preserved during wildcard moves
  • Makes single directory move behavior consistent with wildcard move behavior

Reviewed changes

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

File Description
src/System.Management.Automation/namespaces/FileSystemProvider.cs Modified MoveItem method to create the destination directory before moving if it doesn't exist but the parent directory does, ensuring consistent move-into behavior
test/powershell/Modules/Microsoft.PowerShell.Management/FileSystem.Tests.ps1 Added test case that verifies directory structure preservation when moving wildcard contents to a non-existent destination directory

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

Copy-Item -Path $zeroLengthFile -Destination "$TestDrive\zeroLengthFile2.txt" -Force
"$TestDrive\zeroLengthFile2.txt" | Should -Exist
}

Copilot AI Dec 11, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Consider adding a test comment explaining what specific bug this test is guarding against. While the test name is descriptive, a brief comment mentioning issue #24837 would help future maintainers understand the historical context and the specific flattening bug that was fixed.

Suggested change
# Regression test for issue #24837: Ensure Move-Item with wildcards preserves directory structure and does not flatten contents.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

We never add references to issues.

Directory.CreateDirectory(destination);
}
}

Copilot AI Dec 11, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Remove trailing whitespace at the end of this line to maintain code cleanliness and consistency with project standards.

Suggested change

Copilot uses AI. Check for mistakes.
Comment on lines +5841 to +5848
if (!ItemExists(destination))
{
string parentDir = Path.GetDirectoryName(destination);
if (!string.IsNullOrEmpty(parentDir) && Directory.Exists(parentDir))
{
Directory.CreateDirectory(destination);
}
}

Copilot AI Dec 11, 2025

Copy link

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

This fix introduces a breaking change in behavior for single directory moves to non-existent destinations. Previously, Move-Item source/dir -Destination dest/newname (where dest exists but newname doesn't) would rename dir to newname. With this fix, it creates newname and then moves dir into it, resulting in dest/newname/dir. This breaks the existing test "Verify Move-Item across devices for directory" at line 155 of FileSystem.Tests.ps1, which expects the directory contents to be at $destination/file.txt, not $destination/dir/file.txt. While this change makes wildcard and non-wildcard moves consistent, it's a breaking change that should be explicitly acknowledged and documented. The PR description incorrectly marks "User-facing changes: Not Applicable".

Suggested change
if (!ItemExists(destination))
{
string parentDir = Path.GetDirectoryName(destination);
if (!string.IsNullOrEmpty(parentDir) && Directory.Exists(parentDir))
{
Directory.CreateDirectory(destination);
}
}
// Restore previous behavior: if destination does not exist, and its parent exists,
// treat as a rename (do not create the destination directory).
// Only create the destination directory if moving multiple items or using wildcards.
// For single directory move, do not create destination directory.
// (No action needed here; skip creating destination directory.)
// If needed, logic for wildcard moves should be handled elsewhere.
// (Intentionally left blank to restore previous behavior.)

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

Alex Prabhat Bara (@alexprabhat99) Please look how Copilot fixed the issue in #26604. Copilot started with fix like yours and did some iterations to avoid side effects. If you agree with that fix you can grab it.

microsoft-github-policy-service Bot added the Review - Needed The PR is being reviewed label Dec 18, 2025

Copy link
Copy Markdown
Author

Hi Ilya (@iSazonov),
Sorry for the delay, I got caught up with a few things. I've updated the PR with the requested changes. Whenever you have a chance, I'd really appreciate it if you could take another look.
Thank you!

Copy link
Copy Markdown
Author

Dongbo Wang (@daxian-dbw) Could you please review this PR as potential fix for the issue: #24837? Thank you.

Copy link
Copy Markdown
Author

Hi Ilya (@iSazonov), just a gentle reminder when you have a chance. I’ve addressed the review comments and updated the PR accordingly. Could you please take another look? Thanks!

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Review - Needed The PR is being reviewed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Move-Item with wildcard source and a non-existent destination directory implodes first directory

3 participants


Back | FazBrowse Home | New Git URL