From abd334f401aad59963ebb717921cb26a24a5f8d4 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Jun 2025 03:07:57 +0000 Subject: [PATCH 1/2] Initial plan From e31b2071ee8dce7945dcd585fa898fcab00499fc Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Fri, 27 Jun 2025 03:21:25 +0000 Subject: [PATCH 2/2] Fix BuildVariables temp file deletion issue with lazy initialization Co-authored-by: BenjaminMichaelis <22186029+BenjaminMichaelis@users.noreply.github.com> --- .../RepositoryPaths.Tests.cs | 34 +++++++++++++++ IntelliTect.Multitool/RepositoryPaths.cs | 42 +++++++++++++++++-- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/IntelliTect.Multitool.Tests/RepositoryPaths.Tests.cs b/IntelliTect.Multitool.Tests/RepositoryPaths.Tests.cs index 902ce18..eac1279 100644 --- a/IntelliTect.Multitool.Tests/RepositoryPaths.Tests.cs +++ b/IntelliTect.Multitool.Tests/RepositoryPaths.Tests.cs @@ -43,4 +43,38 @@ public void TrySearchForGitContainingDirectory_ReturnsFalseWhenNotFound() Assert.False(RepositoryPaths.TrySearchForGitContainingDirectory(new DirectoryInfo(path), out string gitParentDirectory)); Assert.Empty(gitParentDirectory); } + + [Fact] + public void BuildVariables_HandlesFileNotFound_Gracefully() + { + // Save current temp file if it exists + string tempFilePath = Path.Combine(Path.GetTempPath(), RepositoryPaths.BuildVariableFileName); + string? backupContent = null; + bool fileExisted = File.Exists(tempFilePath); + if (fileExisted) + { + backupContent = File.ReadAllText(tempFilePath); + File.Delete(tempFilePath); + } + + try + { + // Reset the static field to force re-initialization + var field = typeof(RepositoryPaths).GetField("_buildVariables", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static); + field?.SetValue(null, null); + + // Access BuildVariables when file doesn't exist - should not throw + var buildVars = RepositoryPaths.BuildVariables; + Assert.NotNull(buildVars); + Assert.Empty(buildVars); + } + finally + { + // Restore the file if it existed + if (fileExisted && backupContent != null) + { + File.WriteAllText(tempFilePath, backupContent); + } + } + } } \ No newline at end of file diff --git a/IntelliTect.Multitool/RepositoryPaths.cs b/IntelliTect.Multitool/RepositoryPaths.cs index 68627f2..470202c 100644 --- a/IntelliTect.Multitool/RepositoryPaths.cs +++ b/IntelliTect.Multitool/RepositoryPaths.cs @@ -15,10 +15,44 @@ public static class RepositoryPaths /// /// Holds the key value pairs of the build variables that this class can use. /// - public static ReadOnlyDictionary BuildVariables { get; } = new(File.ReadAllLines(Path.Combine(Path.GetTempPath(), BuildVariableFileName)) - .Select(line => line.Split("::")) - .ToDictionary(split => split[0].Trim(), - split => !string.IsNullOrEmpty(split[1]) ? split[1].Trim() : null)); + public static ReadOnlyDictionary BuildVariables + { + get + { + if (_buildVariables == null) + { + _buildVariables = LoadBuildVariables(); + } + return _buildVariables; + } + } + + private static ReadOnlyDictionary? _buildVariables; + + private static ReadOnlyDictionary LoadBuildVariables() + { + try + { + string filePath = Path.Combine(Path.GetTempPath(), BuildVariableFileName); + var lines = File.ReadAllLines(filePath); + var dictionary = lines + .Select(line => line.Split("::")) + .ToDictionary(split => split[0].Trim(), + split => !string.IsNullOrEmpty(split[1]) ? split[1].Trim() : null); + return new ReadOnlyDictionary(dictionary); + } + catch (FileNotFoundException) + { + // Return empty dictionary if the build variables file doesn't exist + // This can happen when the temp file is cleaned up by the OS + return new ReadOnlyDictionary(new Dictionary()); + } + catch (DirectoryNotFoundException) + { + // Return empty dictionary if the temp directory doesn't exist + return new ReadOnlyDictionary(new Dictionary()); + } + } /// /// Finds the root of the repository by looking for the directory containing the .git directory.