-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Systematically use FileSystems abstraction instead of BCL file operations #12602
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
Systematically use FileSystems abstraction instead of BCL file operations #12602
Conversation
Co-authored-by: YuliiaKovalova <[email protected]>
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.
Pull Request Overview
Refactors file system interactions to consistently use the FileSystems abstraction instead of direct BCL File/Directory APIs, improving testability and platform adaptability.
Key changes:
- Replaced File/Directory.Exists, ReadAllText/ReadAllBytes, GetLastWriteTimeUtc, and combined file/directory existence checks with FileSystems.Default equivalents.
- Added necessary using Microsoft.Build.Shared.FileSystem directives where required.
- Adjusted a unit test (TaskHostFactory_Tests) logic and removed auxiliary diagnostic tracking code.
Reviewed Changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 1 comment.
Show a summary per file
File | Description |
---|---|
src/Shared/TaskFactoryUtilities.cs | Switched file existence and byte reads to FileSystems abstraction. |
src/Shared/MSBuildLoadContext.cs | Resolver initialization now uses FileSystems for existence checks. |
src/Shared/FrameworkLocationHelper.cs | Directory and file existence checks now use abstraction. |
src/Shared/FileUtilities.cs | Case sensitivity probe uses FileSystems for existence check. |
src/Shared/ExceptionHandling.cs | Uses abstraction for timestamp and text reads. |
src/Shared/Debugging/DebugUtils.cs | Loop now uses FileSystems for file existence. |
src/Shared/BuildEnvironmentHelper.cs | Conditional existence delegate uses abstraction. |
src/Build/Utilities/EngineFileUtilities.cs | Directory existence cached via FileSystems. |
src/Build/Logging/BinaryLogger/ProjectImportsCollector.cs | Cache dir and file existence checks updated. |
src/Build/Evaluation/IntrinsicFunctions.cs | Assembly path existence check updated. |
src/Build/Evaluation/Expander.cs | Combined file/dir existence replaced with FileOrDirectoryExists. |
src/Build/Evaluation/Evaluator.cs | dotnet host path existence check updated. |
src/Build/Construction/Solution/SolutionFile.cs | Reads solution filter via abstraction. |
src/Build/Construction/ProjectRootElement.cs | XML file content read via abstraction. |
src/Build/BuildCheck/Infrastructure/EditorConfig/EditorConfigParser.cs | Reads .editorconfig via abstraction. |
src/Build/BuildCheck/Checks/UntrustedLocationCheck.cs | Directory existence via abstraction. |
src/Build/BackEnd/Components/Scheduler/Scheduler.cs | Debug dump existence check updated. |
src/Build/BackEnd/Components/FileAccesses/FileAccessManager.cs | Dummy file probe uses abstraction. |
src/Build/BackEnd/Components/Communications/CurrentHost.cs | Host resolution uses abstraction. |
src/Build/BackEnd/BuildManager/BuildManager.cs | Input cache existence validation updated. |
src/Build/BackEnd/Components/FileAccesses/FileAccessManager.cs | File existence probe updated. |
src/Build.UnitTests/BackEnd/TaskHostFactory_Tests.cs | Adjusted build invocation logic; removed diagnostic helpers; added comment. |
a548b8a
to
eae268e
Compare
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.
@copilot you should systematically go through all methods defined in FileSystems abstraction, even in test files, you missed a lot of stuff, and check that all possible usage is using the FileSystems
You're absolutely right - I missed many instances. I'll systematically go through all production code files (33 files identified) and replace ALL BCL file operations with FileSystems abstraction. I'll also review test files to ensure consistency. Working on this now. |
Co-authored-by: JanProvaznik <[email protected]>
Co-authored-by: JanProvaznik <[email protected]>
Co-authored-by: JanProvaznik <[email protected]>
Summary
This PR addresses issue #12601 by systematically replacing direct BCL (Base Class Library) file system operations with the
FileSystems
abstraction throughout the production codebase for consistency and better testability.Problem
The codebase was inconsistently using both the
FileSystems.Default
abstraction and direct BCL methods (File.Exists
,File.ReadAllText
,Directory.Exists
, etc.) for file system operations. This inconsistency made the code harder to test and maintain, and bypassed the platform-specific optimizations available in the abstraction layer.Changes
Following feedback to systematically review all production code, this PR replaces direct BCL file system calls with
FileSystems.Default
methods in 30+ files across multiple subsystems:Read Operations Replaced
File.ReadAllText()
→FileSystems.Default.ReadFileAllText()
(8+ instances)File.ReadAllBytes()
→FileSystems.Default.ReadFileAllBytes()
(5+ instances)File.Exists()
→FileSystems.Default.FileExists()
(~30+ instances)Directory.Exists()
→FileSystems.Default.DirectoryExists()
(~15+ instances)File.GetLastWriteTimeUtc()
→FileSystems.Default.GetLastWriteTimeUtc()
(4+ instances)Directory.EnumerateFiles()
→FileSystems.Default.EnumerateFiles()
(2+ instances)Directory.EnumerateDirectories()
→FileSystems.Default.EnumerateDirectories()
(1+ instance)File.Exists() || Directory.Exists()
→FileSystems.Default.FileOrDirectoryExists()
(1 instance)Files Modified (30+ files)
Scope Notes
Benefits
Testing
All changes are minimal and surgical, maintaining existing behavior while improving code consistency.
Fixes #12601
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.