Skip to content

Commit 827b5d7

Browse files
committed
Clean up the cache folder before saving
1 parent e939796 commit 827b5d7

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Razor/src/Microsoft.CodeAnalysis.Remote.Razor/RemoteMefComposition.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ private static async Task TrySaveCachedExportProviderAsync(CachedComposition cac
120120
var cacheDirectory = Path.GetDirectoryName(compositionCacheFile).AssumeNotNull();
121121
var directoryInfo = Directory.CreateDirectory(cacheDirectory);
122122

123+
CleanCacheDirectory(directoryInfo, cancellationToken);
124+
123125
var tempFilePath = Path.Combine(cacheDirectory, Path.GetRandomFileName());
124126
using (var cacheStream = new FileStream(compositionCacheFile, FileMode.Create, FileAccess.Write, FileShare.None, bufferSize: 4096, useAsync: true))
125127
{
@@ -134,6 +136,25 @@ private static async Task TrySaveCachedExportProviderAsync(CachedComposition cac
134136
}
135137
}
136138

139+
private static void CleanCacheDirectory(DirectoryInfo directoryInfo, CancellationToken cancellationToken)
140+
{
141+
try
142+
{
143+
// Delete any existing cached files.
144+
foreach (var fileInfo in directoryInfo.EnumerateFiles())
145+
{
146+
// Failing to delete any file is fine, we'll just try again the next VS session in which we attempt
147+
// to write a new cache
148+
fileInfo.Delete();
149+
cancellationToken.ThrowIfCancellationRequested();
150+
}
151+
}
152+
catch (Exception)
153+
{
154+
// We ignore all errors when cleaning the cache directory, because we'll try again if the cache is corrupt.
155+
}
156+
}
157+
137158
[return: NotNullIfNotNull(nameof(cacheDirectory))]
138159
private static string? GetCompositionCacheFile(string? cacheDirectory)
139160
{

src/Razor/test/Microsoft.CodeAnalysis.Remote.Razor.Test/RemoteMefCompositionTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,29 @@ public async Task CorruptCacheFileIsOverwritten()
6262
Assert.Single(Directory.GetFiles(cacheDirectory));
6363
Assert.True(new FileInfo(cacheFile).Length > 35);
6464
}
65+
66+
[Fact]
67+
public async Task CleansOldCacheFiles()
68+
{
69+
using var tempRoot = new TempRoot();
70+
var cacheDirectory = tempRoot.CreateDirectory().Path;
71+
Directory.CreateDirectory(cacheDirectory);
72+
73+
File.WriteAllText(Path.Combine(cacheDirectory, Path.GetRandomFileName()), "");
74+
File.WriteAllText(Path.Combine(cacheDirectory, Path.GetRandomFileName()), "");
75+
File.WriteAllText(Path.Combine(cacheDirectory, Path.GetRandomFileName()), "");
76+
File.WriteAllText(Path.Combine(cacheDirectory, Path.GetRandomFileName()), "");
77+
78+
Assert.Equal(4, Directory.GetFiles(cacheDirectory).Length);
79+
80+
var cacheFile = RemoteMefComposition.TestAccessor.GetCacheCompositionFile(cacheDirectory);
81+
82+
var exportProvider = await RemoteMefComposition.CreateExportProviderAsync(cacheDirectory, DisposalToken);
83+
84+
Assert.NotNull(RemoteMefComposition.TestAccessor.SaveCacheFileTask);
85+
await RemoteMefComposition.TestAccessor.SaveCacheFileTask;
86+
87+
Assert.Single(Directory.GetFiles(cacheDirectory));
88+
Assert.True(new FileInfo(cacheFile).Length > 35);
89+
}
6590
}

0 commit comments

Comments
 (0)