Skip to content

Commit

Permalink
Fix: Process cannot access the file when building a multi-target proj…
Browse files Browse the repository at this point in the history
…ect (#197) (#207)
  • Loading branch information
gasparnagy authored Jul 12, 2024
1 parent 194e995 commit 2adffb3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
## Bug fixes:

* Fix: Reqnroll.Autofac: Objects registered in the global container cannot be relsolved in BeforeTestRun/AfterTestRun hooks (#183)
* Fix: Process cannot access the file when building a multi-target project (#197)

*Contributors of this release (in alphabetical order):* @ajeckmans, @cimnine, @gasparnagy, @obligaron, @stbychkov

Expand Down
30 changes: 28 additions & 2 deletions Reqnroll.Tools.MsBuild.Generation/CodeBehindWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public string WriteCodeBehindFile(string outputPath, string featureFile, TestFil
{
if (!FileSystemHelper.FileCompareContent(outputPath, testFileGeneratorResult.GeneratedTestCode))
{
File.WriteAllText(outputPath, testFileGeneratorResult.GeneratedTestCode, Encoding.UTF8);
WriteAllTextWithRetry(outputPath, testFileGeneratorResult.GeneratedTestCode, Encoding.UTF8);
}
}
else
Expand All @@ -42,10 +42,36 @@ public string WriteCodeBehindFile(string outputPath, string featureFile, TestFil
Directory.CreateDirectory(directoryPath);
}

File.WriteAllText(outputPath, testFileGeneratorResult.GeneratedTestCode, Encoding.UTF8);
WriteAllTextWithRetry(outputPath, testFileGeneratorResult.GeneratedTestCode, Encoding.UTF8);
}

return outputPath;
}

/// <summary>
/// When building a multi-targeted project, the build system may try to write the same file multiple times,
/// and this can cause an IOException ("The process cannot access the file because it is being used by another process.").
/// See https://github.com/reqnroll/Reqnroll/issues/197
/// Once we move to Roslyn-based generation, this problem will go away, but for now, we use a workaround of
/// retrying the write operation a few times (the content is anyway the same).
/// </summary>
private void WriteAllTextWithRetry(string path, string contents, Encoding encoding)
{
const int maxAttempts = 5;
for (int i = 1; i <= maxAttempts; i++)
{
try
{
File.WriteAllText(path, contents, encoding);
return;
}
catch (IOException)
{
if (i == maxAttempts)
throw;
System.Threading.Thread.Sleep(i * 50);
}
}
}
}
}
12 changes: 9 additions & 3 deletions Reqnroll.Utils/FileSystemHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,16 @@ public static bool FileCompare(string filePath1, string filePath2)
// files are not the same.
public static bool FileCompareContent(string filePath1, string fileContent)
{
var currentFileContent = File.ReadAllText(filePath1);

return string.CompareOrdinal(currentFileContent, fileContent) == 0;
try
{
var currentFileContent = File.ReadAllText(filePath1);

return string.CompareOrdinal(currentFileContent, fileContent) == 0;
}
catch (IOException)
{
return false;
}
}

public static string NormalizeDirectorySeparators(string path)
Expand Down

0 comments on commit 2adffb3

Please sign in to comment.