Skip to content
This repository was archived by the owner on Apr 27, 2026. It is now read-only.
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,45 @@ internal static class BuildCacheFileUtilities
</Project>
""";

internal static async Task<string> BuildAndCreateCacheFileAsync(string csprojFilePath)
internal static async Task BuildAndCreateCacheFileAsync(string csprojFilePath)
{
if (string.IsNullOrEmpty(csprojFilePath))
{
throw new ArgumentException($"The csproj file path is null or empty");
}

if (!File.Exists(csprojFilePath))
{
throw new FileNotFoundException($"The csproj file does not exist: {csprojFilePath}");
}

DirectoryInfo directoryInfo = new DirectoryInfo(Path.GetDirectoryName(csprojFilePath));
FileInfo lastBuildErrorLogFile = new FileInfo(Path.Combine(directoryInfo.FullName, ".net-interactive-builderror"));

CleanObjFolder(directoryInfo);

string tempDirectoryBuildTarget =
Path.Combine(Path.GetDirectoryName(csprojFilePath), DirectoryBuildTargetFilename);
File.WriteAllText(tempDirectoryBuildTarget, DirectoryBuildTargetsContent);
string tempDirectoryBuildTarget = Path.Combine(Path.GetDirectoryName(csprojFilePath), DirectoryBuildTargetFilename);

var args = "";
if (Path.Exists(csprojFilePath))
try
{
File.WriteAllText(tempDirectoryBuildTarget, DirectoryBuildTargetsContent);
}
catch (UnauthorizedAccessException ex)
{
throw new UnauthorizedAccessException($"Failed to create the target file due to unauthorized access: {tempDirectoryBuildTarget}", ex);
}
catch (IOException ex)
{
args = $@"""{csprojFilePath}"" {args}";
throw new IOException($"Failed to create the target file due to an I/O error: {tempDirectoryBuildTarget}", ex);
}

var args = $@"""{csprojFilePath}""";

var result = await new Dotnet(directoryInfo).Build(args: args);

if (result.ExitCode != 0)
{
File.WriteAllText(
lastBuildErrorLogFile.FullName,
string.Join(Environment.NewLine, result.Error));
throw new InvalidOperationException($"Build failed with exit code {result.ExitCode}. See {lastBuildErrorLogFile.FullName} for details.");
}
else if (lastBuildErrorLogFile.Exists)
{
Expand All @@ -111,9 +126,11 @@ internal static async Task<string> BuildAndCreateCacheFileAsync(string csprojFil
// Clean up the temp project file
File.Delete(tempDirectoryBuildTarget);

result.ThrowOnFailure();

return string.Empty;
var cacheFile = FindCacheFile(directoryInfo);
if (cacheFile == null || !cacheFile.Exists)
{
throw new FileNotFoundException($"Cache file not found after build completion in directory: {directoryInfo.FullName}");
}
}

private static void CleanObjFolder(DirectoryInfo directoryInfo)
Expand Down