Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Build.UnitTests/BinaryLogger_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,39 @@ public void BinaryLoggerShouldNotThrowWhenMetadataCannotBeExpanded()
ObjectModelHelpers.BuildProjectExpectSuccess(project, binaryLogger);
}

/// <summary>
/// Regression test for dotnet/dotnet#5433 — ClearCacheDirectory must not destroy the
/// ProjectImports archive before it is embedded in the binlog.
/// </summary>
[Fact]
public void BinlogEmbeddedImportsSurviveClearCacheDirectory()
{
string logFilePath = Path.Combine(_env.DefaultTestDirectory.Path, "test.binlog");

var collector = new ProjectImportsCollector(logFilePath, createFile: false, runOnBackground: false);
collector.AddFileFromMemory("testfile.proj", "<Project />");

// This is what XMake.cs does after EndBuild — wipes the cache directory.
FileUtilities.ClearCacheDirectory();

// ProcessResult must still read the archive after the cache dir is gone.
bool archiveRead = false;
collector.ProcessResult(
stream =>
{
stream.Length.ShouldBeGreaterThan(0);
archiveRead = true;
},
error => throw new InvalidOperationException(error));
archiveRead.ShouldBeTrue("Archive must be readable after ClearCacheDirectory");

// DeleteArchive must not throw (directory must still exist).
collector.DeleteArchive();

// Satisfy the fixture's expectation that _logFile exists.
File.WriteAllText(_logFile, string.Empty);
}

/// <summary>
/// Regression test for https://github.com/dotnet/msbuild/issues/6323.
/// </summary>
Expand Down
15 changes: 7 additions & 8 deletions src/Build/Logging/BinaryLogger/ProjectImportsCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,12 @@ public ProjectImportsCollector(
}
else
{
string cacheDirectory = FileUtilities.GetCacheDirectory();
if (!FileSystems.Default.DirectoryExists(cacheDirectory))
{
Directory.CreateDirectory(cacheDirectory);
}
// Store the temporary archive in TempFileDirectory rather than GetCacheDirectory()
// because ClearCacheDirectory() wipes the cache dir during build shutdown.
string tempDirectory = FileUtilities.TempFileDirectory;

// Archive file will be temporarily stored in MSBuild cache folder and deleted when no longer needed
_archiveFilePath = Path.Combine(
cacheDirectory,
tempDirectory,
GetArchiveFilePath(
Path.GetFileName(logFilePath),
sourcesArchiveExtension));
Expand Down Expand Up @@ -304,7 +301,9 @@ public void Close()
public void DeleteArchive()
{
Close();
File.Delete(_archiveFilePath);

// Best effort — the archive may already have been cleaned up.
FileUtilities.DeleteNoThrow(_archiveFilePath);
}
}
}