diff --git a/src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs b/src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs index cb39fcb7e03047..df790a51b8d9f6 100644 --- a/src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs +++ b/src/libraries/System.IO.FileSystem/src/System/IO/FileSystem.Unix.cs @@ -381,6 +381,19 @@ public static void MoveDirectory(string sourceFullPath, string destFullPath) throw new IOException(SR.Format(SR.IO_AlreadyExists_Name, destFullPath)); } +#if TARGET_BROWSER + // renaming a file doesn't return correct error code on emscripten if one of the parent paths does not exist, + // manually workaround it for now (https://github.com/dotnet/runtime/issues/40305) + if (!Directory.Exists(Path.GetDirectoryName(sourceFullPath))) + { + throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, sourceFullPath)); + } + if (!Directory.Exists(Path.GetDirectoryName(destFullPath))) + { + throw new DirectoryNotFoundException(SR.Format(SR.IO_PathNotFound_Path, destFullPath)); + } +#endif + if (Interop.Sys.Rename(sourceFullPath, destFullPath) < 0) { Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();