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

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

Merged
iSazonov merged 6 commits intoPowerShell:masterfromkborowinski:new-item-force-fix
Feb 7, 2025

Conversation

kborowinski
Copy link
Contributor

@kborowinskikborowinski commentedFeb 4, 2025
edited
Loading

PR Summary

Fix#21111

@SteveL-MSFT@iSazonov

This PR ensures thatNew-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

ThePowerShell 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

@@ -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);
Copy link
Collaborator

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?

Copy link
ContributorAuthor

@kborowinskikborowinskiFeb 4, 2025
edited
Loading

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.

Copy link
Collaborator

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?

Copy link
Collaborator

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.

kborowinski reacted with thumbs up emoji

Describe "New-Item -Force should throw an error for invalid characters in directory path" -Tags "CI" {
BeforeAll {
$invalidPath = Join-Path -Path $TestDrive -ChildPath 'Invalid?'
Copy link
Collaborator

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.

Copy link
ContributorAuthor

@kborowinskikborowinskiFeb 4, 2025
edited
Loading

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.

Copy link
Contributor

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.)

kborowinski and iSazonov reacted with thumbs up emoji
@iSazonoviSazonov added the CL-GeneralIndicates that a PR should be marked as a general cmdlet change in the Change Log labelFeb 4, 2025
@kborowinskikborowinski marked this pull request as draftFebruary 4, 2025 13:38
@kborowinskikborowinski marked this pull request as ready for reviewFebruary 4, 2025 14:37
@kborowinski
Copy link
ContributorAuthor

/azp run

@azure-pipelinesAzure Pipelines
Copy link

Commenter does not have sufficient privileges for PR 24936 in repo PowerShell/PowerShell

@TravisEz13
Copy link
Member

/azp run PowerShell-CI-linux-packaging, PowerShell-Windows-Packaging-CI

@azure-pipelinesAzure Pipelines
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@iSazonoviSazonov merged commit8854c00 intoPowerShell:masterFeb 7, 2025
37 checks passed
@microsoft-github-policy-serviceMicrosoft GitHub Policy Service
Copy link
Contributor

microsoft-github-policy-servicebot commentedFeb 7, 2025
edited by unfurl-linksbot
Loading

📣 Hey@kborowinski, how did we do? We would love to hear your feedback with the link below! 🗣️

🔗https://aka.ms/PSRepoFeedback

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

@mklement0mklement0mklement0 left review comments

@iSazonoviSazonoviSazonov approved these changes

Assignees
No one assigned
Labels
CL-GeneralIndicates that a PR should be marked as a general cmdlet change in the Change Log
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

New-Item -Force does not throw an error
4 participants
@kborowinski@TravisEz13@mklement0@iSazonov

[8]ページ先頭

©2009-2025 Movatter.jp