From 535b601d552de8d94f290e5ef25168d1e05a3ead Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Thu, 15 Feb 2018 17:26:07 +0000 Subject: [PATCH] Multitargeted generate AngleSharp twice, so minimise the chance of either seeing the other in an incomplete state during build --- src/anglesharp/AngleSharpBuilder/Program.cs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/anglesharp/AngleSharpBuilder/Program.cs b/src/anglesharp/AngleSharpBuilder/Program.cs index a4b4603aeb..2e2261030e 100644 --- a/src/anglesharp/AngleSharpBuilder/Program.cs +++ b/src/anglesharp/AngleSharpBuilder/Program.cs @@ -45,8 +45,24 @@ namespace AngleSharpBuilder RemoveStrongName(moduleDefinition); SetAssemblyName(moduleDefinition, "Microsoft.AspNetCore.Blazor.AngleSharp"); - moduleDefinition.Write( - Path.Combine(outputDir, $"{moduleDefinition.Name}.dll")); + // Try to minimize the chance of a parallel build reading the assembly in an + // incomplete state by writing it to a temp location then moving the result. + // There's still a race condition here, but hopefully this is enough to stop + // it from surfacing in practice. + + var tempFilePath = Path.GetTempFileName(); + moduleDefinition.Write(tempFilePath); + + var outputPath = Path.Combine(outputDir, $"{moduleDefinition.Name}.dll"); + try + { + File.Delete(outputPath); + File.Move(tempFilePath, outputPath); + } + finally + { + File.Delete(tempFilePath); // In case it didn't get moved above + } } private static void SetAssemblyName(ModuleDefinition moduleDefinition, string name)