-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Difference in File/DirectoryNotFoundException behavior when both directory and file don't exist #21745
Comments
Hmmm. We have code in the Unix implementation especially to deal with what the comment claims is a difference: |
Are you talking about the trimming of the final separator change to the original logic? You added the original check for this, didn't you? |
Ok, so normally the Windows behavior matches what we're seeing on Unix. You get PATH_NOT_FOUND if the path to the left of the last separator doesn't exist. The kicker here is that on desktop we explicitly ignore the error value ( The error always comes back as FileNotFound for FileInfo as a result. (Assuming it doesn't exist as a directory.) Ugh. If someone else gets a chance to take this before Thursday please assign it to yourself. Otherwise I'll take it then. |
Note that I validated the Windows error behavior for both |
I'm talking about the whole thing. I must have added this in response to something failing, but none if the existing tests are failing without it, and the behavior on Windows doesn't match the comment. |
I remember us discussing this. If I recall correctly, the issue was that Windows would provide more information in the error that we would use to determine whether to throw a DirectoryNotFound or a FileNotFound. On Unix the exceptions thrown from the interop call didn't include that info, so we did some manual parsing to throw a DirectoryNotFound or a FileNotFound depending on which piece was missing. An example of this is way back when I added the parsing to the FileInfo CopyTo function. We had the same issue there and did the directory Exists check to throw a DNFE.
Could this be because our interop exception catching/rethrowing logic has changed? |
The problem is that in the case of the Info wrappers we ignored the actual error on NetFX. It is just obfuscated a bit as Refresh() explicitly asks to ignore errors on not found. I'm working on this now, writing a full set of tests that match desktop. |
Hey guys, it's great to see issues reported being picked up so quickly (and discussed openly here). Really enjoying this!! Thanks! |
Ah! So am I correct in understanding that this same logic is exercised via File/Directory (rather than FileInfo/DirectoryInfo), and my comment / fix was relevant to the former but causes problems for the latter? I guess that makes me feel a bit better that I wasn't totally crazy ;)
Excellent :) |
And indeed changing the code from: long x = new FileInfo(@"C:\users\stoub\doesntExist\doesntExist.txt").Length; to: File.GetAttributes(@"C:\users\stoub\doesntExist\doesntExist.txt"); results in a DirectoryNotFoundException instead of a FileNotFoundException. Gotta love inconsistency 😦 |
Yeah- in this case it looks accidental as well (or at best evil). After the if (_dataInitialised == -1)
Refresh();
if (_dataInitialised != 0) // Refresh was unable to initialise the data
__Error.WinIOError(_dataInitialised, DisplayPath);
if ((_data.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) != 0)
__Error.WinIOError(Win32Native.ERROR_FILE_NOT_FOUND, DisplayPath);
return ((long)_data.fileSizeHigh) << 32 | ((long)_data.fileSizeLow & 0xFFFFFFFFL); |
@JeremyKuhne, this can be closed now, right? |
Could dotnet/corefx#20456 be related to the discussion here? |
dotnet/coreclr#11757 fixed this in CoreCLR for master. |
This code on Windows:
throws a FileNotFoundException, whereas this code on Linux:
throws a DirectoryNotFoundException. (If the whole directory exists and just the file doesn't, then we throw a FileNotFoundException on Linux as expected.)
Difference reported by @polarapfel at https://github.com/dotnet/corefx/issues/1728#issuecomment-301935624
cc: @ianhays, @JeremyKuhne
The text was updated successfully, but these errors were encountered: