diff --git a/src/Core/ApiClientCodeGen.Core.Tests/Generators/FileHelperSafeReadTests.cs b/src/Core/ApiClientCodeGen.Core.Tests/Generators/FileHelperSafeReadTests.cs new file mode 100644 index 0000000000..e29c8eb5a5 --- /dev/null +++ b/src/Core/ApiClientCodeGen.Core.Tests/Generators/FileHelperSafeReadTests.cs @@ -0,0 +1,41 @@ +using System; +using System.IO; +using FluentAssertions; +using Rapicgen.Core.Generators; +using Xunit; + +namespace ApiClientCodeGen.Core.Tests.Generators +{ + public class FileHelperSafeReadTests + { + [Fact] + public void SafeReadAllLines_Should_Read_File_Content() + { + // Arrange + var tempFile = Path.GetTempFileName(); + var expected = new[] { "Line 1", "Line 2", "Line 3" }; + File.WriteAllLines(tempFile, expected); + + try + { + // Act + var lines = FileHelper.SafeReadAllLines(tempFile); + + // Assert + lines.Should().BeEquivalentTo(expected); + } + finally + { + // Cleanup + File.Delete(tempFile); + } + } + + [Fact(Skip = "Only meant to be run manually to test very long paths")] + public void SafeReadAllLines_Should_Handle_Long_Paths() + { + // This test is skipped and only meant to be run manually + // on Windows systems with long paths to verify the fix + } + } +} \ No newline at end of file diff --git a/src/Core/ApiClientCodeGen.Core/Generators/CSharpFileMerger.cs b/src/Core/ApiClientCodeGen.Core/Generators/CSharpFileMerger.cs index 27d7aec949..9a227f97c9 100644 --- a/src/Core/ApiClientCodeGen.Core/Generators/CSharpFileMerger.cs +++ b/src/Core/ApiClientCodeGen.Core/Generators/CSharpFileMerger.cs @@ -98,7 +98,7 @@ private static string GenerateCombinedSource( foreach (var file in files.Where(c => !c.EndsWith("tests.cs", StringComparison.OrdinalIgnoreCase))) { - var sourceLines = File.ReadAllLines(file); + var sourceLines = FileHelper.SafeReadAllLines(file); foreach (var sourceLine in sourceLines) { if (string.IsNullOrWhiteSpace(sourceLine)) @@ -152,7 +152,7 @@ private static IEnumerable GetUniqueNamespaces(IEnumerable files foreach (var file in files) { - IEnumerable sourceLines = File.ReadAllLines(file); + IEnumerable sourceLines = FileHelper.SafeReadAllLines(file); foreach (var sourceLine in sourceLines) { @@ -177,7 +177,7 @@ private static IEnumerable GetAssemblyAttributes(IEnumerable fil foreach (var file in files) { - IEnumerable sourceLines = File.ReadAllLines(file); + IEnumerable sourceLines = FileHelper.SafeReadAllLines(file); foreach (var sourceLine in sourceLines) { diff --git a/src/Core/ApiClientCodeGen.Core/Generators/FileHelper.cs b/src/Core/ApiClientCodeGen.Core/Generators/FileHelper.cs index 3502dafdfe..7c843df412 100644 --- a/src/Core/ApiClientCodeGen.Core/Generators/FileHelper.cs +++ b/src/Core/ApiClientCodeGen.Core/Generators/FileHelper.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Security.Cryptography; +using System.Text; namespace Rapicgen.Core.Generators { @@ -36,5 +37,58 @@ public static string CalculateChecksum(string filename) .ToUpperInvariant(); } } + + /// + /// Safely reads all lines from a file, handling potential path-too-long issues on Windows + /// + /// The file to read + /// The lines from the file + public static string[] SafeReadAllLines(string path) + { + try + { + return File.ReadAllLines(path); + } + catch (PathTooLongException) + { + // For long paths, use manual file reading approach + using (var fileStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.SequentialScan)) + using (var reader = new StreamReader(fileStream, Encoding.UTF8, true)) + { + var lines = new System.Collections.Generic.List(); + string line; + while ((line = reader.ReadLine()) != null) + { + lines.Add(line); + } + return lines.ToArray(); + } + } + catch (DirectoryNotFoundException) + { + // If we encounter directory not found, try with a more robust approach + // This catches cases on Windows where the directory seems correct but is too long + try + { + // Try another approach for reading the file + using (var fileStream = new FileStream( + path, + FileMode.Open, + FileAccess.Read, + FileShare.Read, + 4096, + FileOptions.SequentialScan)) + using (var reader = new StreamReader(fileStream, Encoding.UTF8, true)) + { + return reader.ReadToEnd().Replace("\r\n", "\n").Split('\n'); + } + } + catch + { + // If all approaches fail, re-throw the original exception + throw; + } + } + } } } \ No newline at end of file