From 214dd4f12eb0d8c8a5778b78b683dd6fef84e095 Mon Sep 17 00:00:00 2001 From: Tanay Parikh Date: Wed, 10 Jun 2020 12:26:17 -0700 Subject: [PATCH 1/2] Safely handle DirectoryNotFound Essentially the same thing as our `FileNotFoundException` handling, however here we also have to fabricate the directory tree. Fixes https://github.com/dotnet/aspnetcore/issues/22795 --- .../DefaultRemoteTextLoaderFactory.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/DefaultRemoteTextLoaderFactory.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/DefaultRemoteTextLoaderFactory.cs index 30ec8401cc7..9309034672d 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/DefaultRemoteTextLoaderFactory.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/DefaultRemoteTextLoaderFactory.cs @@ -78,6 +78,12 @@ public override Task LoadTextAndVersionAsync(Workspace workspace // in us trying to refresh the "closed" files buffer with what's on disk; however, there's nothing actually on disk because the file was renamed. textAndVersion = TextAndVersion.Create(SourceText.From(string.Empty), VersionStamp.Default, filePath: _filePath); } + catch (DirectoryNotFoundException) + { + var directory = Path.GetDirectoryName(_filePath); + Directory.CreateDirectory(directory); + textAndVersion = TextAndVersion.Create(SourceText.From(string.Empty), VersionStamp.Default, filePath: _filePath); + } return Task.FromResult(textAndVersion); } From a6cb8d94278a3717445aacbdf33db94bd244f5d6 Mon Sep 17 00:00:00 2001 From: Tanay Parikh Date: Wed, 10 Jun 2020 17:30:34 -0700 Subject: [PATCH 2/2] Catch all --- .../DefaultRemoteTextLoaderFactory.cs | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/DefaultRemoteTextLoaderFactory.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/DefaultRemoteTextLoaderFactory.cs index 9309034672d..5f50fa959c1 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/DefaultRemoteTextLoaderFactory.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer.Common/DefaultRemoteTextLoaderFactory.cs @@ -72,18 +72,12 @@ public override Task LoadTextAndVersionAsync(Workspace workspace throw new IOException($"File was externally modified: {_filePath}"); } } - catch (FileNotFoundException) + catch (IOException e) when (e is DirectoryNotFoundException || e is FileNotFoundException) { // This can typically occur when a file is renamed. What happens is the client "closes" the old file before any file system "rename" event makes it to us. Resulting // in us trying to refresh the "closed" files buffer with what's on disk; however, there's nothing actually on disk because the file was renamed. textAndVersion = TextAndVersion.Create(SourceText.From(string.Empty), VersionStamp.Default, filePath: _filePath); } - catch (DirectoryNotFoundException) - { - var directory = Path.GetDirectoryName(_filePath); - Directory.CreateDirectory(directory); - textAndVersion = TextAndVersion.Create(SourceText.From(string.Empty), VersionStamp.Default, filePath: _filePath); - } return Task.FromResult(textAndVersion); }