Uh oh!
There was an error while loading. Please reload this page.
Add missing Invalidate() calls to FileInfo state-changing methods - #124429
Conversation
Fixesdotnet#117709 — FileInfo methods that alter file system state now call Invalidate() so cached properties (Exists, Length, timestamps) are lazily refreshed on next access, matching the pattern already used by Create(), Delete(), and MoveTo(). Methods fixed (FileInfo.cs): - Open(FileStreamOptions) - Open(FileMode, FileAccess, FileShare) - OpenRead() - OpenWrite() - OpenText() - Replace(string, string?) - Encrypt() - Decrypt() - CreateAsHardLink(string) Comprehensive method audit across FileInfo, DirectoryInfo, FileSystemInfo: FileInfo — already correct (no change needed): Create(), Delete(), MoveTo(), CreateText(), AppendText(), CopyTo() (returns new obj), IsReadOnly setter (via Attributes) DirectoryInfo — already correct (no change needed): Create(), Delete(), Delete(bool), MoveTo() CreateSubdirectory() returns new obj, parent unchanged FileSystemInfo — already correct (no change needed): CreateAsSymbolicLink(), Attributes setter, CreationTime(Utc) setter, LastAccessTime(Utc) setter, LastWriteTime(Utc) setter, UnixFileMode setter, ResolveLinkTarget() (read-only) Read-only methods (no invalidation needed): GetFiles, GetDirectories, GetFileSystemInfos, Enumerate* Includes 12 regression tests in InvalidateOnStateChange.cs covering all fixed methods plus CreateText/AppendText (already worked). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tagging subscribers to this area: @dotnet/area-system-io |
There was a problem hiding this comment.
Pull request overview
This pull request fixes issue #117709 by ensuring FileInfo methods that alter or access file system state consistently call Invalidate() to refresh cached metadata. The fix addresses an inconsistency where some state-changing methods (like Create() and Delete()) called Invalidate() while others (like Open(FileMode), OpenRead(), etc.) did not, leading to stale cached properties.
Changes:
- Added
Invalidate()calls to 9 FileInfo methods that were missing them (Open variants, Replace, Encrypt/Decrypt, CreateAsHardLink) - Added comprehensive regression tests (12 test methods) covering all fixed methods plus verification of existing correct behavior
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/IO/FileInfo.cs | Added Invalidate() calls to Open(FileStreamOptions), Open(FileMode, FileAccess, FileShare), OpenRead(), OpenWrite(), OpenText(), Replace(), Encrypt(), Decrypt(), and CreateAsHardLink() methods |
| src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/FileInfo/InvalidateOnStateChange.cs | New comprehensive test file with 12 tests verifying Invalidate() behavior for all file-opening and state-changing methods |
| src/libraries/System.Runtime/tests/System.IO.FileSystem.Tests/System.IO.FileSystem.Tests.csproj | Added reference to new test file |
…filesystems The CreateAsHardLink_InvalidatesExists test fails with 'Too many links' (EMLINK) on filesystems that do not support hard links. Guard the test with MountHelper.CanCreateHardLinks, consistent with BaseHardLinks_FileSystem. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Uh oh!
There was an error while loading. Please reload this page.
jeffhandley
commented
Mar 8, 2026
Thanks @lufen! Nice work with the comprehensive audit; thanks for sharing those notes in the PR description. |
Fixes#117709 — FileInfo methods that alter file system state now call Invalidate() so cached properties (Exists, Length, timestamps) are lazily refreshed on next access, matching the pattern already used by Create(), Delete(), and MoveTo().
Methods fixed (FileInfo.cs):
Comprehensive method audit across FileInfo, DirectoryInfo, FileSystemInfo:
FileInfo — already correct (no change needed):
Create(), Delete(), MoveTo(), CreateText(), AppendText(),
CopyTo() (returns new obj), IsReadOnly setter (via Attributes)
DirectoryInfo — already correct (no change needed):
Create(), Delete(), Delete(bool), MoveTo()
CreateSubdirectory() returns new obj, parent unchanged
FileSystemInfo — already correct (no change needed):
CreateAsSymbolicLink(), Attributes setter, CreationTime(Utc) setter,
LastAccessTime(Utc) setter, LastWriteTime(Utc) setter,
UnixFileMode setter, ResolveLinkTarget() (read-only)
Read-only methods (no invalidation needed):
GetFiles, GetDirectories, GetFileSystemInfos, Enumerate*
Includes 12 regression tests in InvalidateOnStateChange.cs covering all fixed methods plus CreateText/AppendText (already worked).