Fix DirectoryInfo.CreateSubdirectory failing on root directories#125708
Closed
Fix DirectoryInfo.CreateSubdirectory failing on root directories#125708
Conversation
Co-authored-by: danmoseley <6385855+danmoseley@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix CreateSubdirectory method for root directory
Fix DirectoryInfo.CreateSubdirectory failing on root directories
Mar 18, 2026
Member
|
Duplicate of #121906. |
Member
|
@jozkee thanks so why is issue still open? |
Member
|
Oh you closed. Thanks |
Member
|
Yeah, the issue was a lingering duplicate of #116087. It would've been nice if Copilot have discovered that it was a dupe and have closed/reported back instead of sending the PR. |
Member
|
Is its attribute edit in the pr correct? |
Member
|
It is though, I closed it because I thought it wasn't worth the pipeline run, but it has value. |
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
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
DirectoryInfo.CreateSubdirectory("foo")threwArgumentExceptionwhenDirectoryInforeferred to a root directory (C:\,/).Root cause:
Path.TrimEndingDirectorySeparatorintentionally preserves root path separators (since\inC:\and/in/are part of the path identity, not just trailing decoration). This lefttrimmedCurrentPath = "C:\", so the boundary checkIsDirectorySeparator(newPath[3])evaluated'f'instead of'\', causing the false rejection.Fix: Use
FullPath.TrimEnd(Path.DirectorySeparatorChar)instead, which strips the trailing separator unconditionally:C:\→C:→newPath[2] = '\'✓/→""→newPath[0] = '/'✓ (all absolute Unix paths start with/, so vacuousStartsWith("")is semantically correct here)Test: Extended the existing Unix root test from
[PlatformSpecific(TestPlatforms.Linux)]to[PlatformSpecific(TestPlatforms.AnyUnix)]for macOS coverage, consistent with other Unix-specific tests in the file. The Windows root test (CreateSubdirectory_RootDriveSubfolder_Windows) was already in place.Changes
src/libraries/System.Private.CoreLib/src/System/IO/DirectoryInfo.cs— replacePath.TrimEndingDirectorySeparator(FullPath.AsSpan())withFullPath.TrimEnd(Path.DirectorySeparatorChar)fortrimmedCurrentPathsrc/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/DirectoryInfo/CreateSubdirectory.cs— broaden Unix root test fromLinuxtoAnyUnixTesting
All 77
CreateSubdirectorytests pass, including the updated Unix root test.Original prompt
📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.