-
Notifications
You must be signed in to change notification settings - Fork 5.3k
fix: CreateSubdirectory failing for root directories #121906
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: CreateSubdirectory failing for root directories #121906
Conversation
Path.TrimEndingDirectorySeparator preserves trailing separators for root paths like "C:\" or "/", causing boundary validation to check the wrong index. Added explicit trim of trailing separator from trimmedCurrentPath to fix the boundary check for root directory cases. Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
|
Tagging subscribers to this area: @dotnet/area-system-io |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
that can be a test case |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
|
I added a test |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
…ortex/runtime into fix-directory-creating-root-folder
|
it seems Linux is also effected. var dir = new DirectoryInfo("/");
dir.CreateSubdirectory("test");I added a test for linux as well |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
|
linux tests are failing because of permission. the fact that it is trying to create the folder but gets Permission denied is an indication that this PR also fixes linux. |
|
@vcsjones |
if only the newly added tests in PR are failing, then change them to:
both cases are acceptable, either original assert passed or the op failed because of access issue. other exceptions are unexpected and should fail. |
Signed-off-by: Mohamadreza Nakhleh <neo.vortex@pm.me>
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/EnumerableTests.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/Directory/EnumerableTests.cs
Outdated
Show resolved
Hide resolved
…Directory/EnumerableTests.cs Co-authored-by: kasperk81 <83082615+kasperk81@users.noreply.github.com>
…Directory/EnumerableTests.cs Co-authored-by: kasperk81 <83082615+kasperk81@users.noreply.github.com>
…Directory/EnumerableTests.cs Co-authored-by: kasperk81 <83082615+kasperk81@users.noreply.github.com>
|
@vcsjones |
fixes #116087
Fix CreateSubdirectory failing for root directories
Summary
Fixes a bug where
DirectoryInfo.CreateSubdirectory(path)incorrectly throwsArgumentExceptionwhen called on root directory instances (e.g.,C:\).Problem
Path.TrimEndingDirectorySeparatorpreserves trailing separators for root paths to maintain valid path format. This causes the boundary validation logic to use an incorrect index when checking for directory separators.Example failure:
The check at index
[3]evaluates the character't'instead of the separator'\'at index[2], causing validation to fail.Solution
After calling
Path.TrimEndingDirectorySeparator, explicitly check iftrimmedCurrentPathstill has a trailing separator (root directory case) and remove it manually before performing boundary validation.This ensures consistent behavior:
trimmedCurrentPathnever ends with a separator, making the boundary index check work correctly for all directory types.Impact
C:\,D:\, )Testing