Fix DirectoryInfo.CreateSubdirectory for root directories #117519
+47
−1
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.
DirectoryInfo.CreateSubdirectory
was incorrectly throwingArgumentException
when called on root directories (like"/"
on Unix or"C:\"
on Windows), preventing users from creating subdirectories under the filesystem root.Problem
The validation logic in
CreateSubdirectory
expected that the character immediately following the current directory path would be a directory separator when validating that the new path is a proper subdirectory. However, for root directories,Path.Combine
produces paths where this character is actually the first letter of the subdirectory name, not a separator.For example:
"/"
"test"
"/test"
't'
(not a directory separator)This caused the validation to fail and throw:
"The directory specified, 'test', is not a subdirectory of '/'"
Solution
Added a special case in the validation logic to allow subdirectory creation when the current path is a root directory by checking
PathInternal.IsRoot(trimmedCurrentPath)
. This preserves all existing security validations while enabling the legitimate use case of creating subdirectories under root directories.Testing
CreateSubdirectory
tests continue to pass (15/15)DirectoryInfo
tests pass (1575 passed, 17 skipped)The fix is minimal and surgical, changing only the specific validation condition while preserving all existing behavior.
Fixes #116087.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.