- Notifications
You must be signed in to change notification settings - Fork7.7k
FixNew-Item -Force
to error on invalid directory name#24936
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
@@ -62,6 +62,9 @@ public sealed partial class FileSystemProvider : NavigationCmdletProvider, | |||
private const int COPY_FILE_ACTIVITY_ID = 0; | |||
private const int REMOVE_FILE_ACTIVITY_ID = 0; | |||
// Windows error code for invalid characters | |||
private const int ERROR_INVALID_NAME = unchecked((int)0x8007007B); |
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.
What about Linux and MacOS?
kborowinskiFeb 4, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
Unfortunately, I have limited knowledge about Linux or macOS. From what I have read, the only invalid character for directory name on Linux is/
(forward slash). I did try to force an exception on Ubuntu withNew-Item -Path 'Invalid/Folder/Name' -Type Directory
, and it does not error but instead creates a folder structure. What I can do is to introduce check to not suppressIOException
when on Windows.
Perhaps someone with genuine Linux/macOS expertise can advance this commit. If this is not feasible, and the proposed solution is not acceptable, we should consider closing this PR.
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.
@mklement0 What do you think about non-Windows OS-s? Do you agree that the fix should be only for Windows?
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.
Since we don't useRuntimeInformation.IsOSPlatform()
in the file, we prefer to wrap code with#if UNIX
. So I suggest to move the const to code where it is used and use the directive.
Describe "New-Item -Force should throw an error for invalid characters in directory path" -Tags "CI" { | ||
BeforeAll { | ||
$invalidPath = Join-Path -Path $TestDrive -ChildPath 'Invalid?' |
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.
The test should work on Linux and MacOS too.
kborowinskiFeb 4, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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.
As mentioned before, I was not able to force invalid directory name error on Ubuntu, therefore I just propose to skip that test when running on Linux or macOS, or if not feasible, close this PR.
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.
I think it's fine to limit the tests to Windows. The only way to construct an invalid directory path that I'm aware of on Unix-like platforms is to includeNUL
(`0
) characters, but there's a dedicated check for that already in place (Null character in path.
)
/azp run |
Commenter does not have sufficient privileges for PR 24936 in repo PowerShell/PowerShell |
/azp run PowerShell-CI-linux-packaging, PowerShell-Windows-Packaging-CI |
Azure Pipelines successfully started running 1 pipeline(s). |
8854c00
intoPowerShell:masterUh oh!
There was an error while loading.Please reload this page.
microsoft-github-policy-servicebot commentedFeb 7, 2025 • edited by unfurl-linksbot
Loading Uh oh!
There was an error while loading.Please reload this page.
edited by unfurl-linksbot
Uh oh!
There was an error while loading.Please reload this page.
📣 Hey@kborowinski, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗https://aka.ms/PSRepoFeedback |
Uh oh!
There was an error while loading.Please reload this page.
PR Summary
Fix#21111
@SteveL-MSFT@iSazonov
This PR ensures that
New-Item -Path 'Invalid?' -Force
correctly throws an error instead of being suppressed. The change specifically checks forIOException
with theHResult
code related to invalid characters and allows the exception to propagate, aligning behavior with PowerShell 5.1.PR Context
The
PowerShell Working Group
discussed this issue and agreed that, in the case of invalid characters, the cmdlet should return an error just like in PowerShell 5.1, even when-Force
is used. The primary use case of-Force
is to ensure success whether the target directory exists, but suppressing invalid character errors introduces inconsistencies. This PR implements a targeted fix by checking for the specificHResult
associated with invalid characters, ensuring that such exceptions are not suppressed while maintaining existing behavior for other cases.PR Checklist
.h
,.cpp
,.cs
,.ps1
and.psm1
files have the correct copyright headerWIP:
or[ WIP ]
to the beginning of the title (theWIP
bot will keep its status check atPending
while the prefix is present) and remove the prefix when the PR is ready.- [ ] Issue filed:
(which runs in a different PS Host).