Skip to content

Commit

Permalink
Increase tolerance for file conflicts within VS
Browse files Browse the repository at this point in the history
  • Loading branch information
AArnott committed Dec 30, 2017
1 parent d116b7b commit aeba285
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/Nerdbank.GitVersioning.Tasks/AssemblyVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public override bool Execute()
ns.Types.Add(this.CreateThisAssemblyClass());

Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile));
using (var file = File.OpenWrite(this.OutputFile))
FileStream file = null;
Utilities.FileOperationWithRetry(() => file = File.OpenWrite(this.OutputFile));
using (file)
{
using (var fileWriter = new StreamWriter(file, new UTF8Encoding(true), 4096, leaveOpen: true))
{
Expand Down Expand Up @@ -213,7 +215,8 @@ public override bool Execute()
this.CreateThisAssemblyClass();

Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile));
File.WriteAllText(this.OutputFile, this.generator.GetCode());
string fileContent = this.generator.GetCode();
Utilities.FileOperationWithRetry(() => File.WriteAllText(this.OutputFile, fileContent));
}

return !this.Log.HasLoggedErrors;
Expand Down
2 changes: 1 addition & 1 deletion src/Nerdbank.GitVersioning.Tasks/NativeVersionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public override bool Execute()
this.generator.EndFile();

Directory.CreateDirectory(Path.GetDirectoryName(this.OutputFile));
File.WriteAllText(this.OutputFile, this.generator.GetCode());
Utilities.FileOperationWithRetry(() => File.WriteAllText(this.OutputFile, this.generator.GetCode()));
}

return !this.Log.HasLoggedErrors;
Expand Down
31 changes: 31 additions & 0 deletions src/Nerdbank.GitVersioning.Tasks/Utilities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Validation;

namespace Nerdbank.GitVersioning.Tasks
{
internal static class Utilities
{
private const int ProcessCannotAccessFileHR = unchecked((int)0x80070020);

internal static void FileOperationWithRetry(Action operation)
{
Requires.NotNull(operation, nameof(operation));

for (int retriesLeft = 6; retriesLeft > 0; retriesLeft--)
{
try
{
operation();
}
catch (IOException ex) when (ex.HResult == ProcessCannotAccessFileHR && retriesLeft > 0)
{
Task.Delay(100).Wait();
continue;
}
}
}
}
}

0 comments on commit aeba285

Please sign in to comment.